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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3244/viewspace-2837313/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python模組:time模組Python
- python之logging日誌模組詳解Python
- python模組詳解Python
- Python入門—time模組Python
- 每週一個 Python 模組 | timePython
- python時間模組time和datetimePython
- Python time模組詳解(時間戳↔元組形式↔格式化形式三者轉化)Python時間戳
- Python的常見模組:OS和 time模組介紹Python
- Python模組之urllib模組Python
- python模組之collections模組Python
- Python 正規表示式模組詳解Python
- ES6之路之模組詳解
- python之模組Python
- day18:json模組&time模組&zipfile模組JSON
- Python3 pickle模組的使用詳解Python
- python3.x中argparse模組詳解Python
- python IO模組【二】:open函式詳解Python函式
- Python學習【第十二篇】time & datetime模組Python
- time模組,collections模組,佇列和棧佇列
- matplotlib模組詳解
- difflib模組詳解
- psutil模組詳解
- python模組之configparserPython
- python模組之hashlibPython
- python之shutil模組Python
- python–模組之基本Python
- Python模組之jsonPythonJSON
- Nginx原始碼研究之nginx限流模組詳解Nginx原始碼
- Django之使用者認證模組詳解Django
- python–模組之random隨機數模組Pythonrandom隨機
- python–模組之os操作檔案模組Python
- 紅外模組詳解
- lms框架模組詳解框架
- 詳解Python中sys模組的功能與應用Python
- Python時間處理常用模組及用法詳解!Python
- Python學習之模組Python
- Python基礎之模組Python
- Python元組詳解Python