opencv_webcam/utils/time_format.py

57 lines
1.3 KiB
Python
Raw Normal View History

2022-01-21 08:40:51 +08:00
# 时间格式化
# 创建人:曾逸夫
2022-01-21 08:53:37 +08:00
# 创建时间2022-01-20
2022-01-21 08:40:51 +08:00
import sys
# 判断时间格式
def is_time(preTime):
if (preTime <= 0):
print(f'时间格式不正确!程序结束!')
sys.exit()
# 时间格式化
def time_format(preTime):
2022-01-21 08:42:31 +08:00
is_time(preTime) # 判断时间格式
m, s = divmod(preTime, 60) # 获取秒
h, m = divmod(m, 60) # 获取时、分
2022-01-21 08:40:51 +08:00
if (0 < s < 1):
time_str = f'{s:.3f}'
2022-01-21 09:27:18 +08:00
# print(time_str)
2022-01-21 08:40:51 +08:00
return time_str
elif (h == 0 and m == 0 and s >= 1):
2022-01-21 09:27:18 +08:00
time_str = f'{s:.3f}'
# print(time_str)
2022-01-21 08:40:51 +08:00
return time_str
elif (h == 0 and m > 0):
2022-01-21 09:27:18 +08:00
m = int(m)
time_str = f'{m}{s:.3f}'
# print(time_str)
2022-01-21 08:40:51 +08:00
return time_str
elif (h > 0):
if (h >= 24):
h = int(h / 24)
2022-01-21 09:27:18 +08:00
m = int(m)
time_str = f'{h}{m}{s:.3f}'
2022-01-21 08:40:51 +08:00
else:
2022-01-21 09:27:18 +08:00
h = int(h)
m = int(m)
time_str = f'{h}{m}{s:.3f}'
# print(time_str)
2022-01-21 08:40:51 +08:00
return time_str
else:
print(f'时间格式化失败!程序结束!')
sys.exit()
2022-01-21 09:29:37 +08:00
# if __name__ == '__main__':
2022-01-21 08:40:51 +08:00
2022-01-21 09:29:37 +08:00
# time_format(0.52362)
# time_format(50.52362)
# time_format(90.52362)
# time_format(5000.52362)
# time_format(3600*24 + 1000.52362)