Python 时间模块
在本文中,我们将详细探讨时间模块。我们将通过示例学习使用时间模块中定义的不同时间相关函数。
Python 有一个名为 time
的模块 处理与时间相关的任务。要使用模块中定义的函数,我们需要先导入模块。方法如下:
导入时间
以下是常用的时间相关函数。
Python time.time()
time()
函数返回自纪元以来经过的秒数。
对于 Unix 系统,1970 年 1 月 1 日,00:00:00
在 UTC 是纪元(时间开始的点)。
import timeseconds =time.time()print("Seconds since epoch =", seconds)
Python time.ctime()
time.ctime()
函数以纪元以来经过的秒数作为参数,并返回一个表示本地时间的字符串。
import time# 从 epochseconds 开始经过的秒数 =1545925769.9618232local_time =time.ctime(seconds)print("Local time:", local_time)
如果运行该程序,输出将类似于:
当地时间:2018 年 12 月 27 日星期四 15:49:29
Python time.sleep()
sleep()
函数在给定的秒数内暂停(延迟)当前线程的执行。
import timeprint("这是立即打印的。")time.sleep(2.4)print("这是2.4秒后打印的。")
要了解更多信息,请访问:Python sleep()。
在我们讨论其他与时间相关的函数之前,让我们先探索一下 time.struct_time
课程简介。
time.struct_time 类
时间
中的几个函数 gmtime()
等模块 , asctime()
等要么采取 time.struct_time
对象作为参数或返回它。
下面是一个 time.struct_time
的例子 对象。
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=6, tm_min=35, tm_sec=17, tm_wday=3, tm_yday=361, tm_isdst=0)
索引 | 属性 | 价值观 |
---|---|---|
0 | tm_year | 0000, ...., 2018, ..., 9999 |
1 | tm_mon | 1, 2, ..., 12 |
2 | tm_mday | 1, 2, ..., 31 |
3 | tm_hour | 0, 1, ..., 23 |
4 | tm_min | 0, 1, ..., 59 |
5 | tm_sec | 0, 1, ..., 61 |
6 | tm_wday | 0, 1, ..., 6;星期一是 0 |
7 | tm_yday | 1, 2, ..., 366 |
8 | tm_isdst | 0、1 或 -1 |
time.struct_time
的值(元素) 对象可以使用索引和属性访问。
Python time.localtime()
localtime()
函数将自 epoch 以来经过的秒数作为参数并返回 struct_time
当地时间 .
import timeresult =time.localtime(1545925769)print("result:", result)print("\nyear:", result.tm_year)print("tm_hour:", result.tm_hour)
当你运行程序时,输出将类似于:
result:time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_hour=15):isd 2018tm_hour:15
如果没有参数或 None
传递给 localtime()
, time()
返回的值 使用。
Python time.gmtime()
gmtime()
函数将自 epoch 以来经过的秒数作为参数并返回 struct_time
在 UTC .
import timeresult =time.gmtime(1545925769)print("result:", result)print("\nyear:", result.tm_year)print("tm_hour:", result.tm_hour)
当你运行程序时,输出将是:
result =time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_年=0) 2018tm_hour =8
如果没有参数或 None
传递给 gmtime()
, time()
返回的值 使用。
Python time.mktime()
mktime()
函数需要 struct_time
(或包含与 struct_time
对应的 9 个元素的元组 ) 作为参数并返回自本地时间纪元以来经过的秒数。基本上,它是 localtime()
的反函数 .
import timet =(2018, 12, 28, 8, 44, 4, 4, 362, 0)local_time =time.mktime(t)print("Local time:", local_time)
下面的例子展示了 mktime()
和 localtime()
是相关的。
import timeseconds =1545925769#returns struct_timet =time.localtime(seconds)print("t1:", t)#returns from struct_times =time.mktime(t)print("\s:", seconds )
当你运行程序时,输出将类似于:
t1:time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, =tm_isd 1545925769.0
Python time.asctime()
asctime()
函数需要 struct_time
(或包含与 struct_time
对应的 9 个元素的元组 ) 作为参数并返回一个表示它的字符串。举个例子:
import timet =(2018, 12, 28, 8, 44, 4, 4, 362, 0)result =time.asctime(t)print("Result:", result)
当你运行程序时,输出将是:
结果:2018 年 12 月 28 日星期五 08:44:04
Python time.strftime()
strftime()
函数需要 struct_time
(或与之对应的元组)作为参数,并根据所使用的格式代码返回一个表示它的字符串。例如,
import timenamed_tuple =time.localtime() # get struct_timetime_string =time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)print(time_string)代码>
当你运行程序时,输出将类似于:
12/28/2018, 09:47:41
在这里,%Y
, %m
, %d
, %H
等是格式代码。
%Y
- 年份 [0001,..., 2018, 2019,..., 9999]%m
- 月 [01, 02, ..., 11, 12]%d
- 第 [01, 02, ..., 30, 31] 天%H
- 小时 [00, 01, ..., 22, 23%M
- 分钟 [00, 01, ..., 58, 59]%S
- 第二个 [00, 01, ..., 58, 61]
要了解更多信息,请访问:time.strftime()。
Python time.strptime()
strptime()
函数解析表示时间的字符串并返回struct_time
.
import timetime_string ="21 June, 2018"result =time.strptime(time_string, "%d %B, %Y")print(result)
当你运行程序时,输出将是:
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1)>
Python