Python 基礎(chǔ)教程

Python 流程控制

Python 函數(shù)

Python 數(shù)據(jù)類(lèi)型

Python 文件操作

Python 對(duì)象和類(lèi)

Python 日期和時(shí)間

Python 高級(jí)知識(shí)

Python 參考手冊(cè)

Python time 模塊

在本文中,我們將詳細(xì)討論time模塊。我們將通過(guò)實(shí)例學(xué)習(xí)使用time模塊中定義的不同的與時(shí)間相關(guān)的函數(shù)。

Python有一個(gè)命名time為處理與時(shí)間有關(guān)的任務(wù)的模塊。要使用模塊中定義的函數(shù),我們需要首先導(dǎo)入模塊。就是這樣:

import time

這里是常用的時(shí)間相關(guān)函數(shù)。

Python time.time()

time()函數(shù)返回自紀(jì)元以來(lái)經(jīng)過(guò)的秒數(shù)。

對(duì)于Unix系統(tǒng),January 1, 1970, 00:00:00在UTC是歷元(其中,時(shí)間開(kāi)始點(diǎn))。

import time
seconds = time.time()
print("Seconds since epoch =", seconds)

Python time.ctime()

time.ctime()以歷元以來(lái)的秒為參數(shù),返回一個(gè)表示本地時(shí)間的字符串。

import time

# 自紀(jì)元以來(lái)經(jīng)過(guò)的秒數(shù)
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)

如果您運(yùn)行該程序,則輸出將類(lèi)似于:

Local time: Thu Dec 27 15:49:29 2018

Python time.sleep()

sleep()函數(shù)在給定的秒數(shù)內(nèi)暫停(延遲)當(dāng)前線(xiàn)程的執(zhí)行。

import time

print("這是立即打印。")
time.sleep(2.4)
print("這是2.4秒后打印的。")

要了解更多信息,請(qǐng)?jiān)L問(wèn):Python sleep()

在討論其他與時(shí)間相關(guān)的函數(shù)之前,讓我們簡(jiǎn)要地探討一下time.struct_time類(lèi)。

time.struct_time類(lèi)

時(shí)間模塊中的幾個(gè)函數(shù)(例如gmtime(),asctime()等)將time.struct_time對(duì)象作為參數(shù)或?qū)⑵浞祷亍?/p>

這是一個(gè)time.struct_time對(duì)象的實(shí)例。

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, 
                    tm_hour=6, tm_min=35, tm_sec=17, 
                    tm_wday=3, tm_yday=361, tm_isdst=0)
索引
屬性屬性值
0tm_year0000,....,2018,...,9999
1tm_mon1,2,...,12
2tm_mday1,2,...,31
3tm_hour0,1,...,23
4tm_min0,1,...,59
5tm_sec0,1,...,61
6tm_wday0, 1, ..., 6; Monday 為 0
7tm_yday1,2,...,366
8tm_isdst0、1或-1

可以使用索引和屬性訪(fǎng)問(wèn)time.struct_time對(duì)象的值(元素)。

Python time.localtime()

localtime()函數(shù)將自epoch以來(lái)經(jīng)過(guò)的秒數(shù)作為參數(shù),并以localtime返回struct_time。

import time

result = time.localtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

當(dāng)您運(yùn)行程序時(shí),輸出將類(lèi)似于:

result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0)

year: 2018
tm_hour: 15

如果沒(méi)有參數(shù)或None傳遞給localtime(),time()則使用的返回值。

Python time.gmtime()

該gmtime()函數(shù)將自epoch以來(lái)經(jīng)過(guò)的秒數(shù)作為參數(shù),并struct_time以UTC返回。

import time

result = time.gmtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)

運(yùn)行該程序時(shí),輸出為:

result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0)

year = 2018
tm_hour = 8

如果沒(méi)有參數(shù)或None傳遞給gmtime(),time()則使用的返回值。

Python time.mktime()

mktime()函數(shù)將struct_time(或包含9個(gè)元素的元組對(duì)應(yīng)于struct_time)作為參數(shù),并返回自當(dāng)?shù)貢r(shí)間的紀(jì)元以來(lái)經(jīng)過(guò)的秒數(shù)?;旧?,它是localtime()的反函數(shù)。

import time

t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)

local_time = time.mktime(t)
print("Local time:", local_time)

下面的示例顯示mktime()和localtime()的關(guān)系。

import time

seconds = 1545925769

# returns struct_time
t = time.localtime(seconds)
print("t1: ", t)

# returns seconds from struct_time
s = time.mktime(t)
print("\s:", seconds)

當(dāng)您運(yùn)行程序時(shí),輸出將類(lèi)似于:

t1:  time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0)

s: 1545925769.0

Python time.asctime()

該asctime()函數(shù)將struct_time(或包含9個(gè)元素的元組對(duì)應(yīng)于struct_time)作為參數(shù),并返回表示它的字符串。這是一個(gè)實(shí)例:

import time

t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)

result = time.asctime(t)
print("Result:", result)

運(yùn)行該程序時(shí),輸出為:

Result: Fri Dec 28 08:44:04 2018

Python time.strftime()

該strftime()函數(shù)以struct_time(或與其對(duì)應(yīng)的元組)作為參數(shù),并根據(jù)所使用的格式代碼返回表示它的字符串。例如,

import time

named_tuple = time.localtime() # get struct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)

print(time_string)

當(dāng)您運(yùn)行程序時(shí),輸出將類(lèi)似于:

12/28/2018, 09:47:41

這里%Y,%m,%d,%H等都是格式代碼。

  • %Y -年[0001,...,2018,2019,...,9999]

  • %m -月[01,02,...,11,12]

  • %d -天[01,02,...,30,31]

  • %H -小時(shí)[00,01,...,22,23

  • %M -分鐘[00,01,...,58,59]

  • %S -秒[00,01,...,58,61]

要了解更多信息,請(qǐng)?jiān)L問(wèn):time.strftime()。

Python time.strptime()

該strptime()函數(shù)解析表示時(shí)間的字符串并返回struct_time。

import time

time_string = "21 June, 2018"
result = time.strptime(time_string, "%d %B, %Y")

print(result)

運(yùn)行該程序時(shí),輸出為:

time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1)

                                                                                                                                                                                                   

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清