ITxiaobaibai 2019-09-05
walker 经常用到当前时间和相对时间,用来统计程序执行的效率,简单记一下,便于 copy。
>>> import time # 相对时间 >>> startTime = time.time() >>> '%.2fs' % (time.time() - startTime) '24.51s'
# 格式化当前时间 >>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) '2016-01-05 10:34:06'
# 将时间戳转化为时间 >>> time.localtime(1521793044.7850916) time.struct_time(tm_year=2018, tm_mon=3, tm_mday=23, tm_hour=16, tm_min=17, tm_sec=24, tm_wday=4, tm_yday=82, tm_isdst=0) # 将时间戳转化为可读字符串 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1521793044.7850916)) '2018-03-23 16:17:24' # 将时间转化为时间戳 >>> time.strptime('2016-05-05 20:28:54', '%Y-%m-%d %H:%M:%S') time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=20, tm_min=28, tm_sec=54, tm_wday=3, tm_yday=126, tm_isdst=-1) >>> time.mktime(time.strptime('2016-05-05 20:28:54', '%Y-%m-%d %H:%M:%S')) 1462451334.0 # 可读字符串 -> 时间戳 -> 可读字符串 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.strptime('2018-03-23 16:17:24', '%Y-%m-%d %H:%M:%S')) '2018-03-23 16:17:24'
本文出自 walker snapshot