精通 Python time 模块:函数、结构体与实战示例
Python time 模块详解
探索 Python 标准 time 模块,学习其核心函数,并通过清晰示例了解实际应用场景。
Python 的 time 模块提供处理时间戳、调度和测量耗时的工具。使用前需先导入模块:
import time
time.time()
time() 函数返回自 Unix 纪元 (1970 年 1 月 1 日 00:00:00 UTC) 以来的秒数。在类 Unix 系统上该纪元值固定;在 Windows 上从同一起点派生。
import time
seconds = time.time()
print("距纪元秒数 =", seconds)
time.ctime()
将 POSIX 时间戳 (距纪元秒数) 转换为本地时区的可读字符串。
import time
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("本地时间:", local_time)
典型输出:
本地时间: Thu Dec 27 15:49:29 2018
time.sleep()
暂停当前线程执行指定秒数。
import time
print("立即打印。")
time.sleep(2.4)
print("2.4 秒后打印。")
更多详情请参阅 Python sleep() 文档。
time.struct_time 类
gmtime() 和 asctime() 等函数返回或接受 time.struct_time 对象,这是一个表示日历时间的 9 元组。
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 … 9999 |
| 1 | tm_mon | 1 … 12 |
| 2 | tm_mday | 1 … 31 |
| 3 | tm_hour | 0 … 23 |
| 4 | tm_min | 0 … 59 |
| 5 | tm_sec | 0 … 61 |
| 6 | tm_wday | 0 (周一) … 6 (周日) |
| 7 | tm_yday | 1 … 366 |
| 8 | tm_isdst | 0, 1 或 -1 |
可通过索引或属性名访问元素。
time.localtime()
将 POSIX 时间戳转换为本地时区的 struct_time。省略参数则使用当前时间。
import time
result = time.localtime(1545925769)
print("result:", result)
print("\n年份:", 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) 年份: 2018 tm_hour: 15
time.gmtime()
与 localtime() 类似,但返回 UTC (GMT) 时间。省略参数默认为当前纪元值。
import time
result = time.gmtime(1545925769)
print("result:", result)
print("\n年份:", 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) 年份: 2018 tm_hour: 8
time.mktime()
将本地 struct_time (或其 9 元组) 转换回 POSIX 时间戳。它实际上是 localtime() 的逆操作。
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
local_time = time.mktime(t)
print("本地时间戳:", local_time)
演示往返转换:
import time
seconds = 1545925769
t = time.localtime(seconds)
print("t1:", t)
s = time.mktime(t)
print("\ns:", s)
典型输出:
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
time.asctime()
将 struct_time (或元组) 格式化为人类可读字符串。
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("结果:", result)
典型输出:
结果: Fri Dec 28 08:44:04 2018
time.strftime()
根据格式代码将 struct_time 格式化为自定义字符串。
import time
named_tuple = time.localtime()
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)
print(time_string)
典型输出:
12/28/2018, 09:47:41
常用格式代码包括:
%Y– 完整年份 (如 2018)%m– 月份 01–12%d– 日期 01–31%H– 小时 00–23%M– 分钟 00–59%S– 秒 00–61 (含闰秒)
更多格式代码请参阅 time.strftime() 文档。
time.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)
Python