表格梳理解析python内置时间模块看完就懂
无参数函数
先解释一下时间戳,所谓时间戳,即自1970年1月1日00:00:00所经历的秒数,然后就可以理解下面的函数了。下面代码默认
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 | 
上面五组函数中,只有time.time()的值具有绝对的意义,其他值都只具有相对的意义。
通过get_clock_info函数可以查看这些时钟的特性,其输入输出分别为
| 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 | 
其中,
- 如果时钟可以自动更改或由系统管理员手动更改,则adjustable为True,否则为False。
 - implementation表示用于获取时钟值的基础C函数的名称。
 - 如果时钟不能倒退,则monotonic为 True,否则为 False 。
 - resolution表示以秒为单位的时钟分辨率。
 
接下来可以测试一下这些时钟的特性。
>>> 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
>>>
可清晰地看到,在调用test的间隔中,thread_time和process_time并未发生变化,即二者不计算线程或者进程休眠时的时间。
一般在time模块中,最常用的两个函数分别是time.time()和time.sleep(),前者用于获取时间戳,从而统计程序运行时长;后者则可以暂停线程。
可以通过time.thread_time()来检测sleep函数的功能
>>> 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)
时区概念
接下来需要介绍一些有关时间的概念
GMT:即格林威治标准时间。
UTC:世界协调时间,比格林威治更精确。
DST:D即Daylight,表示夏令时。
CST:美国、澳大利亚、中国、古巴的标准时间。
知道这些时区的概念之后,就能理解time中的常量:
| 常量 | altzone | daylight | tzname | timezone | 
|---|---|---|---|---|
| 时区偏移 | 如未定义非DST时区,则为0 | 时区名称 | 本地时区偏移 | 
struct_time
为了更好地表示时间,time中封装了struct_time类,其成员包括
| 索引 | 属性 | 值 | 含义 | 
|---|---|---|---|
| 0 | tm_year | 正整数 | 年 | 
| 1 | tm_mon | range [1, 12] | 月 | 
| 2 | tm_mday | range [1, 31] | 月中的日期 | 
| 3 | tm_hour | range [0, 23] | 时 | 
| 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 | 时区名称的缩写 | |
| - | tm_gmtoff | 以秒为单位的UTC以东偏离 | 
在了解struct_time这一数据结构之后,就能读懂下列函数。
| 单参函数 | |
|---|---|
| gmtime(secs) | 将时间戳转化为UTC时间[struct_time格式] | 
| localtime(secs) | 将戳转化为本地时间[struct_time格式] | 
| ctime(secs) | 将时间戳转化为UTC时间字符串 | 
| asctime(secs) | 将时间结构体转化为本地时间字符串 | 
| mktime | localtime的反函数,将struct_time转为秒数 | 
time.strftime(format[, t])
可以将struct_time通过匹配符进行格式化输出,其转换格式为
| 名称 | 含意 | 名称 | 含意 | 
|---|---|---|---|
| %a | 星期的缩写 | %A | 星期的名称 | 
| %b | 月份缩写 | %B | 月份名称 | 
| %c | 适当的日期和时间表示 | ||
| %d | 月中日,范围[01,31] | %j | 年中日,范围[001,366] | 
| %H | 小时,范围[00,23] | %I | 小时,范围[01,12] | 
| %M | 分钟,范围[00,59] | %S | 秒,范围[00,61] | 
| %p | AM 或 PM | ||
| %m | 月份,范围[01,12] | ||
| %U | 年中周数,范围[00,53] 周日作为第一天  | 
%W | 同左,周一作为第一天 | 
| %w | 周中日,范围[0(星期日),6] | ||
| %x | 适当的日期表示 | %X | 适当的时间表示 | 
| %y | 无世纪年份,范围[00,99] | %Y | 带世纪的年份 | 
| %z | 时区偏移 | ||
| %Z | 时区名称 | ||
| %% | 字面的 ‘%' 字符。 | 
strptime()为其反函数。
例如
>>> 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内置时间模块看完就懂的详细内容,更多关于python内置时间模块的资料请关注本站其它相关文章!
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。
                    关注官方微信