標準庫模塊中的time模塊
阿新 • • 發佈:2018-10-04
當前時間 number display ptime ont 分享 offset sleep hid
1 # Author: Sure Feng 2 3 ‘‘‘ 4 前言: 5 開始學習模塊~ 6 模塊劃分為三類:標準庫模塊、自定義模塊、第三方模塊 7 8 本次目標: 9 學習標準庫模塊中的time模塊 10 11 準備知識: 12 時間格式分三類: 13 struct_time(時間元組) 14 Fomat string(日期字符串) 15 Timestamp(時間戳) 16 ‘‘‘ 17 import time 18 # 可以通過 print(help(time)) 查閱time模塊中內容, 19 # 前提是要import time,否則報錯,NameError: name ‘hlep‘ is not definedView Code20 21 # 分割線 22 def d_line(): 23 print("==============================================") 24 25 # sleep(seconds) ,程序滯後一段seconds 26 s_time = input("how long would you sleep >>>") 27 print("it‘s time to sleep...") 28 time.sleep(int(s_time)) 29 30 # time() -> floating point number ,獲取本地時間戳31 d_line() 32 print("--獲取當前時間戳--") 33 n_time = time.time() 34 print(n_time) 35 36 # 第一類:輸入時間戳 37 print("--輸入時間戳的轉換--") 38 # 時間戳(空時輸本地)→ 對應的UTC時間元組 39 # gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) 40 gmtime_time = time.gmtime(n_time) 41 print("對應的UTC時間元組是:" , gmtime_time) 42 43 # 時間戳(空時輸本地)→ 對應的時間元組 44 # localtime([seconds]) -> 時間元組 45 local_time = time.localtime(n_time) 46 print("對應的本地時間元組是:" , local_time) 47 48 # 時間戳(空時輸本地)→ 固定格式的字符串 49 # ctime(seconds) -> string 50 print("轉為固定格式的字符串:", time.ctime(n_time))# Tue Oct 2 18:15:34 2018 51 52 53 # 第二類:輸入時間元組 54 d_line() 55 print("--輸入時間元組的轉換--") 56 # 時間元組(必須輸入)→ 時間戳 57 # mktime(tuple) -> floating point number 58 print("對應UTC時間元組的時間戳:", time.mktime(gmtime_time)) 59 print("對應本地時間元組的時間戳:", time.mktime(local_time)) 60 61 # 時間元組(空時輸本地)→ 固定格式的字符串 62 # asctime([tuple]) -> string 63 print("轉為固定格式的字符串:", time.asctime(local_time))# Tue Oct 2 18:15:34 2018 64 65 # 時間元組(空時輸本地)→ 自定義格式的字符串 66 # strftime(format[, tuple]) -> string 67 str_time = time.strftime("%Y-%m-%d ~.~ %S:%M:%H",) 68 print("轉為自定義格式的字符串:", str_time) 69 ‘‘‘ 70 %Y Year with century as a decimal number. 71 %m Month as a decimal number [01,12]. 72 %d Day of the month as a decimal number [01,31]. 73 %H Hour (24-hour clock) as a decimal number [00,23]. 74 %M Minute as a decimal number [00,59]. 75 %S Second as a decimal number [00,61]. 76 %z Time zone offset from UTC. 77 %a Locale‘s abbreviated weekday name. 78 %A Locale‘s full weekday name. 79 %b Locale‘s abbreviated month name. 80 %B Locale‘s full month name. 81 %c Locale‘s appropriate date and time representation. 82 %I Hour (12-hour clock) as a decimal number [01,12]. 83 %p Locale‘s equivalent of either AM or PM. 84 ‘‘‘ 85 86 87 # 第三類:輸入字符串形式 88 d_line() 89 print("--輸入字符串的轉換--") 90 # 字符串(空時輸本地)→按自定義格式→ 時間元組 91 # strptime(string, format) -> struct_time 92 print(time.strptime(str_time,"%Y-%m-%d ~.~ %S:%M:%H" )) 93 print(time.strptime("2016/05/12","%Y/%H/%m")) 94 95 96 d_line() 97 print(time.altzone) #返回UTC(協調世界時)與本地夏令時的時間差 --》-32400 98 print(time.timezone) #返回UTC(協調世界時)與本地時間差 --》-28800
how long would you sleep >>>3 it‘s time to sleep... ============================================== --獲取當前時間戳-- 1538537928.3964212 --輸入時間戳的轉換-- 對應的UTC時間元組是: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=3, tm_min=38, tm_sec=48, tm_wday=2, tm_yday=276, tm_isdst=0) 對應的本地時間元組是: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=11, tm_min=38, tm_sec=48, tm_wday=2, tm_yday=276, tm_isdst=0) 轉為固定格式的字符串: Wed Oct 3 11:38:48 2018 ============================================== --輸入時間元組的轉換-- 對應UTC時間元組的時間戳: 1538509128.0 對應本地時間元組的時間戳: 1538537928.0 轉為固定格式的字符串: Wed Oct 3 11:38:48 2018 轉為自定義格式的字符串: 2018-10-03 ~.~ 48:38:11 ============================================== --輸入字符串的轉換-- time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=11, tm_min=38, tm_sec=48, tm_wday=2, tm_yday=276, tm_isdst=-1) time.struct_time(tm_year=2016, tm_mon=12, tm_mday=1, tm_hour=5, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=336, tm_isdst=-1) ============================================== -32400 -28800
標準庫模塊中的time模塊