模块
时间模块:time/datatime/calendar。
系统操作文件模块:os
time模块
- 介绍
1 import time
2
3 # 获取当前时间, 结果是存在时间元组中的。星期一是从0开始的
4 # 结果:time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=18, tm_min=21, tm_sec=46, tm_wday=3, tm_yday=234, tm_isdst=0)
5 c_time = time.localtime()
6 print(c_time)
7
8
9 # 获取时间元组中指定的时间。比如 年
10 year = c_time[0]
11 print(year)
12 # 获取时间元组中指定的时间。比如 年。根据时间元组中指定的时间字段获取
13 year = c_time.tm_year
14 print(year)
15
16
17 # 获取时间戳。指定时间对应的秒数。当前时间对应的时间戳
18 now = time.time()
19 print(now)
20
21
22 # 时间格式化。占位格式:
23 """
24 格式化时间的占位符:
25 %Y ---- 年
26 %m --- 月
27 %d ---- 日
28 %H ---- 24小时制
29 %I ----- 12小时制
30 %p ---- 标记上下午
31 %M --- 分
32 %S ---- 秒
33 %w --- 星期
34 %j ----- 一年中的第几天
35 %z ---- 时区
36 """
37 # 将时间元组格式转为指定时间格式
38 str_time = time.strftime("%Y-%m-%d %H:%M:%S",c_time)
39 print(str_time)
40
41 # 将指定的时间格式转化为时间元组
42 time_string = time.strptime(str_time,"%Y-%m-%d %H:%M:%S")
43 print(time_string)
44
45
46 # 获得自定义时间的时间戳
47 c_time = time.mktime(time_string)
48 print(c_time)
49
50
51 # 将时间戳转换为时间元组
52 time_str = time.localtime(c_time)
53 print(time_str)
- 练习
1 """
2 题干:
3 指定时间:"2018-10-22 12:15:12"
4 在这个时间基础上,获取三天后的时间 "2018-10-25 12:15:12"
5
6 解决:
7 1。指定时间 --- 时间元组
8 2。时间元组 --- 时间戳
9 3。时间戳 + 3天的秒数
10 4。新时间戳 --- 时间元组
11 5。新时间元组 --- 格式化时间
12 """
13 import time
14 def get_time(src_time):
15 time_tuple = time.strptime(src_time,"%Y-%m-%d %H:%M:%S") # 指定时间 变 时间元组
16 c_time = time.mktime(time_tuple) # 时间元组 变 时间戳
17 new_time = c_time + 3 * 24 * 60 * 60 # 时间戳 + 3 天
18 new_time_tuple = time.localtime(new_time) # 时间戳 变 时间元组
19 time_str = time.strftime("%Y-%m-%d %H:%M:%S",new_time_tuple)# 时间元组 变 格式时间
20 return time_str
21
22 if __name__ == '__main__':
23 res = get_time("2018-10-22 13:15:12")
24 print(res)
datetime模块
- 介绍
1 import datetime
2
3
4 # 获取当前时间。 结果:2019-08-22 19:19:21.842469
5 c_time = datetime.datetime.now()
6 print(c_time)
7
8
9 # 获取日期。结果:2019-08-22
10 d_time = datetime.date.today()
11 print(d_time)
12
13
14 # 获取当前时间对应的星期。 结果:3。表示星期四
15 w_time = c_time.weekday()
16 print(w_time)
17
18
19 # 获取年、月、日。 结果:2019
20 y_time = c_time.year
21 print(y_time)
22
23
24 # 自定义时间。 结果:2018-03-23 00:00:00。时分秒没传默认是零,年月日必须传值
25 p_time = datetime.datetime(year=2018,month=3,day=23)
26 print(p_time) # 类型:datetime.datetime
27
28
29 # 获取指定时间的几天后的时间。结果:2018-03-26 00:00:00
30 d_time = p_time + datetime.timedelta(days=3)
31 print(d_time) # 类型:datetime.datetime
32
33
34 # 格式化时间:年/月/日 时 分 秒。结果:字符串类型,2018/03/23 00 00 00
35 f_time = p_time.strftime("%Y-%m-%d %H:%M:%S")
36 print(f_time) # 类型:str
37
38
39 # 反格式化时间
40 r_time = datetime.datetime.strptime(f_time,"%Y-%m-%d %H:%M:%S")
41 print(r_time) # 类型:datetime.datetime
42 print(type(r_time))
43
44
45 # 使用运算符 "—"。 计算两个时间的时间差. 结果:355 days, 0:00:00
46 fr_time = datetime.datetime(2017,12,31)
47 se_time = datetime.datetime(2018,12,21)
48 diff = se_time - fr_time
49 print(diff)
50
51
52 # 获取相差天数。 结果:355
53 days = diff.days
54 print(days)
55
56
57 # 获取时间差差异的秒数。 结果:30672000.0
58 tol = diff.total_seconds()
59 print(tol)
60
61
62 # 获取后面时分秒结合起来的秒数,不包括前面的天数。 结果:0
63 res = diff.seconds
64 print(res)
calendar模块
- 介绍
1 import calendar # 1900年起始 2 3 # 获取指定年的日历 4 y_calendar = calendar.calendar(2018) 5 print(y_calendar) 6 7 8 # 获取指定年 指定月的日历 9 m_calendar = calendar.month(2017,12) 10 print(m_calendar) 11 12 13 # 判断该年是否是闰年。 结果:True 14 is_leap = calendar.isleap(2000) 15 print(is_leap) 16 17 18 # 获取指定月对应中 把星期作为分割点,一个星期为一个小列表存放在一个大列表中。前面没有数据的话,用零占位。 19 # 结果:[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30], [31, 0, 0, 0, 0, 0, 0]] 20 res = calendar.monthcalendar(2018,12) 21 print(res) 22 23 24 # 获得某天对应的星期。 结果:3 表示 星期4 25 week = calendar.weekday(2018,1,4) 26 print(week)
os模块