Python 的 time 模塊下的函數轉換常見日期格式


本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理

本文章來自騰訊雲 作者:Python知識大全

想要學習Python?有問題得不到第一時間解決?來看看這里“1039649593”滿足你的需求,資料都已經上傳至文件中,可以自行下載!還有海量最新2020python學習資料。
點擊查看
在這里插入圖片描述

Python 程序能用很多方式處理日期和時間,轉換日期格式是一個常見的功能。

Python 提供了一個 time 和 calendar 模塊可以用於格式化日期和時間。

時間間隔是以秒為單位的浮點小數。

每個時間戳都以自從1970年1月1日午夜(歷元)經過了多長時間來表示。

Python 的 time 模塊下有很多函數可以轉換常見日期格式。如函數time.time()用於獲取當前時間戳, 如下實例:

>>> import time   # 引入time模塊
>>> a = time.time()
>>> print(a)
1564997412.3804424

 

時間戳單位最適於做日期運算。但是1970年之前的日期就無法以此表示了。太遙遠的日期也不行,UNIX和Windows只支持到2038年。

很多Python函數用一個元組裝起來的9組數字處理時間:
在這里插入圖片描述
上述也就是struct_time元組。這種結構具有如下屬性:
在這里插入圖片描述

獲取當前時間

從返回浮點數的時間輟方式向時間元組轉換,只要將浮點數傳遞給如localtime之類的函數。

import time
>>> localtime = time.localtime(time.time())
>>> print(localtime)
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=5, tm_hour=17, tm_min=36, tm_sec=33, tm_wday=0, tm_yday=217, tm_isdst=0)

 

獲取格式化的時間

你可以根據需求選取各種格式,但是最簡單的獲取可讀的時間模式的函數是asctime():

>>> import time
>>> localtime = time.asctime( time.localtime(time.time()) )
>>> print(localtime)
'Mon Aug  5 17:39:25 2019'

 

日期格式化

我們可以使用 time 模塊的 strftime 方法來格式化日期,:

import time
>>> localtime = time.asctime( time.localtime(time.time()) )

# 轉換為 2019-08-05 17:42:33 格式
>>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
2019-08-05 17:42:33

# 轉換為 Mon Aug 05 17:45:31 2019 格式
>>> print ( time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) )
Mon Aug 05 17:45:31 2019

# 將格式字符串轉換為時間戳
>>> print ( time.mktime(time.strptime(localtime,"%a %b %d %H:%M:%S %Y")) )
1564997965.0

 

在這里插入圖片描述

Time模塊

Time模塊包含了以下內置函數,既有時間處理相的,也有轉換時間格式的:

1 time.altzone

返回格林威治西部的夏令時地區的偏移秒數。如果該地區在格林威治東部會返回負值(如西歐,包括英國)。對夏令時啟用地區才能使用。

>>> print( "time.altzone %d " % time.altzone)
time.altzone -32400

 

2 time.asctime([tupletime])

接受時間元組並返回一個可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時07分14秒)的24個字符的字符串。

import time
>>> t = time.localtime()
>>> print( "time.asctime(t): %s " % time.asctime(t))
time.asctime(t): Mon Aug  5 17:58:39 2019

 

3 time.clock( )

用以浮點數計算的秒數返回當前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。

>>> def procedure():
...     time.sleep(2.5)
...
>>> # 測量過程的時間
... t0 = time.clock()
>>> procedure()
>>> print (time.clock() - t0)
__main__:1: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
2.585575399999925 
>>>
>>> # 測量后的時間
... t0 = time.time()
>>> procedure()
>>> print (time.time() - t0)
8.201099157333374 
>>>

 

4 time.ctime([secs])

作用相當於asctime(localtime(secs)),未給參數相當於asctime() 
import time
>>> print( "time.ctime() : %s" % time.ctime())
time.ctime() : Mon Aug  5 18:08:19 2019

 

5 time.localtime([secs])

接收時間輟(1970紀元后經過的浮點秒數)並返回當地時間下的時間元組t(t.tm_isdst可取0或1,取決於當地當時是不是夏令時)。

>>> import time
>>> t = time.localtime()
>>> print(t)
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=5, tm_hour=18, tm_min=33, tm_sec=31, tm_wday=0, tm_yday=217, tm_isdst=0)

 

6 time.sleep(secs)

推遲調用線程的運行,secs指秒數。

import time
time.sleep(2, 5) # 睡眠時間在[2, 5)之間

 

7 time.strftime(fmt[,tupletime])

接收以時間元組,並返回以可讀字符串表示的當地時間,格式由fmt決定。

t = (2019, 2, 17, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print (time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))
# 輸出
Feb 18 2019 00:03:38

 

8 time.strptime(str,fmt=’%a %b %d %H:%M:%S %Y’)

根據fmt的格式把一個時間字符串解析為時間元組。

9 time.time( )

返回當前時間的時間戳(1970紀元后經過的浮點秒數)。

Time模塊包含了以下2個非常重要的屬性:

10 time.timezone

屬性time.timezone是當地時區(未啟動夏令時)距離格林威治的偏移秒數(>0,美洲;<=0大部分歐洲,亞洲,非洲)。

11 time.tzname

屬性time.tzname包含一對根據情況的不同而不同的字符串,分別是帶夏令時的本地時區名稱,和不帶的。

獲取某月日歷

Calendar模塊有很廣泛的方法用來處理年歷和月歷,例如打印某月的月歷:

import calendar

cal = calendar.month(2019, 8)
print ("--以下輸出2019年8月份的日歷--")
print (cal)

--以下輸出2019年8月份的日歷--
  August 2019
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM