時間模塊

常用的時間模塊

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章