python语言中对于时间函数time的使用说明
目录
3. time.localtime ([timestamp]) 时间戳→本地结构化时间
4. time.gmtime ([timestamp]) 时间戳→UTC 世界标准时间
5. time.mktime (struct_time) 结构化时间→时间戳
6. time.asctime ([struct_time]) 结构化时间→简易英文时间字符串
7. time.ctime ([timestamp]) 时间戳→简易英文时间字符串
8. time.strftime (format, struct_time) 格式化时间(最常用)
9. time.strptime (time_str, format) 时间字符串→结构化时间
Python time 模块完整使用说明
一、导入模块
python运行
import time
time 模块底层调用操作系统时间接口,主要处理时间戳、结构化时间、格式化时间、延时四类功能。
二、核心概念
- 时间戳(timestamp) 浮点数,单位秒,从
1970-01-01 00:00:00 UTC(纪元时间)到当前的总秒数。 - struct_time 结构化时间 元组格式
(年,月,日,时,分,秒,星期,年中天数,夏令时)索引对应: | 下标 | 含义 | |----|----| |0|4 位年份 | |1 | 月份 1~12| |2 | 日期 1~31| |3 | 小时 0~23| |4 | 分钟 0~59| |5 | 秒 0~61(闰秒)| |6 | 星期 0 = 周一,6 = 周日 | |7 | 年内第几天 1~366| |8 | 夏令时标记 -1/0/1|
三、常用函数详解
1. time.time () 获取当前时间戳
python运行
t = time.time()
print(t) # 输出 17xxxxxx.123456
用途:计算代码运行耗时、存储时间标记。
2. time.sleep (sec) 延时等待
python运行
time.sleep(2) # 暂停2秒,支持浮点数 time.sleep(0.5) 延时0.5秒
3. time.localtime ([timestamp]) 时间戳→本地结构化时间
不传参数默认转换当前时间戳
python运行
# 当前本地时间
local = time.localtime()
print(local)
# 指定时间戳转本地时间
local2 = time.localtime(1700000000)
4. time.gmtime ([timestamp]) 时间戳→UTC 世界标准时间
python运行
utc = time.gmtime()
5. time.mktime (struct_time) 结构化时间→时间戳
和 localtime 互逆
python运行
st = time.localtime()
ts = time.mktime(st)
print(ts)
6. time.asctime ([struct_time]) 结构化时间→简易英文时间字符串
python运行
s = time.asctime(time.localtime())
print(s) # Tue Jul 14 15:30:20 2026
7. time.ctime ([timestamp]) 时间戳→简易英文时间字符串
等价于 asctime(localtime(ts))
python运行
print(time.ctime())
print(time.ctime(1700000000))
8. time.strftime (format, struct_time) 格式化时间(最常用)
把 struct_time 转为自定义格式字符串
常用格式化符号
表格
| 符号 | 说明 | 示例 |
|---|---|---|
| %Y | 4 位完整年份 | 2026 |
| %m | 两位月份 01~12 | 07 |
| %d | 两位日期 01~31 | 14 |
| %H | 24 小时制 00~23 | 15 |
| %M | 分钟 00~59 | 25 |
| %S | 秒 00~59 | 30 |
| %w | 星期 0 = 周日,1 = 周一 | 2 |
| %A | 完整星期英文 | Tuesday |
| %B | 完整月份英文 | July |
| %X | 时分秒本地格式 | 15:25:30 |
| %x | 年月日本地格式 | 07/14/26 |
示例:
python运行
# 当前时间格式化
now = time.localtime()
str_t = time.strftime("%Y-%m-%d %H:%M:%S", now)
print(str_t) # 2026-07-14 15:30:00
9. time.strptime (time_str, format) 时间字符串→结构化时间
strftime 反向操作,必须严格匹配格式
python运行
time_str = "2026-07-14 15:30:00"
st = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
print(st)
# 再转时间戳
ts = time.mktime(st)
四、时间互转完整流程示例
案例 1:时间戳 ↔ 格式化字符串
python运行
import time
# 1. 时间戳 → 格式化字符串
ts = time.time()
st = time.localtime(ts)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", st)
print(date_str)
# 2. 格式化字符串 → 时间戳
text = "2026-07-14 10:20:00"
struct = time.strptime(text, "%Y-%m-%d %H:%M:%S")
new_ts = time.mktime(struct)
print(new_ts)
案例 2:计算代码运行耗时
python运行
import time
start = time.time()
# 测试代码
for i in range(1000000):
pass
end = time.time()
cost = end - start
print(f"运行耗时:{cost:.4f} 秒")
五、高精度计时(time.perf_counter ())
time.time() 受系统时间修改影响,高精度计时推荐:
python运行
start = time.perf_counter()
time.sleep(0.3)
end = time.perf_counter()
print(end - start)
适用:算法性能测试、短耗时计算。
六、补充函数
time.process_time():仅统计CPU 占用时间,sleep 休眠不计入time.timezone:本地时区与 UTC 时差(秒)time.tzname:本地时区名称二元组(标准时间 / 夏令时)
七、常见报错与注意事项
strptime格式不匹配 → ValueError 字符串和格式符号必须一一对应,少空格、分隔符不一致都会报错。- 时间戳超出范围 Windows 系统纪元时间限制,过小 / 过大时间戳会报错。
- 本地时间 vs UTC 存储服务器日志建议统一用 UTC(gmtime),避免时区混乱。
- 浮点数精度 time.time () 小数部分是微秒级,如需毫秒可
int(time.time() * 1000)。
八、毫秒、时间戳快捷写法
python运行
# 当前毫秒时间戳
ms = int(time.time() * 1000)
print(ms)
# 当前秒级时间戳
sec = int(time.time())
print(sec)
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐

所有评论(0)