1 #author:sure Feng2 3 " "4 Preface:5 Start learning Module ~6 modules are divided into three categories: standard library module, custom module, third party module7 8 The objective of this session:9 Learn the time module in the standard library moduleTen One Preparation of Knowledge: A The time format is divided into three categories: - Struct_time (Time tuple) - Fomat string (date string) the Timestamp (timestamp) - " " - Import Time - #the contents of the time module can be viewed through print (Help time), + #The premise is to import time, otherwise error, Nameerror:name ' Hlep ' is not defined - + #Split Line A defd_line (): at Print("==============================================") - - #Sleep (seconds), program lags a seconds -S_time = input ("How long would sleep >>>") - Print("it ' s time to sleep ...") - time.sleep (int (s_time)) in - #time (), floating point number, gets the local timestamp to D_line () + Print("-- Get the current timestamp--") -N_time =time.time () the Print(n_time) * $ #First Class: Input timestampPanax Notoginseng Print("-- input timestamp conversion--") - #Timestamp (empty-time local) → corresponding UTC time Tuple the #gmtime ([seconds]) (Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday, Tm_yday, TM_ISDST) +Gmtime_time =time.gmtime (n_time) A Print("the corresponding UTC time tuple is:", Gmtime_time) the + #Timestamp (empty-time local) → corresponding time tuple - #localtime ([seconds]), time-tuple $Local_time =time.localtime (n_time) $ Print("the corresponding local time tuple is:", Local_time) - - #Timestamp (empty-time local) → fixed-format string the #CTime (seconds), string - Print("converted to a fixed-format string:", Time.ctime (N_time))#Tue Oct 2 18:15:34 2018Wuyi the - #Type II: Enter a time tuple Wu D_line () - Print("-- conversion of input time tuple--") About #time tuple (must be entered) → timestamp $ #mktime (tuple), floating point number - Print("Timestamp corresponding to the UTC time tuple:", Time.mktime (gmtime_time)) - Print("time stamp corresponding to local time tuple:", Time.mktime (local_time)) - A #time tuple (empty-time local) → fixed-format string + #asctime ([tuple]), string the Print("converted to a fixed-format string:", Time.asctime (Local_time))#Tue Oct 2 18:15:34 2018 - $ #time tuple (empty-time local) → Custom-formatted string the #strftime (format[, tuple]), string theStr_time = Time.strftime ("%y-%m-%d ~.~%s:%m:%h",) the Print("Convert to a custom formatted string:", Str_time) the " " - %Y year with century as a decimal number. in %m Month as a decimal number [01,12]. the %d day of the month as a decimal number [01,31]. the %H Hour (24-hour clock) as a decimal number [00,23]. About %M Minute as a decimal number [00,59]. the %s Second as a decimal number [00,61]. the %z time zone offset from UTC. the %a Locale ' s abbreviated weekday name. + %A Locale ' s full weekday name. - %b Locale ' s abbreviated month name. the %B Locale ' s full month name.Bayi %c Locale ' s appropriate date and time representation. the %I Hour (12-hour clock) as a decimal number [01,12]. the %p Locale ' s equivalent of either AM or PM. - " " - the the #Class III: Input string form the D_line () the Print("-- conversion of input string--") - #string (empty-time local) → by custom format → time tuple the #strptime (string, format), Struct_time the Print(Time.strptime (Str_time,"%y-%m-%d ~.~%s:%m:%h" )) the Print(Time.strptime ("2016/05/12","%y/%h/%m"))94 the the D_line () the Print(Time.altzone)#returns the time difference between UTC (Coordinated universal) and local daylight savings--" -3240098 Print(Time.timezone)#returns UTC (Coordinated Universal Time) vs. local difference--" -28800
View Code
How long would you sleep >>>3it's time to sleep ...==============================================--gets the current timestamp--1538537928.3964212--the conversion of the input timestamp--the corresponding UTC time tuple is: 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) The corresponding local time tuple is: 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) converted to a fixed-format string: Wed Oct3 11:38:48 2018==============================================--Conversion of input time tuples--Timestamp corresponding to the UTC time tuple:1538509128.0time stamp corresponding to local time tuple:1538537928.0convert to a fixed-format string: Wed Oct3 11:38:48 2018Convert to a custom formatted string:2018-10-03 ~.~ 48:38:11==============================================--Input string Conversion--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 modules in the standard library module