首页 > 其他 > 详细

时间模块

时间:2018-08-01 23:48:12      阅读:241      评论:0      收藏:0      [点我收藏+]
常用的时间模块

1.time 时间戳

2.datetime

3.python-deteutil(pip install)

常用的操作:

1.时间各种格式表示

2.各种加减运算

import time
time.time()  #时间戳
1533133461.6765099
start = time.time()
print(start)
1533133482.158626
end = time.time()
print(f‘过了 {end - start} 秒‘)
过了 73.09727120399475 秒
start = time.time()
# 执行各种操作
for i in range(100):  # 计算循环用了多长时间
    print(i)

end = time.time()
print(f‘过了{end - start} s‘)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
过了0.0 s
# time.sleep()
for i in range(5):
    time.sleep(1)
    print(i)
0
1
2
3
4
# 结构化时间与时间戳的转换
time.localtime()  # 返回的是元祖
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=29, tm_sec=35, tm_wday=2, tm_yday=213, tm_isdst=0)
time.localtime(10) #距离1970年的时间转换
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=10, tm_wday=3, tm_yday=1, tm_isdst=0)
time.mktime(time.localtime())
1533134001.0
time.mktime(time.localtime())  # 返回的浮点数时间戳
1533134010.0
for i in range(10):
    time.sleep(1)
    print(time.mktime(time.localtime()))
1533134061.0
1533134062.0
1533134063.0
1533134064.0
1533134065.0
1533134066.0
1533134067.0
1533134068.0
1533134069.0
1533134070.0
# 结构化时间与字符串格式的转换
# 时间的字符串格式化:http://devdocs.io/python~3.6/library/time#time.strftime
# time.strftime?
‘‘‘
%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%H  Hour (24-hour clock) as a decimal number [00,23].
%M  Minute as a decimal number [00,59].
%S  Second as a decimal number [00,61].
%z  Time zone offset from UTC.
%a  Locale‘s abbreviated weekday name.
%A  Locale‘s full weekday name.
%b  Locale‘s abbreviated month name.
%B  Locale‘s full month name.
%c  Locale‘s appropriate date and time representation.
%I  Hour (12-hour clock) as a decimal number [01,12].
%p  Locale‘s equivalent of either AM or PM.
‘‘‘
"\n%Y  Year with century as a decimal number.\n%m  Month as a decimal number [01,12].\n%d  Day of the month as a decimal number [01,31].\n%H  Hour (24-hour clock) as a decimal number [00,23].\n%M  Minute as a decimal number [00,59].\n%S  Second as a decimal number [00,61].\n%z  Time zone offset from UTC.\n%a  Locale‘s abbreviated weekday name.\n%A  Locale‘s full weekday name.\n%b  Locale‘s abbreviated month name.\n%B  Locale‘s full month name.\n%c  Locale‘s appropriate date and time representation.\n%I  Hour (12-hour clock) as a decimal number [01,12].\n%p  Locale‘s equivalent of either AM or PM.\n"
# 结构化时间与字符串格式的转换
# 时间的字符串格式化
time.strftime("%Y-%m-%d %X", time.localtime())
‘2018-08-01 22:40:28‘
for i in range(5):
    time.sleep(1)
    print(time.strftime("%Y-%m-%d %X", time.localtime()))
2018-08-01 22:41:27
2018-08-01 22:41:28
2018-08-01 22:41:29
2018-08-01 22:41:30
2018-08-01 22:41:31
time.strptime("2018-08-01 22:41:31", "%Y-%m-%d %X")  #与上面的逆向转换
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)
for i in range(5):
    time.sleep(1)
    print(time.strptime("2018-08-01 22:41:31", "%Y-%m-%d %X"))
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=22, tm_min=41, tm_sec=31, tm_wday=2, tm_yday=213, tm_isdst=-1)

时间模块

原文:http://blog.51cto.com/13118411/2153374

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!