時間模塊程序員
在Python中,一般有這幾種方式來表示時間:函數
1 1 import time 2 2 #--------------------------咱們先以當前時間爲準,讓你們快速認識三種形式的時間 3 3 print(time.time()) # 時間戳:1487130156.419527 4 4 print(time.strftime("%Y-%m-%d %X")) #格式化的時間字符串:'2017-02-15 11:40:53' 5 5 6 6 print(time.localtime()) #本地時區的struct_time 7 7 print(time.gmtime()) #UTC時區的struct_time
其中計算機認識的時間只能是'時間戳'格式,而程序員可處理的或者說人類能看懂的時間有: '格式化的時間字符串','結構化的時間' ,因而有了下圖的轉換關係spa
格式化字符串時間 ------strftime------> 結構化的時間 ---------mktime--------> 時間戳線程
(format string ) <------strftime------ (struct_time ) <------localtime gmtime------ (timestamp)code
1 #--------------------------按圖1轉換時間 2 # localtime([secs]) 3 # 將一個時間戳轉換爲當前時區的struct_time。secs參數未提供,則以當前時間爲準。 4 time.localtime() 5 time.localtime(1473525444.037215) 6 7 # gmtime([secs]) 和localtime()方法相似,gmtime()方法是將一個時間戳轉換爲UTC時區(0時區)的struct_time。 8 9 # mktime(t) : 將一個struct_time轉化爲時間戳。 10 print(time.mktime(time.localtime()))#1473525749.0 11 12 13 # strftime(format[, t]) : 把一個表明時間的元組或者struct_time(如由time.localtime()和 14 # time.gmtime()返回)轉化爲格式化的時間字符串。若是t未指定,將傳入time.localtime()。若是元組中任何一個 15 # 元素越界,ValueError的錯誤將會被拋出。 16 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56 17 18 # time.strptime(string[, format]) 19 # 把一個格式化時間字符串轉化爲struct_time。實際上它和strftime()是逆操做。 20 print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X')) 21 #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, 22 # tm_wday=3, tm_yday=125, tm_isdst=-1) 23 #在這個函數中,format默認爲:"%a %b %d %H:%M:%S %Y"。
結構化的時間 ------asctime------> %a %b %d %H:%M %S %Y 串 <------ctime------ 時間戳orm
(struct_time ) %a %b %d %H:%M %S %Y 串 (timestamp) blog
1 #--------------------------按圖2轉換時間 2 # asctime([t]) : 把一個表示時間的元組或者struct_time表示爲這種形式:'Sun Jun 20 23:21:05 1993'。 3 # 若是沒有參數,將會將time.localtime()做爲參數傳入。 4 print(time.asctime())#Sun Sep 11 00:43:43 2016 5 6 # ctime([secs]) : 把一個時間戳(按秒計算的浮點數)轉化爲time.asctime()的形式。若是參數未給或者爲 7 # None的時候,將會默認time.time()爲參數。它的做用至關於time.asctime(time.localtime(secs))。 8 print(time.ctime()) # Sun Sep 11 00:46:38 2016 9 print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016 10 11 12 1 #--------------------------其餘用法 13 2 # sleep(secs) 14 3 # 線程推遲指定的時間運行,單位爲秒。
1 #時間加減 2 import datetime 3 4 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 5 #print(datetime.date.fromtimestamp(time.time()) ) # 時間戳直接轉成日期格式 2016-08-19 6 # print(datetime.datetime.now() ) 7 # print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天 8 # print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天 9 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時 10 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分 11 12 13 # 14 # c_time = datetime.datetime.now() 15 # print(c_time.replace(minute=3,hour=2)) #時間替換 16 17 datetime模塊
import time字符串
# print(time.time())
# print(time.strftime('%Y-%m-%d %X'))
# print(time.localtime())
# print(time.gmtime()) #UTC
# print(time.localtime().tm_mon)string
# print(time.localtime(123123123))
# print(time.gmtime(123123123))
# print(time.mktime(time.localtime()))
# print(time.strftime('%Y',time.gmtime()))form
# print(time.strptime('2017-03-01','%Y-%m-%d')) # '2017-03-01' 結構化時間格式
#當前時間 ctime asctime 傳參後 時間格式結構轉化
# print(time.ctime(12312312))
# print(time.asctime(time.gmtime()))
1 1571210591.6277668 2 2019-10-16 15:23:11 3 time.struct_time(tm_year=2019, tm_mon=10, tm_mday=16, tm_hour=15, tm_min=23, tm_sec=11, tm_wday=2, tm_yday=289, tm_isdst=0) 4 time.struct_time(tm_year=2019, tm_mon=10, tm_mday=16, tm_hour=7, tm_min=23, tm_sec=11, tm_wday=2, tm_yday=289, tm_isdst=0) 5 10
6 time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0) 7 time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=0, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0) 8 1571210591.0 9 2019 10 time.struct_time(tm_year=2017, tm_mon=3, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=60, tm_isdst=-1)
11 Sat May 23 20:05:12 1970 12 Wed Oct 16 07:23:11 2019