Python中怎么使用time模塊-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

10年積累的成都網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有鐘祥免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

Python中怎么使用time模塊?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

python3中time模塊的用法及說明

python中,導(dǎo)入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

作用相當(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

接收時間輟(1970紀(jì)元年后經(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ì)元年后經(jīng)過的浮點秒數(shù))并返回當(dāng)?shù)貢r間下的時間元組t(t.tm_isdst可取為0或1,取決于當(dāng)?shù)禺?dāng)時是不是夏令時)

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ì)元年后經(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.

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代表十進(jìn)制的小時數(shù),M代表十進(jìn)制的分鐘數(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.===>本地相應(yīng)的日期和時間表示
    %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的相應(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

把一個格式化時間字符串轉(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

返回當(dāng)前時間的時間戳(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-22

4.把字符串時間轉(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)

看完上述內(nèi)容,你們掌握Python中怎么使用time模塊的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站名稱:Python中怎么使用time模塊-創(chuàng)新互聯(lián)
文章位置:http://bm7419.com/article6/dgdpog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站用戶體驗、做網(wǎng)站App設(shè)計、定制開發(fā)、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營