当前位置: 首页 > 图灵资讯 > 行业资讯> Python之time模块详解

Python之time模块详解

发布时间:2025-10-27 16:12:33

time模块在python3中的用法及说明

在python中,导入time模块使用的命令是

importtime

可使用以下命令查看time模块内置的可用方法:

dir(time)

查看time模块中每个内置方法的说明可以使用以下命令:

help(time.time_method)

例如,time模块下有一个time.time方法,现在我想查看该方法的官方文件,您可以使用此命令:

help(time.time)

时间表现形式:

在python中,通常有三种方式来表示时间:时间戳、元组(结构化时间,struct_time),格式化的时间字符串。

(1)时间戳(timestamp):一般来说,时间戳是指1970年1月1日的00:00:00开始按秒计算偏移量,其值为float类型

(2)格式化时间字符串(Format String):‘2017-06-20’

(3)结构化时间(struct_time):struct_time元组有9个元素:(年、月、日、时、分、秒、一年中的第几周、一年中的第几天等。)

常用的time模块内置方法:

asctime

接受时间元组并返回可读形式"Tue May 30 17:17:30 2017"24个字符串(2017年5月30日星期二17:17:30秒)

Convertatimetupletoastring,e.g.'SatJun0616:26:111998'.
Whenthetimetupleisnotpresent,currenttimeasreturnedbylocaltime()
isused.
>>>time.asctime()
'ThuJun21919:27:192017'

ctime

相当于asctime(localtime(secs)),未给参数相当于asctime()

ctime(seconds)->string
ConvertatimeinsecondssincetheEpochtoastringinlocaltime.
Thisisequivalenttoasctime(localtime(seconds)).Whenthetimetupleis
notpresent,currenttimeasreturnedbylocaltime()isused.
>>>time.ctime()
'ThuJun21919:34:352017'

gmtime

接收时间辍学(1970年后的浮点秒数),返回格林威治天文时间的时间元组t(t.tm_isdst始终为0。

gmtime([seconds])->(tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
ConvertsecondssincetheEpochtoatimetupleexpressingUTC(a.k.a.
GMT).When'seconds'isnotpassedin,convertthecurrenttimeinstead.
Iftheplatformsupportsthetm_gmtoffandtm_zone,theyareavailableas
attributesonly.
>>>time.gmtime()
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=11,tm_min=35,tm_sec=12,tm_wday=3,
tm_yday=173,tm_isdst=0)

localtime

接收时间退出(1970年后经过的浮点秒数),返回当地时间下的时间元组t(t.tm_isdst可取为0或1,取决于当地是否是夏令时)

localtime([seconds])->(tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
ConvertsecondssincetheEpochtoatimetupleexpressinglocaltime.
When'seconds'isnotpassedin,convertthecurrenttimeinstead.
>>>time.localtime()
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=19,tm_min=35,tm_sec=35,
tm_wday=3,tm_yday=173,tm_isdst=0)

mktime

接受时间元组并返回时间辍学(1970年后的浮点秒)

mktime(tuple)->floatingpointnumber
ConvertatimetupleinlocaltimetosecondssincetheEpoch.
Notethatmktime(gmtime(0))willnotgenerallyreturnzeroformost
timezones;insteadthereturnedvaluewilleitherbeequaltothat
ofthetimezoneoraltzoneattributesonthetimemodule.
>>>time.mktime(time.localtime())
1498131370.0

sleep

推迟调用线程的运行,secs的单位是秒

Delayexecutionforagivennumberofseconds.Theargumentmaybe
afloatingpointnumberforsubsecondprecision.

相关推荐:Python视频教程

strftime

代表时间的元组或struct_time(如time.localtime()和time.gmtime()返回)转换为格式化的时间字符串.如果t未指定,将传入timee.localtime()如果元组中任命一个元素越界,ValueEror异常将被抛出

strftime(format[,tuple])->string
Convertatimetupletoastringaccordingtoaformatspecification.
Seethelibraryreferencemanualforformattingcodes.Whenthetimetuple
isnotpresent,currenttimeasreturnedbylocaltime()isused.
Commonlyusedformatcodes:

%YYearwithcenturyasadecimalnumber.===>完整的年份
%mMonthasadecimalnumber[01,12].===>月份(01-12)
%dDayofthemonthasadecimalnumber[01,31].===>一个月中的第几天(01-31)
%HHour(24-hourclock)asadecimalnumber[00,23].===>一天中的几个小时(24小时制,00-23)
%MMinuteasadecimalnumber[00,59].===>分钟数(00-59)
%SSecondasadecimalnumber[00,61].===>秒(01-61)
%zTimezoneoffsetfromUTC.===>用+HHMM或-HHMM表示距离格林威治的时区偏移(H表示十进制的小时数,M代表十进制的分钟数)
%aLocale'sabbreviatedweekdayname.===>本地(local)简化周名
%ALocale'sfullweekdayname.===>本地完整星期名称
%bLocale'sabbreviatedmonthname.===>本地简化月名
%BLocale'sfullmonthname.===>本地完整月名
%cLocale'sappropriatedateandtimerepresentation.===>本地相应的日期和时间表示
%IHour(12-hourclock)asadecimalnumber[01,12].===>一天中的几个小时(12小时制,01-12)
%pLocale'sequivalentofeitherAMorPM.===>本地am或pm的相应符合
>>>time.strftime("%Y-%m-%d")
'2017-06-22'
>>>time.strftime("%Y-%m-%d%H-%H-%S")
'2017-06-2219-19-28'

strptim

将格式化的时间字符串转化为struct_time,事实上,它和strftie()是逆操作

strptime(string,format)->struct_time
Parseastringtoatimetupleaccordingtoaformatspecification.
Seethelibraryreferencemanualforformattingcodes(sameas
strftime()).
>>>time.strptime("2017-06-21","%Y-%m-%d")
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=21,tm_hour=0,tm_min=0,tm_sec=0,tm_wday=2,tm_yday=172,tm_isdst=-1)
>>>time.strptime("2017-06-2112-34-45","%Y-%m-%d%H-%M-%S")
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=21,tm_hour=12,tm_min=34,tm_sec=45,tm_wday=2,tm_yday=172,tm_isdst=-1)
struct_time

