|
发表于 2020-6-29 18:22:25
|
显示全部楼层
本楼为最佳答案
- import time
- # def一个函数当计算出的时间列表里面有负数时自动调整为需要的没有负数的数据
- def adjust(mylist, index):
- date = [0, 12, 30, 24, 60, 60]
- if mylist[index]:
- mylist[index] -= 1
- mylist[index + 1] += date[index]
- else:
- index -= 1
- adjust(mylist, index)
- class Timer:
- def start(self):
- self.begin = time.localtime()
- print('计时开始')
- def stop(self):
- self.end = time.localtime()
- self.__calc()
- def __calc(self):
- unit = ['年', '月', '日', '时', '分', '秒']
- self.lasted = ''
- self.mylist = []
- for index in range(6):
- self.mylist.append(self.end[index] - self.begin[index])
- for index in range(6):
- if self.mylist[index] < 0:
- index -= 1
- adjust(self.mylist, index)
- for index in range(6):
- if self.mylist[index]:
- self.lasted += str(self.mylist[index]) + unit[index]
- print('计时结束' + '\n' + '共运行%s' % self.lasted)
- t = Timer()
- t.start()
复制代码 |
|