python程序 计算时间差加减

1 import time,datetime,sys,string
2  class clock:
3 def __init__(self):
4 self.time = datetime.datetime(1,1,1,0)
5 def obase(self,inputstr):
6 strs = string.split(inputstr,".")
7 return datetime.timedelta(minutes=int(strs[0]),seconds=int(strs[1]))
8 def add(self,inputstr):
9 self.time = self.time + self.obase(inputstr)
10 def sub(self,inputstr):
11 self.time = self.time - self.obase(inputstr)
12 def show(self):
13 if self.time.hour==0:
14 return str(self.time.minute)+":"+str(self.time.second)
15 else:
16 return str(self.time.hour)+":"+str(self.time.minute)+":"+str(self.time.second)
17
18  print "example:"
19  print "1.00+2.00"
20  print "2.30-1.00+2.00+2.00\n"
21 while True:
22 inp = raw_input("input:")
23 inp = string.replace(inp,"+"," + ")
24 inp = string.replace(inp,"-"," - ")
25 try:
26 inps = string.split(inp," ")
27 c = clock()
28 c.add(inps[0])
29 i = 1
30 while len(inps)>(i+1):
31 if inps[i]=="+":
32 c.add(inps[i+1])
33 elif inps[i]=="-":
34 c.sub(inps[i+1])
35 else:
36 continue
37 i = i + 2
38 print "Result:" + c.show() + "\n"
39 except:
40 print "error\n"
41 pass
原文地址:https://www.cnblogs.com/catcat811/p/1660950.html