将一个时间转化为结构化时间

Thetimevalueasreturnedbygmtime(),localtime(),andstrptime(),and
acceptedbyasctime(),mktime()andstrftime().Maybeconsideredasa
sequenceoff9integers.
>>>time.struct_time(time.localtime())
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=19,tm_min=42,tm_sec=7,
tm_wday=3,tm_yday=173,tm_isdst=0)
time

返回当前时间的时间戳(1970元年后的浮点秒

ReturnthecurrenttimeinsecondssincetheEpoch.
Fractionsofasecondmaybepresentifthesystemclockprovidesthem.
>>>time.time()
1498131760.7711384
>>>time.time()
1498131764.7621822

几种时间形式的转换

1.将时间戳转换为结构化时间,使用time.localtime或time.gmtime命令。

>>>t1=time.time()
>>>print(t1)
1498132526.8227696
>>>t2=time.localtime(t1)
>>>print(t2)
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=19,tm_min=55,tm_sec=26,
tm_wday=3,tm_yday=173,tm_isdst=0)

2.用timee将结构化时间转换为时间戳.mktime命令

>>>t3=time.struct_time(time.localtime())
>>>print(t3)
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=19,tm_min=58,tm_sec=29,tm_wday=3,tm_yday=173,tm_isdst=0)
>>>t4=time.mktime(t3)
>>>print(t4)
1498132709.0

3.将结构化时间转换为时间字符串,使用time.strftime命令

>>>t1=time.localtime()
>>>print(t1)
time.struct_time(tm_year=2017,tm_mon=6,tm_mday=22,tm_hour=20,tm_min=0,tm_sec=37,tm_wday=3,
tm_yday=173,tm_isdst=0)
>>>t2=time.strftime("%Y-%m-%d",t1)
>>>print(t2)
2017-06-22

4.将字符串时间转换为结构化时间,使用time.strptime命令

>>>t1="2017-05-2008:08:10"
>>>print(t1)
2017-05-2008:08:10
>>>t2=time.strptime(t1,"%Y-%m-%d%H:%M:%S")
>>>print(t2)
time.struct_time(tm_year=2017,tm_mon=5,tm_mday=20,tm_hour=8,tm_min=8,tm_sec=10,
tm_wday=5,tm_yday=140,tm_isdst=-1)

例子:

如果我有一个时间字符串,然后想在这个时间后3天得到字符串,我可以使用以下命令:

importtime
time1="2017-05-20"
#将时间字符串转换为时间戳,然后添加3天的秒数
time2=time.mktime(time.strptime(time1,"%Y-%m-%d"))+3*24*3600
#将转换后的时间戳转换为时间字符串
time3=time.strftime("%Y-%m-%d",time.localtime(time2)
print(time3)

相关文章

Python之time模块详解

Python之time模块详解

2025-10-27
Python之random模块详解

Python之random模块详解

2025-10-27
Python导入模块,Python import用法(超级详细)

Python导入模块,Python import用法(超级详细)

2025-10-27
Python类调用实例方法

Python类调用实例方法

2025-10-27
Python之初识类与对象

Python之初识类与对象

2025-10-27
Python面向对象编程之组合关系

Python面向对象编程之组合关系

2025-10-27