目錄
2.3:gmtime([seconds]) 時間戳-->UTC結構化時間
2.4:localtime([seconds]) 時間戳-->本地結構化時間
2.5:time.ctime(seconds) 時間戳-->本地格式化字符串
2.6:time.asctime([tuple]) 結構化時間-->格式化時間字符串
2.7:time.mktime(tuple) 結構化時間-->時間戳
2.8:strftime(format[, tuple]) 結構化時間-->時間字符串
2.9:time.strptime(string, format) 時間字符串-->結構化時間
1:獲取模塊幫助文檔
import time
print(time.__doc__)
print(dir(time))
for item in dir(time):
print(item)
print(help(time.time))
print(help(time.strftime))
print(help(time.strptime))
2:time模塊方法
2.1:time.sleep(t)
time模塊最常用的方法之一,用來睡眠或者暫停程序t秒,t可以是浮點數或整數。
2.2:time.time()
import time
print(time.time()) # 1590908279.7763638
2.3:gmtime([seconds]) 時間戳-->UTC結構化時間
將一個時間戳轉換爲UTC時區的結構化時間。可選參數secs的默認值爲time.time()。
2.4:localtime([seconds]) 時間戳-->本地結構化時間
將一個時間戳轉換爲當前時區的結構化時間。如果secs參數未提供,則以當前時間爲準,即time.time()。
import time
print(time.time()) # 1590908279.7763638
print(time.gmtime())
print(time.localtime())
print('*'*20)
print(time.gmtime(1580908279))
print(time.localtime(1580908279))
"""
1590908802.0024452
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=7, tm_min=6, tm_sec=42, tm_wday=6, tm_yday=152, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=15, tm_min=6, tm_sec=42, tm_wday=6, tm_yday=152, tm_isdst=0)
********************
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=5, tm_hour=13, tm_min=11, tm_sec=19, tm_wday=2, tm_yday=36, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=5, tm_hour=21, tm_min=11, tm_sec=19, tm_wday=2, tm_yday=36, tm_isdst=0)
"""
2.5:time.ctime(seconds) 時間戳-->本地格式化字符串
把一個時間戳轉化爲本地時間的格式化字符串。默認使用time.time()作爲參數。
import time
print(time.ctime())
print(time.ctime(time.time()))
print(time.ctime(1580908279))
"""
Sun May 31 15:12:56 2020
Sun May 31 15:12:56 2020
Wed Feb 5 21:11:19 2020
"""
2.6:time.asctime([tuple]) 結構化時間-->格式化時間字符串
把一個結構化時間轉換爲Sun May 31 15:15:36 2020這種形式的格式化時間字符串。默認將time.localtime()作爲參數。
import time
print(time.asctime())
print(time.asctime(time.localtime()))
print(time.asctime(time.localtime(1580908279)))
"""
Sun May 31 15:15:36 2020
Sun May 31 15:15:36 2020
Wed Feb 5 21:11:19 2020
"""
2.7:time.mktime(tuple) 結構化時間-->時間戳
將一個結構化時間轉化爲時間戳。time.mktime()執行與gmtime(),localtime()相反的操作,它接收struct_time對象作爲參數,返回用秒數表示時間的浮點數。
import time
print(time.mktime(time.localtime())) # 1590909638.0
2.8:strftime(format[, tuple]) 結構化時間-->時間字符串
把一個struct_time(如time.localtime()和time.gmtime()的返回值)轉化爲格式化的時間字符串,顯示的格式由參數format決定。如果未指定t,默認傳入time.localtime()。
import time
print(time.strftime("%Y-%m-%d %H:%M:%S"))
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
"""
2020-05-31 15:25:37
2020-05-31 07:25:37
"""
"""
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%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.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
"""
2.9:time.strptime(string, format) 時間字符串-->結構化時間
將格式化時間字符串轉化成結構化時間。該方法是time.strftime()方法的逆操作。
import time
stime = "2020-05-31 15:25:37"
st = time.strptime(stime,"%Y-%m-%d %H:%M:%S")
print(st)
print(time.mktime(st)) # 轉換爲時間戳
"""
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=15, tm_min=25, tm_sec=37, tm_wday=6, tm_yday=152, tm_isdst=-1)
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%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.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
"""
3:時間格式轉換
時間格式轉換匯總:
從 | 到 | 方法 |
---|---|---|
時間戳 | UTC結構化時間 | gmtime() |
時間戳 | 本地結構化時間 | localtime() |
UTC結構化時間 | 時間戳 | calendar.timegm() |
本地結構化時間 | 時間戳 | mktime() |
結構化時間 | 格式化字符串 | strftime() |
格式化字符串 | 結構化時間 | strptime() |
格式化字符串 | 時間戳 | 見下面代碼 |
時間戳 | 格式化字符串 | 見下面代碼 |
3.1 格式化字符串-->時間戳
# encoding=gbk
import time
# stime 與 format需要對應
def str2time(stime,format="%Y-%m-%d %H:%M:%S"):
st = time.strptime(stime,format)
return time.mktime(st)
# return time.mktime(time.strptime(stime,format))
if __name__ == "__main__":
print(str2time("2020-05-31 15:58:34" ))
print(str2time("2020-05-31 15","%Y-%m-%d %H" ))
print(str2time("2020-05-31","%Y-%m-%d" ))
"""
1590911914.0
1590908400.0
1590854400.0
"""
3.2 時間戳-->格式化字符串
# encoding=gbk
import time
def time2str(itime,format="%Y-%m-%d %H:%M:%S"):
st=time.localtime(itime)
return time.strftime(format,st)
# return time.strftime(format,time.localtime(itime))
if __name__ == "__main__":
print(time2str(1590911914))
print(time2str(1590854400,"%Y-%m-%d %H"))
"""
2020-05-31 15:58:34
2020-05-31 00
"""