web-automation-demo/Library/Time.py

39 lines
1.0 KiB
Python

import time
def fmt_time(fmt='%Y-%m-%d %H:%M:%S', ts=None, utc=False):
return time.strftime(fmt, [time.localtime, time.gmtime][utc](ts))
def par_time(fmt='', dt=''):
return time.mktime(time.strptime(dt, fmt))
def asc_time(ts=None):
return time.asctime(time.localtime(ts))
def int_time():
return int(time.time())
def mic_time():
return round(time.time(), 6)
if __name__ == '__main__':
# e.g. Get the current formatting time.
print(fmt_time(fmt='%Y-%m-%d %H:%M:%S'))
# e.g. Get the formatting time and specify the timestamp.
print(fmt_time(fmt='%Y-%m-%d %H:%M:%S', ts=0))
# e.g. Get UTC formatting time.
print(fmt_time(fmt='%Y-%m-%d %H:%M:%S', ts=0, utc=True))
# e.g. Convert the formatting time to timestamp.
print(par_time(fmt='%Y-%m-%d %H:%M:%S', dt='2020-12-31 12:00:00'))
# e.g. Get ASC formatting time.
print(asc_time())
# e.g. Get integer timestamp.
print(int_time())
# e.g. Get accurate to microsecond timestamp.
print(mic_time())