import time
import datetime
# time.time 返回当前时间的时间戳
x = time.time()
print(x)
# 1529042081.8785088
# 返回时间的字符串格式
y1 = time.ctime()
print(y1)
# Fri Jun 15 13:54:41 2018
y2 = time.ctime(time.time() - 10000)
print(y2)
# Fri Jun 15 11:17:01 2018
# 返回结构化时间格式
z = time.gmtime()
print(z)
# time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=6, tm_min=5, tm_sec=55, tm_wday=4, tm_yday=166, tm_isdst=0)
# 结构化时间应用
time_obj = time.gmtime()
print(time_obj)
# time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=6, tm_min=14, tm_sec=42, tm_wday=4, tm_yday=166, tm_isdst=0)
print(time_obj.tm_year, time_obj.tm_mon) # 2018 6
print("{year}-{month}".format(year=time_obj.tm_year, month=time_obj.tm_mon)) # 2018-6
# 结构化本地时间
local_time = time.localtime()
print(local_time)
# time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=14, tm_min=17, tm_sec=47, tm_wday=4, tm_yday=166, tm_isdst=0)
# 结构化时间转化为时间戳
time_obj = time.gmtime()
print(time.mktime(time_obj)) # 1529014942.0
# 时间停顿
time.sleep(4) # 停顿4秒格式
# 把结构化时间转化为格式化字符
print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())) # 2018-06-15 06:29:21
# strptime把其他格式转化为结构化
print(time.strptime('2018-06-15', '%Y-%m-%d'))
# time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=166, tm_isdst=-1)
# DateTime模块
print(datetime.today()) # 2018-06-15 14:40:56.714725
print(datetime.fromtimestamp(time.time())) # 2018-06-15 14:46:25.525196
print(datetime.now()) # 2018-06-15 14:47:38.777948
print(datetime.now().timetuple())
# time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=14, tm_min=51, tm_sec=38, tm_wday=4, tm_yday=166, tm_isdst=-1)
print(datetime.now().replace(2020)) # 2020-06-15 14:53:34.854929
print(datetime.strptime('15/06/18 15:30', '%d/%m/%y %H:%M')) # 2018-06-15 15:30:00