Python3學習之路~5.2 time & datetime模組
阿新 • • 發佈:2018-12-11
time模組
時間相關的操作,時間有三種表示方式:
- 時間戳 1970年1月1日之後的秒,即:time.time()
- 格式化的字串 2014-11-11 11:11, 即:time.strftime('%Y-%m-%d')
- 結構化時間 元組包含了:年、日、星期(取值0-6, Monday is 0)等... time.struct_time 即:time.localtime()
time模組下常用方法:
asctime([tuple]) -> string(e.g. 'Sat Jun 06 16:26:11 1998')
ctime(seconds) -> string
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
mktime(tuple) -> floating point number
sleep(seconds)
strftime(format[, tuple]) -> string
strptime(string, format) -> struct_time
time() -> floating point number
常用的格式程式碼:
格式參照: %a 本地(Locale)簡化星期名稱 %A 本地完整星期名稱 %b 本地簡化月份名稱 %B 本地完整月份名稱 %c 本地相應的日期和時間表示 %d 一個月中的第幾天(01-31) %H 一天中的第幾個小時(24小時制,00-23) %I 第幾個小時(12小時制,01-12) %j 一年中的第幾天(001-366) %m 月份(01-12) %M 分鐘數(00-59) %p 本地AM或PM的相應符 %S 秒(01-61)View Code%U 一年中的星期數(00-53,星期天是一個星期的開始)第一個星期天之前的所有天數都放在第0周 %w 一個星期中的第幾天(0-6,0是星期天) %W 和%U基本相同,不同的是%W以星期一為一個星期的開始 %x 本地相應日期 %X 本地相應時間 %y 去掉世紀的年份(00-99) %Y 完整的年份 %z 時區偏移量,指示格式為+ HHMM或-HHMM的UTC / GMT的正負時差,其中H表示小時數,M表示分鐘數(-23:59 - +23:59) %Z 時區的名字(如果不存在則為空字元) %% ‘%’字元
程式碼舉例:
import time # print(help(time)) #檢視time的幫助文件 print(time.timezone) # -28800,time模組下的變數,返回difference in seconds between UTC and local standard time,-28800s=-28800/3600=-8h,即東八區,比UTC早8小時 print(time.altzone) # --32400,time模組下的變數,返回difference in seconds between UTC and local DST time,-32400s=--32400/3600=-9h,即比標準時間早9小時, # 所謂的DST(夏令時),就是利用夏季天亮得早這一自然現象,人為地將時間提前一小時,即比當地標準時間(東八區)早1小時 print(time.time()) #當前時間戳,是一個整數或浮點數,單位秒,如1529976123.6539726 # time.sleep(3) # 當前程式睡眠3秒 print(time.gmtime()) # 返回當前時間的元組,可加時間戳引數 print(time.gmtime(0)) # 返回utc時間的struct time物件格式,即一個代表1970年1月1日的元組: # 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) print(time.gmtime(1529976123.6539726)) # gmtime([seconds]) -> time tuple print(time.localtime()) # 返回本地時間的struct time物件格式, localtime([seconds]) -> time tuple print(time.clock()) # 返回處理器時間,3.3開始已廢棄 , 改成了time.process_time()測量處理器運算時間,不包括sleep時間,不穩定,mac上測不出來 print(time.asctime()) # asctime([tuple]) -> string(e.g."Tue Jun 26 09:54:52 2018") print(time.asctime(time.localtime())) # 同上,返回時間格式"Tue Jun 26 09:54:52 2018" print(time.ctime()) # ctime(seconds) -> string(e.g."Tue Jun 26 09:54:52 2018") # 日期字串 轉成 時間戳 string_struct_time = time.strptime("2018/06/26","%Y/%m/%d") #將 日期字串 轉成 struct時間物件格式 print(string_struct_time) # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=177, tm_isdst=-1) struct_stamp_time = time.mktime(string_struct_time) #將struct時間物件轉成時間戳 mktime(tuple) -> floating point number print(struct_stamp_time) # 1529942400.0 # 時間戳 轉為 字串格式 print(time.gmtime(time.time()-86640)) #將utc時間戳轉換成struct_time格式 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) # 將utc struct_time格式轉成指定的字串格式 print(time.strftime('%Y-%m-%d') ) # 預設當前時間,2018-06-26View Code
datetime模組
import datetime import time ''' datetime.date:表示日期的類。常用的屬性有year, month, day datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond datetime.datetime:表示日期時間 datetime.timedelta:表示時間間隔,即兩個時間點之間的長度 timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) strftime("%Y-%m-%d") ''' print(datetime.datetime.now()) # 當前時間,2018-06-26 10:40:16.553391 #時間加減 print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天 print(datetime.datetime.now() - datetime.timedelta(days=5)) # 當前時間-5天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分 print(datetime.date.fromtimestamp(time.time()) ) # 時間戳直接轉成日期格式 2018-06-26 c_time = datetime.datetime.now() print(c_time.replace(minute=3,hour=2)) #時間替換,2018-06-26 02:03:27.844764View Code