Time Module
In Python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string:
(1) timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.
(2) formatted time string (format string): ' 1999-12-06 '
%y Two-digit year representation (00-99)%Y Four-digit year representation (000-9999)%m Month (01-12)One day in%d months (0-31)%H 24-hour hours (0-23)%I 12-hour hours (01-12)%M minutes (00=59)%s seconds (00-59)%a local simplified week name%A Local Full week name%b Local simplified month name%B Local Full month name%c Local corresponding date representation and time representation%j Day of the Year (001-366)%p equivalent of local a.m. or P.M.%u days of the year (00-53) Sunday for the beginning of the week%w Week (0-6), Sunday for the beginning of the week%W number of weeks in the year (00-53) Monday for the beginning of the week%x Local corresponding date representation%X Local corresponding time representation%Z name of the current time zone%% %number itself python in the time date format symbol:
(3) tuple (struct_time): Struct_time A total of 9 elements total nine elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)
indexing (Index) |
Properties (Attribute) |
value (values) |
0 |
Tm_year (year) |
Like 2011. |
1 |
Tm_mon (month) |
1-12 |
2 |
Tm_mday (Sun) |
1-31 |
3 |
Tm_hour (Time) |
0-23 |
4 |
Tm_min (min) |
0-59 |
5 |
Tm_sec (SEC) |
0-60 |
6 |
Tm_wday (Weekday) |
0-6 (0 = Monday) |
7 |
Tm_yday (day ordinal of the year) |
1-366 |
8 |
TM_ISDST (whether it is daylight saving time) |
Default is 0 |
1.time time Stamp
# 1 time () timestamp Import Time Print (Time.time ()) " " results: 1515580697.744986 " "
2.strftime Time string
# 2 strftune Time string Print (Time.strftime ('%y-%m-%d')) " " results: 2018-01-10 " " Print (Time.strftime ('%y-%m-%d%h:%m:%s')) " " 2018-01-10 18:59:03 " "
3.localtime Time Tuple
# 3 localtime Time tuple Print (Time.localtime ()) " " results: Time.struct_time (tm_year=2018, Tm_mon=1, tm_mday=10, tm_hour=19, tm_min=28, tm_sec=10, tm_wday=2, tm_yday=10, tm_isdst=0)" "
4. Conversions between several formats
(1) time stamp, structured time
#(1) time stamp, structured time#time.gmtime () #UTC时间, consistent with local time in London, UK#time.localtime () #当地时间Print(Time.gmtime (1500000000))" "results: Time.struct_time (tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, TM _isdst=0)" "Print(Time.localtime (1500000000))" "time.struct_time (tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_ isdst=0)" "#two time comparison find local time 8 hours faster than UTC time
(2) time stamp with structured time
# (2) structured time , timestamp time_tuple = Time.localtime (1500000000)print(Time.mktime (time_tuple)) The #mktime function receives the Struct_time object as a parameter, returning a floating-point number that represents the time in seconds. " Result: 1500000000.0 "
(3) Structured time-string time
# (3) Structured time-string time Print (Time.strftime ('%y-%m-%d', Time.localtime (Time.time () ))) " " results: 2018-01-10 " "
(4) String time-structured time
# (4) String time-structured time # Time.strptime (Time string, string corresponding format) Print (Time.strptime ("2017-03-16","%y-%m-%d")) " " results: Time.struct_time (tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_ Isdst=-1)" "
(5)%a%b%d%h:%m:%s%y string, structured time
# (5)%a%b%d%h:%m:%s%y string, structured time # Time.asctime (structured time) if the parameter is not passed, the format string of the current time is returned directly Print (Time.asctime (Time.localtime (1500000000))) " " results: Fri Jul 10:40:00 " " Print (Time.asctime ()) " " results: Wed Jan 19:59:16 2018 " "
(6)%a%d%d%h:%m:%s%y string--structured time
# time.ctime (timestamp) If the parameter is not passed, the format string print(Time.ctime ())" result of the current time is returned directly : Wed Jan 20:01:41 2018'print(time.ctime (1500000000)) ' results: Fri Jul 10:40:00 "
Python3 a reference to the time module