|
发表于 2018-1-16 18:28:42
|
显示全部楼层
不超过1天这个限制条件太方便了,手动
- def fun(t1, t2):
- times = [sum(60**(2-i)*int(j) for i,j in enumerate(time.split(":")))
- for time in (t1, t2)]
- return "{}s".format((times[1] - times[0]) % 86400)
- print(fun("17:55:31", "4:21:57"))
复制代码
datetime也很快
- import datetime
- def fun2(t1, t2):
- t1 = datetime.datetime.strptime(t1, "%H:%M:%S")
- t2 = datetime.datetime.strptime(t2, "%H:%M:%S")
- return (t2 - t1).total_seconds() % 86400
- print(fun2("17:55:31", "4:21:57"))
复制代码
久没用过,小小的写个类
- class timedelta:
- def __init__(self, hours=0, minutes=0, seconds=0):
- self.hours = hours
- self.minutes = minutes
- self.seconds = seconds
- self.time = self.hours * 3600 + self.minutes * 60 + self.seconds
-
- def __sub__(self, other):
- return (self.time - other.time) % 86400
- start = timedelta(17, 55, 31)
- stop = timedelta(4, 21, 57)
- print(stop - start)
复制代码 |
评分
-
查看全部评分
|