新聞中心

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設,石屏企業(yè)網(wǎng)站建設,石屏品牌網(wǎng)站建設,網(wǎng)站定制,石屏網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,石屏網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
python3中time模塊的用法及說明
python中,導入time模塊使用的命令是
import time
可以使用以下命令查看time模塊內(nèi)置的能夠使用的方法:
dir(time)
可以使用以下命令查看time模塊中每個內(nèi)置方法的說明:
help(time.time_method)
比如time模塊下有一個time.time的方法,現(xiàn)在我想查看這個方法的官方文檔,就可以使用這樣的命令:
help(time.time)
時間的表示形式:
在python中,通常有三種方式來表示時間:時間戳,元組(結(jié)構(gòu)化時間,struct_time),格式化的時間字符串。
(1)時間戳(timestamp):通常來說,時間戳表示從1970年1月1日00:00:00開始按秒計算的偏移量,它的值是float類型
(2)格式化的時間字符串(Format String):‘2017-06-20’
(3)結(jié)構(gòu)化時間(struct_time):struct_time元組共有9個元素:(年,月,日,時,分,秒,一年中的第幾周,一年中的第幾天等)
time模塊中內(nèi)置的常用的方法:
asctime
接受時間元組并返回一個可讀的形式"Tue May 30 17:17:30 2017"(2017年5月30日周二17時17分30秒)的24個字符的字符串
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
作用相當于asctime(localtime(secs)),未給參數(shù)相當于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
接收時間輟(1970紀元年后經(jīng)過的浮點秒數(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
接收時間輟(1970紀元年后經(jīng)過的浮點秒數(shù))并返回當?shù)貢r間下的時間元組t(t.tm_isdst可取為0或1,取決于當?shù)禺敃r是不是夏令時)
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
接受時間元組并返回時間輟(1970紀元年后經(jīng)過的浮點秒數(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)用線程的運行,secs的單位是秒
Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.
相關推薦:《Python視頻教程》
strftime
把一個代表時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉(zhuǎn)化為格式化的時間字符串.如果t未指定,將傳入time.localtime(),如果元組中任命一個元素越界,將會拋出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].===>一個月中的第幾天(01-31) %H Hour (24-hour clock) as a decimal number [00,23].===>一天中的第幾個小時(24小時制,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表示距離格林威治的時區(qū)偏移(H代表十進制的小時數(shù),M代表十進制的分鐘數(shù)) %a Locale's abbreviated weekday name.===>本地(local)簡化星期名稱 %A Locale's full weekday name.===>本地完整星期名稱 %b Locale's abbreviated month name.===>本地簡化月份名稱 %B Locale's full month name.===>本地完整月份名稱 %c Locale's appropriate date and time representation.===>本地相應的日期和時間表示 %I Hour (12-hour clock) as a decimal number [01,12].===>一天中的第幾個小時(12小時制,01-12) %p Locale's equivalent of either AM or PM.===>本地am或者pm的相應符
>>> time.strftime("%Y-%m-%d")
'2017-06-22'
>>> time.strftime("%Y-%m-%d %H-%H-%S")
'2017-06-22 19-19-28'strptim
把一個格式化時間字符串轉(zhuǎn)化為struct_time,實際上它和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把一個時間轉(zhuǎn)換成結(jié)構(gòu)化時間
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
返回當前時間的時間戳(1970元年后的浮點秒數(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
幾種時間形式的轉(zhuǎn)換
1.把時間戳轉(zhuǎn)換成結(jié)構(gòu)化時間,使用的是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)化時間轉(zhuǎn)換成時間戳,使用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)化時間轉(zhuǎn)換成時間字符串,使用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.把字符串時間轉(zhuǎn)換成結(jié)構(gòu)化時間,使用的是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)
例子:
假如我有一個時間字符串,然后想得到這個時間之后3天的時間字符串,可以使用如下的命令:
import time
time1 = "2017-05-20"
#把時間字符串轉(zhuǎn)換為時間戳,然后加上3天時間的秒數(shù)
time2 = time.mktime(time.strptime(time1,"%Y-%m-%d"))+3 * 24 * 3600
#把轉(zhuǎn)換后的時間戳再轉(zhuǎn)換成時間字符串
time3 = time.strftime("%Y-%m-%d", time.localtime(time2))
print(time3) 標題名稱:創(chuàng)新互聯(lián)Python教程:Python之time模塊詳解
路徑分享:http://m.5511xx.com/article/cocdoid.html


咨詢
建站咨詢
