python3 封装好的时间戳转换函数,可直接使用

import time
# =====封装函数:格式化时间转成时间戳======
def format_time_to_timestamp(format_time=None):
if format_time:
time_tuple = time.strptime(format_time,'%Y-%m-%d %X')
result = time.mktime(time_tuple)
return int(result)
return int(time.time())

#查看调用结果
print(format_time_to_timestamp())
print(format_time_to_timestamp('2009-10-10 18:01:11'))

# ==========封装函数:时间戳转换成格式化时间==========
def timestamp_to_format_time(timestamp=None,format='%Y-%m-%d %X'):
if timestamp:
time_tuple = time.localtime(timestamp)
result = time.strftime(format,time_tuple)
return result
else:
return time.strftime(format)

#查看调用结果
print(timestamp_to_format_time())
print(timestamp_to_format_time(1255168871))
posted @ 2019-05-01 18:17  hello_libra  阅读(202)  评论(0编辑  收藏  举报