import time def time_strftime(fmt='%Y-%m-%d %H:%M:%S', ts=None, utc=False): return time.strftime(fmt, [time.localtime, time.gmtime][utc](ts)) def time_strptime(datetime_format='', dt=''): return time.mktime(time.strptime(dt, datetime_format)) if __name__ == '__main__': # Example. 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'))