在 Python 中是沒有原生數(shù)據類型支持時間的,日期與時間的操作需要借助三個模塊,分別是 time
、datetime
、calendar
。
time
模塊可以操作 C 語言庫中的時間相關函數(shù),時鐘時間與處理器運行時間都可以獲取。
datetime
模塊提供了日期與時間的高級接口。
calendar
模塊為通用日歷相關函數(shù),用于創(chuàng)建數(shù)周、數(shù)月、數(shù)年的周期性事件。
在學習之前,還有一些術語要補充一下,這些術語你當成慣例即可。這里在 Python 官方文檔中也有相關說明,不過信息比較多,橡皮擦為你摘錄必須知道的一部分。
epoch(紀元)
是時間開始的點,其值取決于平臺。
對于 Unix, epoch(紀元)
是 1970年1月1日00:00:00(UTC)
。要找出給定平臺上的 epoch
,請使用 time.gmtime(0)
進行查看,例如橡皮擦電腦顯示:
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
術語 紀元秒數(shù)
是指自 epoch (紀元)
時間點以來經過的總秒數(shù),通常不包括閏秒。 在所有符合 POSIX 標準的平臺上,閏秒都不會記錄在總秒數(shù)中。
程序員中常把 紀元秒數(shù)
稱為 時間戳
。
time 時間模塊
該模塊核心為控制時鐘時間。
get_clock_info 函數(shù)
該函數(shù)獲取時鐘的基本信息,得到的值因不同系統(tǒng)存在差異,函數(shù)原型比較簡單:
time.get_clock_info(name)
其中 name
可以取下述值:
monotonic
:time.monotonic()
perf_counter
: time.perf_counter()
process_time
: time.process_time()
thread_time
: time.thread_time()
time
: time.time()
該函數(shù)的返回值具有以下屬性:
adjustable
: 返回 True 或者 False。如果時鐘可以自動更改(例如通過 NTP 守護程序)或由系統(tǒng)管理員手動更改,則為 True ,否則為 False ;
implementation
: 用于獲取時鐘值的基礎 C 函數(shù)的名稱,就是調用底層 C 的函數(shù);
monotonic
:如果時鐘不能倒退,則為 True ,否則為 False;
resolution
: 以秒為單位的時鐘分辨率( float )。
import time
available_clocks = [
('clock', time.clock),
('monotonic', time.monotonic),
('perf_counter', time.perf_counter),
('process_time', time.process_time),
('time', time.time),
]
for clock_name, func in available_clocks:
print('''
{name}:
adjustable : {info.adjustable}
implementation: {info.implementation}
monotonic : {info.monotonic}
resolution : {info.resolution}
current : {current}
'''.format(
name=clock_name,
info=time.get_clock_info(clock_name),
current=func()))
運行結果如下圖所示。
上圖顯示橡皮擦的計算機在 clock
與 perf_counter
中,調用底層 C 函數(shù)是一致的。
獲取時間戳
在 Python 中通過 time.time()
函數(shù)獲取紀元秒數(shù),它可以把從 epoch
開始之后的秒數(shù)以浮點數(shù)格式返回。
import time
print(time.time())
# 輸出結果 1615257195.558105
時間戳大量用于計算時間相關程序,屬于必須掌握內容。
獲取可讀時間
時間戳主要用于時間上的方便計算,對于人們閱讀是比較難理解的,如果希望獲取可讀時間,使用 ctime()
函數(shù)獲取。
import time
print(time.ctime())
# 輸出內容:Tue Mar 9 10:35:51 2021
如何將時間戳轉換為可讀時間,使用 localtime
函數(shù)即可。
localtime = time.localtime(time.time())
print("本地時間為 :", localtime)
輸出結果為 class 'time.struct_time'>
類型數(shù)據,后文將對其進行格式化操作:
本地時間為 : time.struct_time(tm_year=2021, tm_mon=3, tm_mday=9, tm_hour=10, tm_min=37, tm_sec=27, tm_wday=1, tm_yday=68, tm_isdst=0)
上述代碼中的時間戳最小值是 0,最大值由于 Python 環(huán)境和操作系統(tǒng)決定,我本地 64 位操作系統(tǒng)進行測試的時候,得到的數(shù)據如下:
import time
localtime = time.localtime(0)
print("時間為 :", localtime)
# 時間為 : time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
localtime = time.localtime(32536799999)
print("時間為 :", localtime)
# 時間為 : time.struct_time(tm_year=3001, tm_mon=1, tm_mday=19, tm_hour=15, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=19, tm_isdst=0)
localtime = time.localtime(99999999999)
print("時間為 :", localtime)
# OSError: [Errno 22] Invalid argument
print(type(localtime))
單調時間 monotonic time
monotonic time
從系統(tǒng)啟動開始計時,從 0 開始單調遞增。
操作系統(tǒng)的時間可能不是從 0 開始,而且會因為時間出錯而回調。
該函數(shù)原型如下,不需要任何參數(shù),返回一個浮點數(shù),表示小數(shù)秒內的單調時鐘的值:
測試代碼如下:
print("單調時間",time.monotonic())
# 輸出:單調時間 12279.244
處理器時鐘時間
time()
函數(shù)返回的是紀元秒數(shù)(時間戳), clock()
函數(shù)返回的是處理器時鐘時間。
該函數(shù)函數(shù)的返回值:
- 在第一次調用的時候,返回的是程序運行的實際時間;
- 在第二次之后的調用,返回的是自第一次調用后到這次調用的時間間隔。
需要注意的是 Python 3.8 已移除 clock()
函數(shù),用 time.perf_counter()
或 time.process_time()
方法替代。
t0 = time.clock()
# 運行一段代碼
print(time.clock() - t0, "程序運行時間")
我使用的 Python 版本較高,提示異常如下:
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 t0 = time.clock()
性能計數(shù)器 time.perf_counter
perf_counter()
函數(shù)的 epoch
(紀元)是未定義的。一般使用該函數(shù)都是為了比較和計算,不是為了用作絕對時間,該點需要注意下。
該函數(shù)用于測量較短持續(xù)時間的具有最高有效精度的時鐘,包括睡眠狀態(tài)消耗的時間,使用兩次調用才會有效。
測試代碼如下:
t0 = time.perf_counter()
# 運行一段代碼
for i in range(100000):
pass
print("程序運行時間", time.perf_counter() - t0)
與其類似的函數(shù)有 perf_counter_ns()
、process_time()
、process_time_ns()
,具體可以查詢手冊進行學習,先掌握 perf_counter()
函數(shù)即可。
時間組件
上文已經涉及了時間組件相關的知識,通過 localtime
得到的 struct_time
類型的數(shù)據。
這里涉及到的函數(shù)有 gmtime()
返回 UTC 中的當前時間,localtime()
返回當前時區(qū)對應的時間,mktime()
接收 struce_time
類型數(shù)據并將其轉換成浮點型數(shù)值,即時間戳。
print("*"*10)
print(time.gmtime())
print("*"*10)
print(time.localtime())
print("*"*10)
print(time.mktime(time.localtime()))
struct_time 類型包含的內容
上述代碼返回的數(shù)據格式為:
time.struct_time(tm_year=2021, tm_mon=3, tm_mday=9, tm_hour=12, tm_min=50, tm_sec=35, tm_wday=1, tm_yday=68, tm_isdst=0)
其中各值可以根據英文含義進行理解 :tm_year
年份(range[1,12]),tm_mon
月份(range[1,12]),tm_mday
天數(shù)(range[1,31]),tm_hour
天數(shù)(range[0,23]),tm_min
分鐘 (range[0,59]), tm_sec
秒數(shù) (range[0,61]), tm_wday
星期 (range[0,6],0 是星期日), tm_yday
一年中的一天(range[1,366] ),tm_isdst
在夏令時生效時設置為 1,而在夏令時不生效時設置為 0,值-1 表示這是未知的。
解析和格式化時間
strptime()
和 strftime()
函數(shù)可以使時間值在 struct_time
表示和字符串表示之間相互轉換。
對于 strftime
函數(shù),其中的參數(shù)參考官方即可。
x = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(x)
這里的學習,沒有什么難度大的點,孰能生巧的知識。
strptime 函數(shù)的應用
x = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(x)
# 方向操作,字符串格式化成 time.struct_time
struct_time = time.strptime(x, "%Y-%m-%d %H:%M:%S")
print(struct_time)
需要記憶的就是 strftime
與 strptime
函數(shù)只有中間的字符不同,一個是 f
,另一個是 p
。
time 小節(jié)
對于 time 模塊,sleep
函數(shù)屬于必備知識點,但是太常用了,你肯定已經很熟悉了。
對于模塊的學習,最權威的就是官方手冊了,time 模塊
datetime 模塊
該模塊比 time
模塊高級了很多,并且對 time
模塊進行了封裝,提供的功能更加強大了。
在 datetime
模塊中,Python 提供了 5 個主要的對象類,分別如下:
datetime
:允許同時操作時間和日期;
date
:只操作日期;time
:只操作時間;
timedelta
:用于操作日期以及測量時間跨度;
tzinfo
:處理時區(qū)。
date 類
優(yōu)先展示部分該類的屬性和方法,都是記憶層面的知識。
min
、max
:date 對象能表示的最大、最小日期;
resolution
:date 對象表示日期的最小單位,返回天;
today()
:返回表示當前本地日期的 date 對象;
fromtimestamp(timestamp)
:根據時間戳,返回一個 date 對象。
測試代碼如下:
from datetime import date
import time
print('date.min:', date.min)
print('date.max:', date.max)
print('date.resolution:', date.resolution)
print('date.today():', date.today())
print('date.fromtimestamp():', date.fromtimestamp(time.time()))
輸出結果:
date.min: 0001-01-01
date.max: 9999-12-31
date.resolution: 1 day, 0:00:00
date.today(): 2021-03-09
date.fromtimestamp(): 2021-03-09
date 對象的屬性和方法
通過下述代碼創(chuàng)建一個 date 對象:
d = date(year=2021,month=3,day=9)
print(d)
該對象具備下述屬性和方法:
d.year
:返回年;
d.month
:返回月;
d.day
:返回日;
d.weekday()
:返回 weekday,如果是星期一,返回 0;如果是星期 2,返回 1,以此類推;
d.isoweekday()
:返回 weekday,如果是星期一,返回 1;如果是星期 2,返回 2,以此類推;
d.isocalendar()
:返回格式如(year, wk num, wk day);
d.isoformat()
:返回格式如'YYYY-MM-DD'的字符串;
d.strftime(fmt)
:自定義格式化字符串,與 time 模塊中的 strftime 類似。
time 類
time
類定義的類屬性:
min
、max
:time 類所能表示的最小、最大時間。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999)
;
- resolution:時間的最小單位,這里是 1 微秒;
通過其構造函數(shù)可以創(chuàng)建一個 time
對象。
t = time(hour=20, minute=20, second=40)
print(t)
time
類提供的實例方法和屬性:
t.hour
、t.minute
、t.second
、t.microsecond
:時、分、秒、微秒;
t.tzinfo
:時區(qū)信息;
t.isoformat()
:返回型如”HH:MM:SS”格式的字符串時間表示;
t.strftime(fmt)
:返回自定義格式化字符串。
datetime 類
該類是 date
類與 time
類的結合體,很多屬性和方法前文已經介紹,再補充一些比較常用的屬性和方法。
獲取當前的日期與時間:
from datetime import datetime
dt = datetime.now()
print(dt)
獲取時間戳:
dt = datetime.now()
# 使用 datetime 的內置函數(shù) timestamp()
stamp = datetime.timestamp(dt)
print(stamp)
timedelta 類
通過 timedelta
函數(shù)返回一個 timedelta
時間間隔對象,該函數(shù)沒有必填參數(shù),如果寫入一個整數(shù)就是間隔多少天的的意思。
# 間隔 10 天
timedelta(10)
# 跨度為1 周
timedelta(weeks=1)
兩個時間間隔對象可以彼此之間相加或相減,返回的仍是一個時間間隔對象。
一個 datetime 對象如果減去一個時間間隔對象,那么返回的對應減去之后的 datetime 對象,然后兩個 datetime 對象如果相減,返回的是一個時間間隔對象。
更多關于 datetime 類使用的知識,可以參考官方手冊。
calendar 模塊(日歷)
此模塊的函數(shù)都是日歷相關的,例如打印某月的字符月歷。
calendar
模塊定義了 Calendar 類,它封裝了值的計算, 例如給定月份或年份中周的日期。通過 TextCalendar 和 HTMLCalendar 類可以生成預格式化的輸出。
基本代碼:
import calendar
c = calendar.TextCalendar(calendar.SUNDAY)
c.prmonth(2021, 3)
上述代碼,默認是從周日開始的,輸出結果:
March 2021
Su Mo Tu We Th Fr Sa
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
該模塊使用頻率較低,詳細使用參考地址 進行學習。
總結
到此這篇關于Python玩轉時間和日期庫的實現(xiàn)的文章就介紹到這了,更多相關Python 時間和日期庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python3時間轉換之時間戳轉換為指定格式的日期方法詳解
- python獲取指定時間段內特定規(guī)律的日期列表
- python (logging) 日志按日期、大小回滾的操作
- Python tkinter實現(xiàn)日期選擇器
- Python繪制數(shù)碼晶體管日期
- Pycharm創(chuàng)建python文件自動添加日期作者等信息(步驟詳解)
- python實現(xiàn)按日期歸檔文件
- 基于python獲取本地時間并轉換時間戳和日期格式
- python 帶時區(qū)的日期格式化操作
- 如何使用 Python 讀取文件和照片的創(chuàng)建日期
- Python 常用日期處理 -- calendar 與 dateutil 模塊的使用
- python 常用日期處理-- datetime 模塊的使用
- python自動提取文本中的時間(包含中文日期)
- Python 處理日期時間的Arrow庫使用
- Python 日期與時間轉換的方法
- Python如何將字符串轉換為日期
- python實現(xiàn)將中文日期轉換為數(shù)字日期
- 教你怎么用python實現(xiàn)字符串轉日期