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 defined 20 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