python3-time模塊


補充知識

夏令時

  夏令時,是一種為節約能源而人為規定地方時間的制度。也叫夏時制,夏時令(Daylight Saving Time:DST),又稱“日光節約時制”和“夏令時間”,在這一制度實行期間所采用的統一時間稱為“夏令時間”。一般在天亮早的夏季人為將時間調快一小時,可以使人早起早睡,減少照明量,以充分利用光照資源,從而節約照明用電。各個采納夏時制的國家具體規定不同。目前全世界有近110個國家每年要實行夏令時。我國1992年起,夏令時暫停實行。

UTC

UTC(Coordinated Universal Time,世界協調時)也叫格林威治天文時間,是世界標准時間。在中國為 UTC+8 。

1、三種時間表示形式

在python3中時間有三種表示方式:時間戳、元組(struct_time)、格式化時間字符串:

1.1 時間戳(timestamp):

  時間戳是指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總秒數。通俗的講, 時間戳是一份能夠表示一份數據在一個特定時間點已經存在的完整的可驗證的數據。

通過time.time()獲取當前時間戳:

1 >>> import time
2 >>> ticks=time.time()
3 >>> ticks
4 1566286317.9938319
5 >>> ticks/3600/24/365+1970
6 2019.666613330601

1.2 時間元組(struct_time):

struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天等)

時間元組(struct_time)
屬性(Attribute)
值(value)
tm_year(年) 比如2019
tm_mon(月) 1-12
tm_mday(日) 1-31
tm_hour(時) 0-23
tm_min(分) 0-59
tm_sec(秒) 0-61(注1:60 代表閏秒,61 是基於歷史原因保留。)
tm_wday(weekday) 0-6(0表示周一)
tm_yday(一年中的第幾天) 1-366
tm_isdst(是否是夏令時) 1(夏令時),0(非夏令時),-1(未知,默認)

 

 

 

 

 

 

 

 

 

 

 

 

可以使用 time.localtime() 獲取當前時間元組,元組用於裝載當前的時間信息,

1 >>> time.localtime()
2 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=24, tm_wday=1, tm_yday=232, tm_isdst=0)

 1.3 格式化時間

 對於時間元組的格式化,可以使用 time.asctime() 或 time.strptime() 函數獲取時間元組的格式化輸出;

python 中的時間格式化符號如下:
%Y  四位數的年份表示(000-9999) %y 兩位數的年份表示(00-99)
%m 月份(01-12) %d 月內中的一天(0-31)
%H 24小時制小時數(0-23) %I 12小時制小時數(01-12)
%M 分鍾數(00-59) %S 秒(00-59)
%p 本地化的 AM,PM %w 一周中的第幾天(0-6,0為周日)
%a 本地簡化星期名稱,如:Mon %A 本地完整星期名稱,如:Monday
%b 本地簡化的月份名稱,如:Jan %B 本地完整的月份名稱,如:January
%j 年內的一天(001-366) %U 一年中的星期數(00-53),周日為起始
%c 本地相應的日期表示和時間表示;
等同於:%a %b %d %H:%M%S %Y
如:Thu Jan 25 01:22:04 2018
%Z 當前時區的名稱
%x 本地相應的日期表示;
等同於:%d/%m/%y
如:01/25/18
%X 本地相應的時間表示;
等同於:%H:%M%S
如:01:32:46

 

 

 

 

 

 

 

 

 

 

 

1 # 快速格式化時間輸出
2 >>> time.asctime(time.localtime())
3 'Tue Aug 20 16:08:57 2019'
4 # 時間對象轉化為格式化字符串
5 >>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
6 '2019-08-20 16:09:40'

 

2、time模塊常用方法

線程相關

(1)time.sleep(sec)  推遲線程的運行時間,通過參數sec指定秒數,表示線程掛起時間

1 import time
2 print('start:', time.localtime())
3 time.sleep(5)
4 print('stop:', time.localtime())
5 輸出:
6 start: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=16, tm_min=19, tm_sec=10, tm_wday=1, tm_yday=232, tm_isdst=0)
7 stop: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=16, tm_min=19, tm_sec=15, tm_wday=1, tm_yday=232, tm_isdst=0)

 時間操作相關

(2)time.time()  返回當前時間的時間戳(1970紀元后經過的浮點秒數)。

(3)*time.clock() 用以浮點數計算的秒數返回當前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。(time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8)

不建議使用

(4) *time altzone() 函數返回格林威治西部的夏令時地區的偏移秒數。如果該地區在格林威治東部會返回負值(如西歐,包括英國)。對夏令時啟用地區才能使用。

