AutoFramework/Base/Class/Time.py

18 lines
556 B
Python
Raw Normal View History

2022-07-01 22:48:22 +08:00
import time
2022-07-22 16:31:27 +08:00
def time_strftime(fmt='%Y-%m-%d %H:%M:%S', ts=None, utc=False):
return time.strftime(fmt, [time.localtime, time.gmtime][utc](ts))
2022-07-01 22:48:22 +08:00
2022-07-22 16:31:27 +08:00
def time_strptime(datetime_format='', dt=''):
2022-07-01 22:48:22 +08:00
return time.mktime(time.strptime(dt, datetime_format))
2022-07-22 16:31:27 +08:00
2022-07-01 22:48:22 +08:00
if __name__ == '__main__':
# Example.
2022-07-22 16:31:27 +08:00
print(time_strftime(fmt='%Y年%m月%d%H:%M:%S'))
print(time_strftime(fmt='%Y年%m月%d%H:%M:%S', ts=1640012345))
print(time_strftime(fmt='%H:%M:%S', ts=120, utc=True))
print(time_strptime('%Y-%m-%d %H:%M:%S', '2021-12-20 22:59:05'))