您好,登錄后才能下訂單哦!
Python中怎么使用time模塊?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
python3中time模塊的用法及說明
python中,導入time模塊使用的命令是
import time
可以使用以下命令查看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"(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)),未給參數相當于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紀元年后經過的浮點秒數)并返回格林威治天文時間下的時間元組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紀元年后經過的浮點秒數)并返回當地時間下的時間元組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) 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紀元年后經過的浮點秒數)
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
推遲調用線程的運行,secs的單位是秒
Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.
strftime
把一個代表時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉化為格式化的時間字符串.如果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].===>分鐘數(00-59) %S Second as a decimal number [00,61].===>秒(01-61) %z Time zone offset from UTC.===>用+HHMM或者-HHMM表示距離格林威治的時區偏移(H代表十進制的小時數,M代表十進制的分鐘數) %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
把一個格式化時間字符串轉化為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
把一個時間轉換成結構化時間
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元年后的浮點秒數
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
幾種時間形式的轉換
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.把結構化時間轉換成時間戳,使用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.把結構化時間轉換成時間字符串,使用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-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" #把時間字符串轉換為時間戳,然后加上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模塊的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。