(5)time.gmtime([secs]) 函數將一個時間戳轉換為UTC時區(0時區)的struct_time,可選的參數sec表示從1970-1-1以來的秒數。其默認值為time.time(),函數返回time.struct_time類型的對象。(struct_time是在time模塊中定義的表示時間的對象)。我國時間是東8區,比0時區時間早了8個小時。

1 import time
2 print(time.localtime())# 本地時區時間
3 print(time.gmtime())# UTC時區(0時區)時間
4 輸出>>>
5 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=16, tm_min=39, tm_sec=5, tm_wday=1, tm_yday=232, tm_isdst=0)
6 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=8, tm_min=39, tm_sec=5, tm_wday=1, tm_yday=232, tm_isdst=0)

 

 (6)time.asctime([t])    函數接受時間元組並返回一個可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時07分14秒)的24個字符的字符串。

1 import time
2 t=time.localtime()
3 print(t)
4 print(time.asctime(t))
5 輸出>>>
6 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=16, tm_min=46, tm_sec=16, tm_wday=1, tm_yday=232, tm_isdst=0)
7 Tue Aug 20 16:46:16 2019

 (7)time.ctime([sec]) 函數把一個時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。 如果參數未給或者為None的時候,將會默認time.time()為參數。它的作用相當於 asctime(localtime(secs))。

1 import time
2 print(time.ctime())
3 >>>
4 Tue Aug 20 16:51:55 2019

 

 (8) time.mktime(tupletime) 接受時間元組並返回時間輟(1970紀元后經過的浮點秒數)。

1 import time
2 t = time.localtime()
3 print(t)
4 print(time.mktime(t))
5 >>>
6 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=16, tm_min=54, tm_sec=53, tm_wday=1, tm_yday=232, tm_isdst=0)
7 1566291293.0

 

(9)  time.strftime(format,[t])     t -- 可選的參數t是一個struct_time對象,默認為此刻時間

接收以時間元組,並返回以可讀字符串表示的當地時間,格式由參數format決定。

1 import time
2 t = time.localtime()
3 print(t)
4 print(time.strftime("%Y-%m-%d %H:%M:%S"))
5 print(time.strftime("%Y-%m-%d %H:%M:%S",t))
6 >>>
7 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=17, tm_min=1, tm_sec=19, tm_wday=1, tm_yday=232, tm_isdst=0)
8 2019-08-20 17:01:19
9 2019-08-20 17:01:19

(10)time.strptime(str,format)  函數根據指定的格式把一個時間字符串解析為時間元組。

1 import time
2 struct_time = time.strptime('2019-8-20 17:07:25', '%Y-%m-%d %H:%M:%S')
3 print(struct_time)
4 >>>
5 time.struct_time(tm_year=2019, tm_mon=8, tm_mday=20, tm_hour=17, tm_min=7, tm_sec=25, tm_wday=1, tm_yday=232, tm_isdst=-1)

(11)time.perf_counter() 返回計時器的精准時間(系統的運行時間),包含整個系統的睡眠時間。由於返回值的基准點是未定義的,所以,只有連續調用的結果之間的差才是有效的。

 1 import time
 2 def test():
 3     time.sleep(2)
 4     print('in the test')
 5 print('start:',time.perf_counter())
 6 test()
 7 print('stop:',time.perf_counter())
 8 >>>
 9 start: 0.023112338
10 in the test
11 stop: 2.022961182

 

(12) time.process_time() 

返回當前進程執行 CPU 的時間總和,不包含睡眠時間。由於返回值的基准點是未定義的,所以,只有連續調用的結果之間的差才是有效的。 

3、三種格式的轉換

time.localtime([secs]) 接收時間輟(1970 紀元年后經過的浮點秒數)並返回當地時間下的時間元組 t(t.tm_isdst 可取 0 或 1,取決於當地當時是不是夏令時)
time.strftime(format[, t])  把一個代表時間的元組或者 struct_time(如由 time.localtime() 和 time.gmtime() 返回)轉化為格式化的時間字符串。如果 t 未指定,將傳入 time.localtime()。如果元組中任何一個元素越界,將會拋出 ValueError 異常。
time.gmtime([secs]) 接收時間輟(1970 紀元年后經過的浮點秒數)並返回格林威治天文時間下的時間元組 t(注:t.tm_isdst 始終為 0)
time.strptime(string[, format]) 把一個格式化時間字符串轉化為 struct_time。實際上它和 strftime() 是逆操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM