When it comes to time, we need to use the time module. Before using the module, you should first import the module.
# Common Methods 1. Time.sleep (secs) (thread) Delays the specified time run. Unit is seconds. 2. Time.time () Gets the current timestamp
Three ways to represent time
In Python, we typically have three ways to represent time: a timestamp, a formatted time tuple (struct_time), and a 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 zonePercent% of the number itself
python format symbols in time date
(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 |
First, let's start by importing the time module to recognize the several formats in Python that represent times:
#Import Time Module>>>Import Time#time Stamp>>>time.time ()1500875844.800804#Time String>>>time.strftime ("%y-%m-%d%x")'2017-07-24 13:54:37'>>>time.strftime ("%y-%m-%d%h-%m-%s")'2017-07-24 13-55-04'#time tuple: localtime converts a timestamp to the struct_time of the current time zonetime.localtime () time.struct_time (Tm_year=2017, tm_mon=7, tm_mday=24, Tm_hour=13, tm_min=59, tm_sec=37, Tm_wday=0, tm_yday=205, tm_isdst=0)
Summary: Time stamp is the time that the computer can recognize; The time string is the time that a person can read; Tuples are used to manipulate time.
#Timestamp-- structured time#time.gmtime (timestamp) #UTC时间, consistent with local time in London, UK#time.localtime (timestamp) #当地时间. For example, we are now implementing this method in Beijing: 8 hour difference from UTC time, UTC time + 8 hour = Beijing Time>>>time.gmtime (1500000000) 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)>>>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)#structured time-time stamping#Time.mktime (structured time)>>>time_tuple = Time.localtime (1500000000)>>>time.mktime (time_tuple)1500000000.0
#structured Time--string time#time.strftime ("format definition", "structured Time") if the structured time parameter does not pass, the actual current time>>>time.strftime ("%y-%m-%d%x")'2017-07-24 14:55:36'>>>time.strftime ("%y-%m-%d", Time.localtime (1500000000))'2017-07-14'#string Time--structured time#Time.strptime (Time string, string corresponding format)>>>time.strptime ("2017-03-16","%y-%m-%d") 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)>>>time.strptime ("07/24/2017","%m/%d/%y") time.struct_time (tm_year=2017, Tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, Tm_isdst=-1)
#structured Time--%a%b%d%h:%m:%s%y string#Time.asctime (structured time) if the parameter is not passed, the format string of the current time is returned directly>>>time.asctime (Time.localtime (1500000000))'Fri Jul 10:40:00'>>>time.asctime ()'Mon Jul 15:18:33'#Timestamp- %a%d%d%h:%m:%s%y string#Time.ctime (timestamp) If the parameter is not passed, the format string of the current time is returned directly>>>time.ctime ()'Mon Jul 15:19:07'>>>time.ctime (1500000000)'Fri Jul 10:40:00'
ImportTimetrue_time=time.mktime (Time.strptime ('2017-09-11 08:30:00','%y-%m-%d%h:%m:%s')) Time_now=time.mktime (Time.strptime ('2017-09-12 11:00:00','%y-%m-%d%h:%m:%s')) Dif_time=time_now-True_timestruct_time=time.gmtime (dif_time)Print('%d months%d%d days%d minutes%d seconds'% (struct_time.tm_year-1970,struct_time.tm_mon-1, Struct_time.tm_mday-1, Struct_time.tm_hour, struct_time.tm_min,struct_time.tm_sec))
Python Time Module