python程序 计算时间差加减
1 import time,datetime,sys,string
2class 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),seconds=int(strs))
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
18print "example:"
19print "1.00+2.00"
20print "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)
29 i = 1
30 while len(inps)>(i+1):
31 if inps=="+":
32 c.add(inps)
33 elif inps=="-":
34 c.sub(inps)
35 else:
36 continue
37 i = i + 2
38 print "Result:" + c.show() + "\n"
39 except:
40 print "error\n"
41 pass
页:
[1]