在本文中,我们将详细探讨时间模块。在示例的帮助下,我们将学习使用在时间模块中定义的与时间相关的不同功能。
Python有一个命名time
为处理与时间有关的任务的模块。要使用模块中定义的功能,我们需要首先导入模块。就是这样:
import time
这是常用的与时间相关的功能。
Python time.time()
该time()
函数返回自纪元以来经过的秒数。
对于Unix系统,January 1, 1970, 00:00:00
在UTC是历元(其中,时间开始点)。
import time
seconds = time.time()
print("Seconds since epoch =", seconds)
Python time.ctime()
该time.ctime()
函数将自epoch以来的秒数作为参数传递,并返回表示本地时间的字符串。
import time
# seconds passed since epoch
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)
如果运行该程序,则输出将类似于:
Local time: Thu Dec 27 15:49:29 2018
Python time.sleep()
该sleep()
函数在给定的秒数内暂停(延迟)当前线程的执行。
import time
print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")
要了解更多信息,请访问:Python sleep()。
在讨论其他与时间相关的功能之前,让我们time.struct_time
简要地探讨一下类。
time.struct_time类别
time
模块中的几个函数,例如gmtime()
,asctime()
等等,都可以将time.struct_time
object作为参数或将其返回。
这是一个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 time
result = 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_isdst=0) year: 2018 tm_hour: 15
如果没有参数或None
传递给localtime()
,time()
则使用的返回值。
Python time.gmtime()
该gmtime()
函数将自epoch以来经过的秒数作为参数,并struct_time
以UTC返回。
import time
result = 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_isdst=0) year = 2018 tm_hour = 8
如果没有参数或None
传递给gmtime()
,time()
则使用的返回值。
Python time.mktime()
该mktime()
函数以struct_time
(或包含9个与相对应的9个元素的元组struct_time
)作为参数,并返回自当地时间的纪元以来经过的秒数。基本上,它是的反函数localtime()
。
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
local_time = time.mktime(t)
print("Local time:", local_time)
下面的示例显示mktime()
和localtime()
的关系。
import time
seconds = 1545925769
# returns struct_time
t = time.localtime(seconds)
print("t1: ", t)
# returns seconds from struct_time
s = 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_isdst = 0) s:1545925769.0
Python time.asctime()
该asctime()
函数以struct_time
(或包含9个与相对应的元素的元组struct_time
)作为参数,并返回表示它的字符串。这是一个例子:
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("Result:", result)
运行该程序时,输出为:
12/28/2018, 09:47:41
Python time.strftime()
该strftime()
函数以struct_time
(或与其对应的元组)作为参数,并根据所使用的格式代码返回表示它的字符串。例如,
import time
named_tuple = time.localtime() # get struct_time
time_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 time
time_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)