Python 中的 time
模块提供了处理时间相关任务的函数。
时间相关任务包括:
- 读取当前时间
- 格式化时间
- 休眠指定的秒数等。
Python time.time() 函数
在 Python 中,time()
函数返回自 epoch(时间开始点)以来经过的秒数。
对于 Unix 系统,epoch 是 UTC 时间 1970 年 1 月 1 日 00:00:00
。
让我们看一个例子,
# import the time module
import time
# get the current time in seconds since the epoch
seconds = time.time()
print("Seconds since epoch =", seconds)
# Output: Seconds since epoch = 1672214933.6804628
在上面的示例中,我们使用了 time.time()
函数来获取自 epoch 以来的当前时间(秒),然后打印了结果。
Python time.ctime() 函数
Python 中的 time.ctime()
函数将自 epoch 以来经过的秒数作为参数,并返回一个表示本地时间的字符串。
import time
# seconds passed since epoch
seconds = 1672215379.5045543
# convert the time in seconds since the epoch to a readable format
local_time = time.ctime(seconds)
print("Local time:", local_time)
输出
Local time: Wed Dec 28 08:16:19 2022
在这里,我们使用了 time.ctime()
函数将自 epoch 以来经过的秒数转换为可读的格式,然后打印了结果。
Python time.sleep() 函数
sleep()
函数暂停(延迟)当前线程的执行,持续给定的秒数。
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
输出
Printed immediately. Printed after 2.4 seconds.
此程序的工作原理如下:
- 打印
"Printed immediately"
time.sleep(2.4)
暂停执行 2.4 秒。- 打印
"Printed after 2.4 seconds"
。
要了解更多关于 sleep()
的信息,请访问Python sleep()。
Python time.localtime() 函数
localtime()
函数将自 epoch 以来经过的秒数作为参数,并返回 struct_time
(一个包含 9 个元素的元组,对应于 struct_time
)的本地时间。
import time
result = time.localtime(1672214933)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
输出
result: time.struct_time(tm_year=2022, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=8, tm_sec=53, tm_wday=2, tm_yday=362, tm_isdst=0) year: 2022 tm_hour: 8
在这里,如果没有参数或 None
传递给 localtime()
,则使用 time()
返回的值。
Python time.gmtime() 函数
gmtime()
函数将自 epoch 以来经过的秒数作为参数,并返回 UTC 时间的 struct_time
。
import time
result = time.gmtime(1672214933)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
输出
result: time.struct_time(tm_year=2022, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=8, tm_sec=53, tm_wday=2, tm_yday=362, tm_isdst=0) year: 2022 tm_hour: 8
在这里,如果没有参数或 None
传递给 gmtime()
,则使用 time()
返回的值。
Python time.mktime() 函数
mktime()
函数将 struct_time
(一个包含 9 个元素的元组,对应于 struct_time
)作为参数,并返回自 epoch 以来在本地时间经过的秒数。
struct_time
具有以下结构:
(year, month, day, hour, minute, second, weekday, day of the year, daylight saving)
让我们看一个例子,
import time
time_tuple = (2022, 12, 28, 8, 44, 4, 4, 362, 0)
# convert time_tuple to seconds since epoch
seconds = time.mktime(time_tuple)
print(seconds)
# Output: 1672217044.0
在这里,我们将 time_tuple
转换为自 epoch 以来经过的秒数。
Python time.asctime() 函数
在 Python 中,asctime()
函数将 struct_time
作为参数,并返回一个表示它的字符串。
与 mktime()
类似,time_tuple
具有以下结构:
(year, month, day, hour, minute, second, weekday, day of the year, daylight saving)
让我们看一个例子,
import time
t = (2022, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("Result:", result)
# Output: Result: Fri Dec 28 08:44:04 2022
在这里,我们可以看到 time.asctime()
将时间元组转换为人类可读的字符串。
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/29/2022, 08:36:22
在这里,%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 = "14 July, 2023"
result = time.strptime(time_string, "%d %B, %Y")
print(result)
输出
time.struct_time(tm_year=2023, tm_mon=7, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=-1)
在这里,strptime()
解析字符串并将其转换为 struct_time
对象。