亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

精通 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)
索引属性取值范围
0tm_year0000 … 9999
1tm_mon1 … 12
2tm_mday1 … 31
3tm_hour0 … 23
4tm_min0 … 59
5tm_sec0 … 61
6tm_wday0 (周一) … 6 (周日)
7tm_yday1 … 366
8tm_isdst0, 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

常用格式代码包括:

更多格式代码请参阅 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

  1. Python关键字与标识符:掌握保留字与命名规范
  2. 精通Python数据类型:实战指南
  3. 精通Python运算符:全面指南
  4. 精通Python函数参数:位置、关键字与默认参数
  5. Python模块:创建、导入与高级用法
  6. 精通 Python 字典:创建、操作与进阶技巧
  7. Python 正则表达式 (re 模块) 实战指南
  8. Python 获取当前日期和时间:实用指南
  9. Python 轻松获取当前时间与时区数据
  10. 精通 Python time.sleep():最佳实践、多线程与实战案例
  11. Python timeit():精准测量代码执行时间与实战示例
  12. Python time.sleep():为代码添加延迟的完整指南