一、时间相关模块
2.1 常用时间模块
time: 时间
datetime: 日期
calendar: 日历
2.2 time
UTC:格林威治天文时间, 世界标准时间, UTC+8
DST:夏令时
import time
# 获取当前时间,时间戳,以s为单位
c = time.time()
# print(c)
# 将时间戳转换为UTC时间
g = time.gmtime(c)
print(g)
# 将时间戳转换成本地时间
l = time.localtime(c)
print(l)
# 将UTC时间转换成时间戳
c2 = time.mktime(l)
print(c2)
c3 = time.mktime((2019,12,17,20,59,59,1,351,0))
print(c3)
# 将时间戳可视化
c4 = time.ctime(c3)
print(c4)
# 将元组格式化字符串
s = time.strftime("%Y:%m:%d %H:%M:%S",l)
print(s)
# 将格式化时间字符串转换成时间元组
p = time.strptime("2019-12-17 21:07:50","%Y-%m-%d %H:%M:%S")
print(p)
# 休眠暂停
time.sleep(5)
2.3 datatime 日期模块
import datetime
# 获取当前时间
d1 = datetime.datetime.now()
print(d1)
# 将指定时间转换成字符串
d4 = d1.strftime("%Y-%m-%d")
print(d4)
# 根据元组获取指定时间
d2 = datetime.datetime(2019,12,17,21,19,59,124213)
print(d2)
# 把字符串转换成时间
d5 = datetime.datetime.strptime(d4,"%Y-%m-%d")
print(d5)
# 获取时间间隔
d6 = datetime.datetime(2019,12,15,21,23,59,8888)
d7 = datetime.datetime(2019,12,17,21,22,59,9999)
d8 = d7 - d6
print(d8,d8.days)
2.4 calendar 日历模块
import calendar
# 获取指定年和月日历形式
print(calendar.month(2019,12))
# 二维元组列表形式展现
print(calendar.monthcalendar(2019,12))
# 查看一年的日历
print(calendar.calendar(2019))
# 判断是否为闰年
print(calendar.isleap(2018))
# 年份区间段里, 有几个闰年
print(calendar.leapdays(2000,2020))
# 指定月份的天数,这个月第一天周几?
print(calendar.monthrange(2019,12))
# 判断当前日期星期数
print(calendar.weekday(2019,12,17))