#时间戳-->结构化时间
#time.gmtime(时间戳) #UTC时间,与英国伦敦当地时间一致
#time.localtime(时间戳) #当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间
>>>time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
#结构化时间-->时间戳
#time.mktime(结构化时间)
>>>time_tuple = time.localtime(1500000000)
>>>time.mktime(time_tuple)
1500000000.0
#结构化时间-->字符串时间
#time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间
>>>time.strftime("%Y-%m-%d %X")
‘2017-07-24 14:55:36‘
>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))
‘2017-07-14‘
#字符串时间-->结构化时间
#time.strptime(时间字符串,字符串对应格式)
>>>time.strptime("2017-03-16","%Y-%m-%d")
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)
>>>time.strptime("07/24/2017","%m/%d/%Y")
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)
#结构化时间 --> %a %b %d %H:%M:%S %Y串
#time.asctime(结构化时间) 如果不传参数,直接返回当前时间的格式化串
>>>time.asctime(time.localtime(1500000000))
‘Fri Jul 14 10:40:00 2017‘
>>>time.asctime()
‘Mon Jul 24 15:18:33 2017‘
#时间戳 --> %a %b %d %H:%M:%S %Y串
#time.ctime(时间戳) 如果不传参数,直接返回当前时间的格式化串
>>>time.ctime()
‘Mon Jul 24 15:19:07 2017‘
>>>time.ctime(1500000000)
‘Fri Jul 14 10:40:00 2017‘
#计算时间差
import time
true_time=time.mktime(time.strptime(‘2017-09-11 08:30:00‘,‘%Y-%m-%d %H:%M:%S‘))
time_now=time.mktime(time.strptime(‘2017-09-12 11:00:00‘,‘%Y-%m-%d %H:%M:%S‘))
dif_time=time_now-true_time
struct_time=time.gmtime(dif_time)
print(‘过去了%d年%d月%d天%d小时%d分钟%d秒‘%(struct_time.tm_year-1970,struct_time.tm_mon-1,
struct_time.tm_mday-1,struct_time.tm_hour,
struct_time.tm_min,struct_time.tm_sec))
import time
# time.sleep(100)
# print(time.time())
# 格式化时间 —— 字符串: 给人看的
# 时间戳时间 —— float时间 : 计算机看的
# 结构化时间 —— 元祖 :计算用的
print(time.strftime("%Y-%m-%d %a %H:%M:%S")) #year month day HOUR MINUTE SECOND
print(time.strftime("%Y/%m/%d %H:%M:%S")) #year month day HOUR MINUTE SECOND
print(time.strftime("%m-%d %H:%M:%S")) #year month day HOUR MINUTE SECOND
print(time.strftime("%H:%M:%S")) #year month day HOUR MINUTE SECOND
print(time.strftime("%H:%M")) #year month day HOUR MINUTE SECOND
struct_time = time.localtime()
print(struct_time)
print(struct_time.tm_year)
import time
# 时间戳和结构化时间
t = time.time()
print(t)
print(time.localtime(3000000000))
print(time.gmtime(t))
print(time.mktime(time.localtime()))
print(time.strptime(‘2000-12.31‘,‘%Y-%m.%d‘))
print(time.strftime(‘%m/%d/%Y %H:%M:%S‘,time.localtime(3000000000)))
print(time.asctime())
import time
true_time=time.mktime(time.strptime(‘2017-09-11 08:30:00‘,‘%Y-%m-%d %H:%M:%S‘))
time_now=time.mktime(time.strptime(‘2017-09-12 11:00:00‘,‘%Y-%m-%d %H:%M:%S‘))
dif_time=time_now-true_time
struct_time=time.gmtime(dif_time)
print(‘过去了%d年%d月%d天%d小时%d分钟%d秒‘%(struct_time.tm_year-1970,struct_time.tm_mon-1,
struct_time.tm_mday-1,struct_time.tm_hour,
struct_time.tm_min,struct_time.tm_sec))