新聞中心

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),石屏企業(yè)網(wǎng)站建設(shè),石屏品牌網(wǎng)站建設(shè),網(wǎng)站定制,石屏網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,石屏網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
python3中time模塊的用法及說(shuō)明
python中,導(dǎo)入time模塊使用的命令是
import time
可以使用以下命令查看time模塊內(nèi)置的能夠使用的方法:
dir(time)
可以使用以下命令查看time模塊中每個(gè)內(nèi)置方法的說(shuō)明:
help(time.time_method)
比如time模塊下有一個(gè)time.time的方法,現(xiàn)在我想查看這個(gè)方法的官方文檔,就可以使用這樣的命令:
help(time.time)
時(shí)間的表示形式:
在python中,通常有三種方式來(lái)表示時(shí)間:時(shí)間戳,元組(結(jié)構(gòu)化時(shí)間,struct_time),格式化的時(shí)間字符串。
(1)時(shí)間戳(timestamp):通常來(lái)說(shuō),時(shí)間戳表示從1970年1月1日00:00:00開(kāi)始按秒計(jì)算的偏移量,它的值是float類型
(2)格式化的時(shí)間字符串(Format String):‘2017-06-20’
(3)結(jié)構(gòu)化時(shí)間(struct_time):struct_time元組共有9個(gè)元素:(年,月,日,時(shí),分,秒,一年中的第幾周,一年中的第幾天等)
time模塊中內(nèi)置的常用的方法:
asctime
接受時(shí)間元組并返回一個(gè)可讀的形式"Tue May 30 17:17:30 2017"(2017年5月30日周二17時(shí)17分30秒)的24個(gè)字符的字符串
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'. When the time tuple is not present, current time as returned by localtime() is used. >>> time.asctime() 'Thu Jun 22 19:27:19 2017'
ctime
作用相當(dāng)于asctime(localtime(secs)),未給參數(shù)相當(dāng)于asctime()
ctime(seconds) -> string Convert a time in seconds since the Epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). When the time tuple is not present, current time as returned by localtime() is used. >>> time.ctime() 'Thu Jun 22 19:34:35 2017'
gmtime
接收時(shí)間輟(1970紀(jì)元年后經(jīng)過(guò)的浮點(diǎn)秒數(shù))并返回格林威治天文時(shí)間下的時(shí)間元組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) Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When 'seconds' is not passed in, convert the current time instead. If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only. >>> 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
接收時(shí)間輟(1970紀(jì)元年后經(jīng)過(guò)的浮點(diǎn)秒數(shù))并返回當(dāng)?shù)貢r(shí)間下的時(shí)間元組t(t.tm_isdst可取為0或1,取決于當(dāng)?shù)禺?dāng)時(shí)是不是夏令時(shí))
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min, tm_sec,tm_wday,tm_yday,tm_isdst) Convert seconds since the Epoch to a time tuple expressing local time. When 'seconds' is not passed in, convert the current time instead. >>> 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
接受時(shí)間元組并返回時(shí)間輟(1970紀(jì)元年后經(jīng)過(guò)的浮點(diǎn)秒數(shù))
mktime(tuple) -> floating point number Convert a time tuple in local time to seconds since the Epoch. Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module. >>> time.mktime(time.localtime()) 1498131370.0
sleep
推遲調(diào)用線程的運(yùn)行,secs的單位是秒
Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.
相關(guān)推薦:《Python視頻教程》
strftime
把一個(gè)代表時(shí)間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉(zhuǎn)化為格式化的時(shí)間字符串.如果t未指定,將傳入time.localtime(),如果元組中任命一個(gè)元素越界,將會(huì)拋出ValueError異常
strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used. Commonly used format codes: %Y Year with century as a decimal number.===>完整的年份 %m Month as a decimal number [01,12].===>月份(01-12) %d Day of the month as a decimal number [01,31].===>一個(gè)月中的第幾天(01-31) %H Hour (24-hour clock) as a decimal number [00,23].===>一天中的第幾個(gè)小時(shí)(24小時(shí)制,00-23) %M Minute as a decimal number [00,59].===>分鐘數(shù)(00-59) %S Second as a decimal number [00,61].===>秒(01-61) %z Time zone offset from UTC.===>用+HHMM或者-HHMM表示距離格林威治的時(shí)區(qū)偏移(H代表十進(jìn)制的小時(shí)數(shù),M代表十進(jìn)制的分鐘數(shù)) %a Locale's abbreviated weekday name.===>本地(local)簡(jiǎn)化星期名稱 %A Locale's full weekday name.===>本地完整星期名稱 %b Locale's abbreviated month name.===>本地簡(jiǎn)化月份名稱 %B Locale's full month name.===>本地完整月份名稱 %c Locale's appropriate date and time representation.===>本地相應(yīng)的日期和時(shí)間表示 %I Hour (12-hour clock) as a decimal number [01,12].===>一天中的第幾個(gè)小時(shí)(12小時(shí)制,01-12) %p Locale's equivalent of either AM or PM.===>本地am或者pm的相應(yīng)符
>>> time.strftime("%Y-%m-%d")
'2017-06-22'
>>> time.strftime("%Y-%m-%d %H-%H-%S")
'2017-06-22 19-19-28'strptim
把一個(gè)格式化時(shí)間字符串轉(zhuǎn)化為struct_time,實(shí)際上它和strftie()是逆操作
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
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-21 12-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把一個(gè)時(shí)間轉(zhuǎn)換成結(jié)構(gòu)化時(shí)間
The time value as returned by gmtime(), localtime(), and strptime(), and accepted by asctime(), mktime() and strftime(). May be considered as a sequence of 9 integers. >>> 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
返回當(dāng)前時(shí)間的時(shí)間戳(1970元年后的浮點(diǎn)秒數(shù)
Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. >>> time.time() 1498131760.7711384 >>> time.time() 1498131764.7621822
幾種時(shí)間形式的轉(zhuǎn)換
1.把時(shí)間戳轉(zhuǎn)換成結(jié)構(gòu)化時(shí)間,使用的是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.把結(jié)構(gòu)化時(shí)間轉(zhuǎn)換成時(shí)間戳,使用time.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.把結(jié)構(gòu)化時(shí)間轉(zhuǎn)換成時(shí)間字符串,使用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-224.把字符串時(shí)間轉(zhuǎn)換成結(jié)構(gòu)化時(shí)間,使用的是time.strptime命令
>>> t1="2017-05-20 08:08:10" >>> print(t1) 2017-05-20 08: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)
例子:
假如我有一個(gè)時(shí)間字符串,然后想得到這個(gè)時(shí)間之后3天的時(shí)間字符串,可以使用如下的命令:
import time
time1 = "2017-05-20"
#把時(shí)間字符串轉(zhuǎn)換為時(shí)間戳,然后加上3天時(shí)間的秒數(shù)
time2 = time.mktime(time.strptime(time1,"%Y-%m-%d"))+3 * 24 * 3600
#把轉(zhuǎn)換后的時(shí)間戳再轉(zhuǎn)換成時(shí)間字符串
time3 = time.strftime("%Y-%m-%d", time.localtime(time2))
print(time3) 本文名稱:創(chuàng)新互聯(lián)Python教程:Python之time模塊詳解
瀏覽路徑:http://www.fisionsoft.com.cn/article/cocdoid.html


咨詢
建站咨詢
