1 time 模塊
time 模塊提供了很多與時間相關的類和函數,下面我們介紹一些常用的。
1.1 struct_time 類
time 模塊的 struct_time 類代表一個時間對象,可以通過索引和屬性名訪問值。對應關系如下所示:
索引 | 屬性 | 值 |
---|---|---|
0 | tm_year(年) | 如:1945 |
1 | tm_mon(月) | 1 ~ 12 |
2 | tm_mday(日) | 1 ~ 31 |
3 | tm_hour(時) | 0 ~ 23 |
4 | tm_min(分) | 0 ~ 59 |
5 | tm_sec(秒) | 0 ~ 61 |
6 | tm_wday(周) | 0 ~ 6 |
7 | tm_yday(一年內第幾天) | 1 ~ 366 |
8 | tm_isdst(夏時令) | -1、0、1 |
tm_sec 范圍為 0 ~ 61,值 60 表示在閏秒的時間戳中有效,并且由于歷史原因支持值 61。
localtime() 表示當前時間,返回類型為 struct_time 對象,示例如下所示:
import time
t = time.localtime()
print('t-->', t)
print('tm_year-->', t.tm_year)
print('tm_year-->', t[0])
輸出結果:???????
t--> time.struct_time(tm_year=2019, tm_mon=12, tm_mday=1, tm_hour=19, tm_min=49, tm_sec=54, tm_wday=6, tm_yday=335, tm_isdst=0)
tm_year--> 2019
tm_year--> 2019
1.2 常用函數
函數(常量) | 說明 |
---|---|
time() | 返回當前時間的時間戳 |
gmtime([secs]) | 將時間戳轉換為格林威治天文時間下的 struct_time,可選參數 secs 表示從 epoch 到現在的秒數,默認為當前時間 |
localtime([secs]) | 與 gmtime() 相似,返回當地時間下的 struct_time |
mktime(t) | localtime() 的反函數 |
asctime([t]) | 接收一個 struct_time 表示的時間,返回形式為:Mon Dec ?2 08:53:47 2019 的字符串 |
ctime([secs]) | ctime(secs) 相當于 asctime(localtime(secs)) |
strftime(format[, t]) | 格式化日期,接收一個 struct_time 表示的時間,并返回以可讀字符串表示的當地時間 |
sleep(secs) | 暫停執行調用線程指定的秒數 |
altzone | 本地 DST 時區的偏移量,以 UTC 為單位的秒數 |
timezone | 本地(非 DST)時區的偏移量,UTC 以西的秒數(西歐大部分地區為負,美國為正,英國為零) |
tzname | 兩個字符串的元組:第一個是本地非 DST 時區的名稱,第二個是本地 DST 時區的名稱 |
epoch:1970-01-01 00:00:00 UTC
基本使用如下所示:???????
import time
print(time.time())
print(time.gmtime())
print(time.localtime())
print(time.asctime(time.localtime()))
print(time.tzname)
# strftime 使用
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
strftime 函數日期格式化符號說明如下所示:
符號 | 說明 |
---|---|
%a | 本地化的縮寫星期中每日的名稱 |
%A | 本地化的星期中每日的完整名稱 |
%b | 本地化的月縮寫名稱 |
%B | 本地化的月完整名稱 |
%c | 本地化的適當日期和時間表示 |
%d | 十進制數 [01,31] 表示的月中日 |
%H | 十進制數 [00,23] 表示的小時(24小時制) |
%I | 十進制數 [01,12] 表示的小時(12小時制) |
%j | 十進制數 [001,366] 表示的年中日 |
%m | 十進制數 [01,12] 表示的月 |
%M | 十進制數 [00,59] 表示的分鐘 |
%p | 本地化的 AM 或 PM |
%S | 十進制數 [00,61] 表示的秒 |
%U | 十進制數 [00,53] 表示的一年中的周數(星期日作為一周的第一天) |
%w | 十進制數 [0(星期日),6] 表示的周中日 |
%W | 十進制數 [00,53] 表示的一年中的周數(星期一作為一周的第一天) |
%x | 本地化的適當日期表示 |
%X | 本地化的適當時間表示 |
%y | 十進制數 [00,99] 表示的沒有世紀的年份 |
%Y | 十進制數表示的帶世紀的年份 |
%z | 時區偏移以格式 +HHMM 或 -HHMM 形式的 UTC/GMT 的正或負時差指示,其中 H 表示十進制小時數字,M 表示小數分鐘數字 [-23:59, +23:59] |
%Z | 時區名稱 |
%% | 字面的 '%' 字符 |
2 datetime 模塊
datatime 模塊重新封裝了 time 模塊,提供了更多接口,變得更加直觀和易于調用。
2.1 date 類
date 類表示一個由年、月、日組成的日期,格式為:datetime.date(year, month, day)。
-
year 范圍為:[1, 9999]
-
month 范圍為:[1, 12]
-
day 范圍為 [1, 給定年月對應的天數]。
類方法和屬性如下所示:
方法(屬性) | 說明 |
---|---|
today() | 返回當地的當前日期 |
fromtimestamp(timestamp) | 根據給定的時間戮,返回本地日期 |
min | date 所能表示的最小日期 |
max | date 所能表示的最大日期 |
使用示例如下所示:???????
import datetime
import time
print(datetime.date.today())
print(datetime.date.fromtimestamp(time.time()))
print(datetime.date.min)
print(datetime.date.max)
實例方法和屬性如下所示:
方法(屬性) | 說明 |
---|---|
replace(year, month, day) | 生成一個新的日期對象,用參數指定的年,月,日代替原有對象中的屬性 |
timetuple() | 返回日期對應的 struct_time 對象 |
weekday() | 返回一個整數代表星期幾,星期一為 0,星期天為 6 |
isoweekday() | 返回一個整數代表星期幾,星期一為 1,星期天為 7 |
isocalendar() | 返回格式為 (year,month,day) 的元組 |
isoformat() | 返回格式如 YYYY-MM-DD 的字符串 |
strftime(format) | 返回自定義格式的字符串 |
year | 年 |
month | 月 |
day | 日 |
使用示例如下所示:???????
import datetime
td = datetime.date.today()
print(td.replace(year=1945, month=8, day=15))
print(td.timetuple())
print(td.weekday())
print(td.isoweekday())
print(td.isocalendar())
print(td.isoformat())
print(td.strftime('%Y %m %d %H:%M:%S %f'))
print(td.year)
print(td.month)
print(td.day)
2.2 time 類
time 類表示由時、分、秒、微秒組成的時間,格式為:time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)。
-
hour 范圍為:[0, 24)
-
minute 范圍為:[0, 60)
-
second 范圍為:[0, 60)
-
microsecond 范圍為:[0, 1000000)
-
fold 范圍為:[0, 1]
實例方法和屬性如下所示:
方法(屬性) | 說明 |
---|---|
isoformat() | 返回 HH:MM:SS 格式的字符串 |
replace(hour, minute, second, microsecond, tzinfo, * fold=0) | 創建一個新的時間對象,用參數指定的時、分、秒、微秒代替原有對象中的屬性 |
strftime(format) | 返回自定義格式的字符串 |
hour | 時 |
minute | 分 |
second | 秒 |
microsecond | 微秒 |
tzinfo | 時區 |
使用示例如下所示:???????
import datetime
t = datetime.time(10, 10, 10)
print(t.isoformat())
print(t.replace(hour=9, minute=9))
print(t.strftime('%I:%M:%S %p'))
print(t.hour)
print(t.minute)
print(t.second)
print(t.microsecond)
print(t.tzinfo)
2.3 datetime 類
datetime 包括了 date 與 time 的所有信息,格式為:datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0),參數范圍值參考 date 類與 time 類。
類方法和屬性如下所示:
方法(屬性) | 說明 |
---|---|
today() | 返回當地的當前時間 |
now(tz=None) | 類似于 today(),可選參數 tz 可指定時區 |
utcnow() | 返回當前 UTC 時間 |
fromtimestamp(timestamp, tz=None) | 根據時間戳返回對應時間 |
utcfromtimestamp(timestamp) | 根據時間戳返回對應 UTC 時間 |
combine(date, time) | 根據 date 和 time 返回對應時間 |
min | datetime 所能表示的最小日期 |
max | datetime 所能表示的最大日期 |
使用示例如下所示:???????
import datetime
print(datetime.datetime.today())
print(datetime.datetime.now())
print(datetime.datetime.utcnow())
print(datetime.datetime.fromtimestamp(time.time()))
print(datetime.datetime.utcfromtimestamp(time.time()))
print(datetime.datetime.combine(datetime.date(2019, 12, 1), datetime.time(10, 10, 10)))
print(datetime.datetime.min)
print(datetime.datetime.max)
實例方法和屬性如下所示:
方法(屬性) | 說明 |
---|---|
date() | 返回具有同樣 year,month,day 值的 date 對象 |
time() | 返回具有同樣 hour, minute, second, microsecond 和 fold 值的 time 對象 |
replace(year, month, day=self.day, hour, minute, second, microsecond, tzinfo, * fold=0) | 生成一個新的日期對象,用參數指定的年,月,日,時,分,秒...代替原有對象中的屬性 |
weekday() | 返回一個整數代表星期幾,星期一為 0,星期天為 6 |
isoweekday() | 返回一個整數代表星期幾,星期一為 1,星期天為 7 |
isocalendar() | 返回格式為 (year,month,day) 的元組 |
isoformat() | 返回一個以 ISO 8601 格式表示日期和時間的字符串 YYYY-MM-DDTHH:MM:SS.ffffff |
strftime(format) | 返回自定義格式的字符串 |
year | 年 |
month | 月 |
day | 日 |
hour | 時 |
minute | 分 |
second | 秒 |
microsecond | 微秒 |
tzinfo | 時區 |
使用示例如下所示:???????
import datetime
td = datetime.datetime.today()
print(td.date())
print(td.time())
print(td.replace(day=11, second=10))
print(td.weekday())
print(td.isoweekday())
print(td.isocalendar())
print(td.isoformat())
print(td.strftime('%Y-%m-%d %H:%M:%S .%f'))
print(td.year)
print(td.month)
print(td.month)
print(td.hour)
print(td.minute)
print(td.second)
print(td.microsecond)
print(td.tzinfo)
3 calendar 模塊
calendar 模塊提供了很多可以處理日歷的函數。
3.1 常用函數
方法 | 說明 |
---|---|
setfirstweekday(weekday) | 設置每一周的開始(0 表示星期一,6 表示星期天) |
firstweekday() | 返回當前設置的每星期的第一天的數值 |
isleap(year) | 如果 year 是閏年則返回 True ,否則返回 False |
leapdays(y1, y2) | 返回 y1 至 y2 (包含 y1 和 y2 )之間的閏年的數量 |
weekday(year, month, day) | 返回指定日期的星期值 |
monthrange(year, month) | 返回指定年份的指定月份第一天是星期幾和這個月的天數 |
month(theyear, themonth, w=0, l=0) | 返回月份日歷 |
prcal(year, w=0, l=0, c=6, m=3) | 返回年份日歷 |
使用示例如下所示:???????
import calendar
calendar.setfirstweekday(1)
print(calendar.firstweekday())
print(calendar.isleap(2019))
print(calendar.leapdays(1945, 2019))
print(calendar.weekday(2019, 12, 1))
print(calendar.monthrange(2019, 12))
print(calendar.month(2019, 12))
print(calendar.prcal(2019))
3.2 Calendar 類
Calendar 對象提供了一些日歷數據格式化的方法,實例方法如下所示:
方法 | 說明 |
---|---|
iterweekdays() | 返回一個迭代器,迭代器的內容為一星期的數字 |
itermonthdates(year, month) | 返回一個迭代器,迭代器的內容為年 、月的日期 |
使用示例如下所示:
???????
from calendar import Calendar
c = Calendar()
print(list(c.iterweekdays()))
for i in c.itermonthdates(2019, 12):
print(i)
3.3 TextCalendar 類
TextCalendar 為 Calendar子類,用來生成純文本日歷。實例方法如下所示:
方法 | 說明 |
---|---|
formatmonth(theyear, themonth, w=0, l=0) | 返回一個多行字符串來表示指定年、月的日歷 |
formatyear(theyear, w=2, l=1, c=6, m=3) | 返回一個 m 列日歷,可選參數 w, l, 和 c 分別表示日期列數, 周的行數, 和月之間的間隔 |
使用示例如下所示:???????
from calendar import TextCalendar
tc = TextCalendar()
print(tc.formatmonth(2019, 12))
print(tc.formatyear(2019))
3.4 HTMLCalendar類
HTMLCalendar 類可以生成 HTML 日歷。實例方法如下所示:
方法 | 說明 |
---|---|
formatmonth(theyear, themonth, withyear=True) | 返回一個 HTML 表格作為指定年、月的日歷 |
formatyear(theyear, width=3) | 返回一個 HTML 表格作為指定年份的日歷 |
formatyearpage(theyear, width=3, css='calendar.css', encoding=None) | 返回一個完整的 HTML 頁面作為指定年份的日歷 |
使用示例如下所示:???????
from calendar import HTMLCalendar
hc = HTMLCalendar()
print(hc.formatmonth(2019, 12))
print(hc.formatyear(2019))
print(hc.formatyearpage(2019))