Python格式化时间
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.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 ***
相关文章