导图社区 Python time模块
探索Python中的时间管理艺术!通过time模块,我们可以轻松获取当前时间戳、格式化时间字符串,甚至深入解析本地时间的每个细节。
编辑于2025-03-13 20:22:40time模块
时间格式
时间戳
指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
时间元组
(struct_time)
时间元组是另一种表示时间的方式,又称 结构化时间
格式
(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
元素的含义
tm_year
年
tm_mon
月,范围为[1,12]
tm_mday
日,范围为[1,31]
tm_hour
时, 范围为[0,23]
tm_min
分, 范围为[0,59]
tm_sec
秒,范围为[0,59]
tm_wday
一周中的第几天,范围是[0,6],周一为0
tm_yday
一年中的第几天,范围是[1,366]
tm_isdat
-1:
默认值
表示根据当前的日期时间格式来判定
0:
表示正常格式
1:
表示为夏令时格式
时间的概念
GMT:
即格林威治标准时间。
UTC:
世界协调时间,比格林威治更精确。
DST:
D即Daylight,表示夏令时。
CST:
美国、澳大利亚、中国、古巴的标准时间。
时间元组
(struct_time)
时间元组是另一种表示时间的方式,又称 结构化时间
格式
(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
元素的含义
tm_year
年
tm_mon
月,范围为[1,12]
tm_mday
日,范围为[1,31]
tm_hour
时, 范围为[0,23]
tm_min
分, 范围为[0,59]
tm_sec
秒,范围为[0,59]
tm_wday
一周中的第几天,范围是[0,6],周一为0
tm_yday
一年中的第几天,范围是[1,366]
tm_isdat
-1:
默认值
表示根据当前的日期时间格式来判定
0:
表示正常格式
1:
表示为夏令时格式
time模块
操作
获取
获取当前时间戳
秒
time()
语法:
time.time()
返回:
时间戳
<class 'float'>
概要
import time ti = time.time() print(ti) # 1731638390.4439592 print(type(ti)) # <class 'float'>
纳秒
time_ns()
语法:
time.time_ns()
返回:
时间戳
<class 'int'>
概要
import time print(time.time()) # 1731658709.72509 ti_n = time.time_ns() print(ti_n) # 1731658709725090000 print(type(ti_n)) # <class 'int'>
获取相对时间戳
获取当前线程使用CPU时间(不含 sleep()时间)
秒为单位
thread_time()
语法:
time.thread_time()
返回:
时间戳
<class 'float'>
纳秒为单位
thread_time_ns()
语法:
time.thread_time_ns()
返回:
时间戳
<class 'float'>
概要
获取当前进程使用CPU时间(不含 sleep()时间)
秒为单位
process_time()
语法:
time.process_time()
返回:
时间戳
<class 'float'>
纳秒为单位
process_time_ns()
语法:
time.process_time_ns()
返回:
时间戳
<class 'float'>
概要
import time a = 0 start = time.perf_counter() print(start) # 起始时间 time.sleep(1) for i in range(2 ** 23): a = a + 1 end = time.perf_counter() print(end) # 结束时间 print(end - start) # 包含1s睡眠时间 # 1.4019487000005029 a = 0 t1 = time.process_time() time.sleep(1) # 程序睡眠1秒 for i in range(2 ** 23): a = a + 1 t2 = time.process_time() print(t2 - t1) # 不包含1s的睡眠时间 # 0.375
获取程序的执行时间点 (包含 sleep()时间)
秒为单位
perf_counter()
语法:
time.perf_counter()
返回:
时间戳
<class 'float'>
纳秒为单位
perf_counter_ns()
语法:
time.perf_counter_ns()
返回:
时间戳
<class 'float'>
概要
import time start = time.perf_counter() print(start) # 起始时间 # 24155.3895797 time.sleep(2) end = time.perf_counter() print(end) # 结束时间 # 24156.3899156 print(end - start) # 时间间隔 # 1.0003359000002092
获取一个单调时钟的值
秒为单位
monotonic()
语法:
time.monotonic()
返回:
时间戳
<class 'float'>
纳秒为单位
monotonic_ns()
语法:
time.monotonic_ns()
返回:
时间戳
<class 'float'>
概要
获取本地时间元组
localtime()
语法:
time.localtime()
返回:
时间元组
<class 'time.struct_time'>
概要
import time ti = time.localtime() print(ti) # time.struct_time(tm_year=2024, tm_mon=11, tm_mday=15, tm_hour=11, tm_min=23, tm_sec=21, tm_wday=4, tm_yday=320, tm_isdst=0) print(type(ti)) # <class 'time.struct_time'>
获取UTC时间元组
gmtime()
语法:
time.gmtime()
返回:
时间元组
<class 'time.struct_time'>
概要
import time ti = time.gmtime() print(ti) # time.struct_time(tm_year=2024, tm_mon=11, tm_mday=15, tm_hour=3, tm_min=46, tm_sec=0, tm_wday=4, tm_yday=320, tm_isdst=0) print(type(ti)) # <class 'time.struct_time'>
转换
==> 时间戳
时间元组 ==> 时间戳
mktime()
语法:
time.mktime(tuple)
参数:
tuple
指定时间
tuple
struct_time
返回:
时间戳
<class 'float'>
概要
import time ti_t=time.localtime(1) print(ti_t)#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0) ti = time.mktime(ti_t) print(ti) # 1.0 print(type(ti)) # <class 'float'>
==> 时间元组
时间戳 ==> 本地时间元组
localtime()
语法:
time.localtime(seconds)
参数:
seconds
当前时间
None
默认值
指定时间
秒
返回:
+本地时差时间元组
<class 'time.struct_time'>
时间戳 ==> UTC时间元组
gmtime()
语法:
time.gmtime(seconds)
参数:
seconds
本地当前时间戳
None
默认值
指定时间戳
秒
返回:
UTC时间元组(0时区)
<class 'time.struct_time'>
概要
import time ti = time.localtime(1) print(time.gmtime(1)) # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0) print(ti) # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0) print(type(ti)) # <class 'time.struct_time'>
字符串时间 ==> 时间元组 (指定格式)
strptime()
语法:
time.strptime(str, format)
参数:
str
对应format格式字符串
format
字符串时间格式(指定格式)
返回:
时间元组
概要
import time ti = time.strptime('2024-11-15','%Y-%m-%d') print(ti) # time.struct_time(tm_year=2024, tm_mon=11, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=320, tm_isdst=-1) print(type(ti)) # <class 'time.struct_time'>
==> 字符串
时间元组 ==> 字符串时间 (指定格式)
strftime()
语法:
time.strftime(format, tuple)
参数:
format
字符串时间格式(指定格式)
tuple
当前时间
None
默认值
指定时间
tuple
struct_time
返回:
字符串时间(指定格式)
<class 'str'>
概要
import time ti_t = time.localtime() print(ti_t) # time.struct_time(tm_year=2024, tm_mon=11, tm_mday=15, tm_hour=14, tm_min=21, tm_sec=0, tm_wday=4, tm_yday=320, tm_isdst=0) ti = time.strftime('%Y年%m月%d日',ti_t) print(ti) # 2024年11月15日 print(type(ti)) # <class 'str'>
时间元组 ==> 字符串时间 (欧美格式)
asctime()
语法:
time.asctime(tuple)
参数:
tuple
当前时间
None
默认值
指定时间
tuple
struct_time
返回:
字符串时间
<class 'str'>
Fri Nov 15 11:57:21 2024
概要
import time ti_t = time.localtime() print(ti_t) # time.struct_time(tm_year=2024, tm_mon=11, tm_mday=15, tm_hour=14, tm_min=19, tm_sec=10, tm_wday=4, tm_yday=320, tm_isdst=0) ti = time.asctime(ti_t) print(ti) # Fri Nov 15 14:19:22 2024 print(type(ti)) # <class 'str'>
时间戳 ==> 字符串时间 (欧美格式)
ctime()
语法:
time.ctime(seconds)
参数:
seconds
本地当前时间戳
None
默认值
指定时间戳
秒
返回:
字符串时间
<class 'str'>
概要
import time ti_c = time.time() print(ti_c) # 1731643041.487003 ti = time.ctime(ti_c) print(ti) # Fri Nov 15 11:57:21 2024 print(type(ti)) # <class 'str'>
概要
程序控制
暂停
sleep()
语法:
sleep(sec)
参数:
sec
秒数
int
float
概要
import time print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) # 格式化当前时间 time.sleep(1) # 暂停一秒 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
属性
夏令时区
altzone
语法:
altzone
返回:
夏令时区(单位秒)
夏令时差
daylight
语法:
daylight
返回:
0
表示没有定义夏令时 (DST)
非零整数值(单位秒)
夏令时差
时区名称
tzname
语法:
tzname
返回:
时区名称
本地时区
timezone
语法:
timezone
返回:
本地时差(单位秒)
日期时间格式化符号
基本
年份
%y
两位数(00-99)
%Y
四位数(000-9999)
月份
%m
月份(01-12)
日期
%d
月内日期(0-31)
%j
年内日期(001-366)
时间
%H
24小时制(0-23)
%p
AM或PM
%I
12小时制(01-12)
分
%M
分钟数(00-59)
秒
%S
秒(00-59)
微秒
%f
微秒(000000-999999)
时区
%z
时区名称
%Z
当前时区的名称
扩展
完整表示
%c
完整日期时间表示
%a %b %d H:M:S %Y (星期_月份_日期_时间_年份)
%x
完整日期表示
m/d/y
%X
完整时间表示
H:M:S
月份 (英文)
%b
简化月份名称
%B
完整月份名称
星期
%w
星期(0-6),星期天 =0
%a
简化星期名称
%A
完整星期名称
其它
%U
年中星期数(00-53)星期天 =0
%W
年中星期数(00-53)星期一 =0
%%
%号本身