表格梳理解析python內(nèi)置時(shí)間模塊看完就懂
無(wú)參數(shù)函數(shù)
先解釋一下時(shí)間戳,所謂時(shí)間戳,即自1970年1月1日00:00:00所經(jīng)歷的秒數(shù),然后就可以理解下面的函數(shù)了。下面代碼默認(rèn)
from time import *
implementation | monotonic | adjustable | resolution | |
---|---|---|---|---|
'time' | GetSystemTimeAsFileTime() | False | True | 0.015625 |
'thread_time' | GetThreadTimes() | True | False | 1e-07 |
'process_time' | GetProcessTimes() | True | False | 1e-07 |
'monotonic' | GetTickCount64() | True | False | 0.015625 |
'perf_counter' | QueryPerformanceCounter() | True | False | 1e-07 |
上面五組函數(shù)中,只有time.time()的值具有絕對(duì)的意義,其他值都只具有相對(duì)的意義。
通過(guò)get_clock_info函數(shù)可以查看這些時(shí)鐘的特性,其輸入輸出分別為
implementation | monotonic | adjustable | resolution | |
---|---|---|---|---|
'time' | GetSystemTimeAsFileTime() | False | True | 0.015625 |
'thread_time' | GetThreadTimes() | True | False | 1e-07 |
'process_time' | GetProcessTimes() | True | False | 1e-07 |
'monotonic' | GetTickCount64() | True | False | 0.015625 |
'perf_counter' | QueryPerformanceCounter() | True | False | 1e-07 |
其中,
- 如果時(shí)鐘可以自動(dòng)更改或由系統(tǒng)管理員手動(dòng)更改,則adjustable為T(mén)rue,否則為False。
- implementation表示用于獲取時(shí)鐘值的基礎(chǔ)C函數(shù)的名稱。
- 如果時(shí)鐘不能倒退,則monotonic為 True,否則為 False 。
- resolution表示以秒為單位的時(shí)鐘分辨率。
接下來(lái)可以測(cè)試一下這些時(shí)鐘的特性。
>>> def test(n): ...aTime = time.time() ...aTh = time.thread_time() ...aPr = time.process_time() ...aMo = time.monotonic() ...aPe = time.perf_counter() ...for i in range(int(n)): j = i**2 ...bTime = time.time() ...bTh = time.thread_time() ...bPr = time.process_time() ...bMo = time.monotonic() ...bPe = time.perf_counter() ...aStr = f'aTime={aTime},aTh={aTh},aPr={aPr},aMo={aMo},aPe={aPe}\n' ...bStr = f'bTime={bTime},bTh={bTh},bPr={bPr},bMo={bMo},bPe={bPe}' ...print(aStr+bStr) ... >>> test(1e6) aTime=1634625786.136904,aTh=0.03125,aPr=0.03125,aMo=199082.078,aPe=199085.4751224 bTime=1634625786.340363,bTh=0.234375,bPr=0.234375,bMo=199082.281,bPe=199085.6787309 >>> test(1e6) aTime=1634625789.7817287,aTh=0.234375,aPr=0.234375,aMo=199085.734,aPe=199089.1195357 bTime=1634625789.981198,bTh=0.421875,bPr=0.421875,bMo=199085.921,bPe=199089.3195721 >>> test(1e6) aTime=1634625796.3934195,aTh=0.421875,aPr=0.421875,aMo=199092.343,aPe=199095.731209 bTime=1634625796.5789576,bTh=0.609375,bPr=0.609375,bMo=199092.531,bPe=199095.9172852 >>>
可清晰地看到,在調(diào)用test
的間隔中,thread_time
和process_time
并未發(fā)生變化,即二者不計(jì)算線程或者進(jìn)程休眠時(shí)的時(shí)間。
一般在time
模塊中,最常用的兩個(gè)函數(shù)分別是time.time()
和time.sleep()
,前者用于獲取時(shí)間戳,從而統(tǒng)計(jì)程序運(yùn)行時(shí)長(zhǎng);后者則可以暫停線程。
可以通過(guò)time.thread_time()
來(lái)檢測(cè)sleep
函數(shù)的功能
>>> def test(n): ... aTime = time.time() ... aTh = time.thread_time() ... aPr = time.process_time() ... time.sleep(n) ... bTime = time.time() ... bTh = time.thread_time() ... bPr = time.process_time() ... aStr = f'aTime={aTime},aTh={aTh},aPr={aPr}\n' ... bStr = f'bTime={bTime},bTh={bTh},bPr={bPr}' ... print(aStr+bStr) ... >>> test(1) aTime=1634649370.2819958,aTh=0.640625,aPr=0.640625 bTime=1634649371.2862759,bTh=0.640625,bPr=0.640625 >>> test(1) aTime=1634649372.72013,aTh=0.640625,aPr=0.640625 bTime=1634649373.723695,bTh=0.640625,bPr=0.640625 >>> test(1)
時(shí)區(qū)概念
接下來(lái)需要介紹一些有關(guān)時(shí)間的概念
GMT
:即格林威治標(biāo)準(zhǔn)時(shí)間。
UTC
:世界協(xié)調(diào)時(shí)間,比格林威治更精確。
DST
:D即Daylight,表示夏令時(shí)。
CST
:美國(guó)、澳大利亞、中國(guó)、古巴的標(biāo)準(zhǔn)時(shí)間。
知道這些時(shí)區(qū)的概念之后,就能理解time
中的常量:
常量 | altzone | daylight | tzname | timezone |
---|---|---|---|---|
時(shí)區(qū)偏移 | 如未定義非DST時(shí)區(qū),則為0 | 時(shí)區(qū)名稱 | 本地時(shí)區(qū)偏移 |
struct_time
為了更好地表示時(shí)間,time
中封裝了struct_time
類,其成員包括
索引 | 屬性 | 值 | 含義 |
---|---|---|---|
0 | tm_year | 正整數(shù) | 年 |
1 | tm_mon | range [1, 12] | 月 |
2 | tm_mday | range [1, 31] | 月中的日期 |
3 | tm_hour | range [0, 23] | 時(shí) |
4 | tm_min | range [0, 59] | 分 |
5 | tm_sec | range [0, 61] | 秒 |
6 | tm_wday | range [0, 6],周一為 0 | 星期即 |
7 | tm_yday | range [1, 366] | 在一年中的第幾天 |
8 | tm_isdst | 0, 1 或 -1 | 是否為DST |
- | tm_zone | 時(shí)區(qū)名稱的縮寫(xiě) | |
- | tm_gmtoff | 以秒為單位的UTC以東偏離 |
在了解struct_time這一數(shù)據(jù)結(jié)構(gòu)之后,就能讀懂下列函數(shù)。
單參函數(shù) | |
---|---|
gmtime(secs) | 將時(shí)間戳轉(zhuǎn)化為UTC時(shí)間[struct_time格式] |
localtime(secs) | 將戳轉(zhuǎn)化為本地時(shí)間[struct_time格式] |
ctime(secs) | 將時(shí)間戳轉(zhuǎn)化為UTC時(shí)間字符串 |
asctime(secs) | 將時(shí)間結(jié)構(gòu)體轉(zhuǎn)化為本地時(shí)間字符串 |
mktime | localtime的反函數(shù),將struct_time轉(zhuǎn)為秒數(shù) |
time.strftime(format[, t])
可以將struct_time
通過(guò)匹配符進(jìn)行格式化輸出,其轉(zhuǎn)換格式為
名稱 | 含意 | 名稱 | 含意 |
---|---|---|---|
%a | 星期的縮寫(xiě) | %A | 星期的名稱 |
%b | 月份縮寫(xiě) | %B | 月份名稱 |
%c | 適當(dāng)?shù)娜掌诤蜁r(shí)間表示 | ||
%d | 月中日,范圍[01,31] | %j | 年中日,范圍[001,366] |
%H | 小時(shí),范圍[00,23] | %I | 小時(shí),范圍[01,12] |
%M | 分鐘,范圍[00,59] | %S | 秒,范圍[00,61] |
%p | AM 或 PM | ||
%m | 月份,范圍[01,12] | ||
%U | 年中周數(shù),范圍[00,53] 周日作為第一天 |
%W | 同左,周一作為第一天 |
%w | 周中日,范圍[0(星期日),6] | ||
%x | 適當(dāng)?shù)娜掌诒硎?/td> | %X | 適當(dāng)?shù)臅r(shí)間表示 |
%y | 無(wú)世紀(jì)年份,范圍[00,99] | %Y | 帶世紀(jì)的年份 |
%z | 時(shí)區(qū)偏移 | ||
%Z | 時(shí)區(qū)名稱 | ||
%% | 字面的 ‘%' 字符。 |
strptime()
為其反函數(shù)。
例如
>>> t = time.strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) >>> t 'Tue, 19 Oct 2021 13:46:37 +0000' >>> T = time.strptime(t,"%a, %d %b %Y %H:%M:%S +0000") >>> T time.struct_time(tm_year=2021, tm_mon=10, tm_mday=19, tm_hour=13, tm_min=46, tm_sec=37, tm_wday=1, tm_yday=292, tm_isdst=-1)
以上就是表格梳理解析python內(nèi)置時(shí)間模塊看完就懂的詳細(xì)內(nèi)容,更多關(guān)于python內(nèi)置時(shí)間模塊的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。