目录

无参数函数

先解释一下时间戳,所谓时间戳,即自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_timeprocess_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内置时间模块的资料请关注www.887551.com其它相关文章!