Python3 the time module
Time & datetime & Calendar
I. Overview
Python provides many ways to handle dates and times, and converting date formats is a common feature.
Time tuples : Many Python functions use a single element to assemble 9 sets of digital processing time.
Field |
Property |
Value |
4-digit number of years |
Tm_year |
2017 |
Month |
Tm_mon |
1 to 12 |
Day |
Tm_mday |
1 to 31 |
Hours |
Tm_hour |
0 to 23 |
Minutes |
Tm_min |
0 to 59 |
Seconds |
Tm_sec |
0 to 61 (60 or 61 is run seconds) |
The first day of the week |
Tm_wday |
0 to 6 (0 is Monday) |
The first day of the year |
Tm_yday |
1 to 366, the day ordinal of a year |
Daylight saving time |
Tm_isdst |
Is daylight saving time, the value is 1 o'clock daylight saving time, the value is 0 o'clock is not daylight saving time, default is-1 |
#从返回浮点数的时间辍方式向时间元组转换, simply pass the floating-point number to a function such as localtime
>>> Print (Time.localtime (Time.time ()))
Time.struct_time (tm_year=2018, tm_mon=5, tm_mday=18, tm_hour=18, tm_min=40, tm_sec=58, tm_wday=4, tm_yday=138, tm_ isdst=0)
#当前时间为: 2018, May, 18th, 18 o'clock, 40 minutes, 58 seconds, Thursday, 138 days of the year, not daylight saving time
python Medium Time date formatting symbols:
1 |
%y |
Two-digit year representation (00-99) |
2 |
%Y |
A four-digit year representation (000-9999) |
3 |
%m |
Month (01-12) |
4 |
%d |
Day of the Month (0-31) |
5 |
%H |
24-hour hours (0-23) |
6 |
%I |
12-hour hours (01-12) |
7 |
%M |
Number of minutes (00=59) |
8 |
%s |
Seconds (00-59) |
9 |
%a |
Local Simplified Week name |
10 |
%A |
Local Full week name |
11 |
%b |
Locally Simplified month name |
12 |
%B |
Local Full month name |
13 |
%c |
Local corresponding date representation and time representation |
14 |
%j |
Day of the Year (001-366) |
15 |
%p |
equivalent of local a.m. or p.m. |
16 |
%u |
Week of the Year (00-53) Sunday is the beginning of the week |
17 |
%w |
Week (0-6), Sunday for the beginning of the week |
18 |
%W |
Week of the Year (00-53) Monday is the beginning of the week |
19 |
%x |
Local corresponding date representation |
20 |
%x |
Local corresponding time representation |
21st |
%Z |
The name of the current time zone |
22 |
%% |
% number itself |
two. Time Module
Time-related operations, time has three kinds of means:
- Timestamp after January 1, 1970 seconds, i.e.: Time.time ()
- Formatted string 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')
- Structured time tuples include: year, day, week, etc. time.struct_time is: time.localtime ()
Time.time ()
1 2 3 4 5 |
Returns the timestamp of the current time (the number of floating-point seconds elapsed after the 1970 era). Timestamp units are best suited for date operations. But the dates before 1970 cannot be expressed in this way. Too far away from the date, UNIX and Windows only support to 2038 years. >>> Import Time >>> print (Time.time ()) 1459999336.1963577 |
Time.mktime (Tupletime)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Accepts the time tuple and returns the time suffix (the number of floating-point seconds elapsed after the 1970 era). The Python time.mktime () function performs the opposite of Gmtime (), localtime (), which receives the Struct_time object as a parameter and returns a floating-point number that represents the time in seconds. If the value entered is not a valid time, overflowerror or valueerror will be triggered. #!/usr/bin/python3 Import time t = (2016, 2, 17, 17, 3, 38, 1, 48, 0) secs = Time.mktime (t) Print ("Time.mktime (t):%f"% secs) Print ("Asctime (localtime (secs)):%s"% Time.asctime (time.localtime (secs))) The result of the above example output is: Time.mktime (t): 1455699818.000000 Asctime (localtime (secs)): Wed Feb 17 17:03:38 2016 |
Time.gmtime ([secs])
1 2 3 4 |
Receives the time suffix (the number of floating-point seconds elapsed after the 1970 era) and returns the time-tuple T in Greenwich Astronomical time. Note: T.TM_ISDST is always 0 >>> Import Time >>> print ("Gmtime:", Time.gmtime (1455508609.34375)) Gmtime:time.struct_time (tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, Tm_yday=46, TM _isdst=0) |
Time.localtime ([secs])
1 2 3 4 |
The Receive time suffix (the number of floating-point seconds after the 1970 era) and the time-of-day tuple T (t.tm_isdst 0 or 1, depending on whether the local time is daylight saving). >>> Import Time >>> Print ("LocalTime ():", Time.localtime (1455508609.34375)) LocalTime (): Time.struct_time (tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=11, tm_min=56, tm_sec=49, tm_wday=0, Tm_yday =46, tm_isdst=0) |
Time.mktime (Tupletime)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Accepts the time tuple and returns the time suffix (the number of floating-point seconds elapsed after the 1970 era). The Python time Mktime () function performs the opposite of Gmtime (), localtime (), which receives the Struct_time object as a parameter and returns a floating-point number that represents the times in seconds. If the value entered is not a valid time, overflowerror or valueerror will be triggered. #!/usr/bin/python3 Import time t = (2016, 2, 17, 17, 3, 38, 1, 48, 0) secs = Time.mktime (t) Print ("Time.mktime (t):%f"% secs) Print ("Asctime (localtime (secs)):%s"% Time.asctime (time.localtime (secs))) The result of the above example output is: Time.mktime (t): 1455699818.000000 Asctime (localtime (secs)): Wed Feb 17 17:03:38 2016 |
Time.strftime (Fmt[,tupletime])
1 2 3 4 5 |
Receives a time tuple and returns the local time as a readable string, formatted as determined by the FMT. >>> Import Time >>> Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) 2016-04-07 11:18:05 |
Time.strptime (str,fmt= '%a%b%d%h:%m:%s%Y ')
1 2 3 4 5 |
Resolves a time string to a time tuple according to the format of the FMT. >>> Import Time >>> struct_time = Time.strptime ("Nov", "%d%b%y") >>> print ("Return to Tuple:", Struct_time) Return tuples: Time.struct_time (tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_ ISDST=-1) |
Time.altzone
1 2 3 4 |
Returns the number of seconds to offset the daylight saving time region in West Greenwich. If the region returns negative values in eastern Greenwich (such as Western Europe, including the UK). Available for daylight saving time enabled regions. >>> Import Time >>> print ("Time.altzone%d"% time.altzone) time.altzone-28800 |
Time.asctime ([Tupletime])
1 2 3 4 5 |
Accepts a time tuple and returns a read-only string of 24 characters in the form "Tue Dec 11 18:07:14 2008" (December 11, 2008 Tuesday 18:07 14 seconds). >>> Import Time >>> t = time.localtime () >>> Print ("Time.asctime (t):%s"% time.asctime (t)) Time.asctime (t): Thu APR 7 10:36:20 2016 |
Time.clock ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
The number of seconds used to calculate the floating-point count returns the current CPU time. It is more useful than time.time () to measure the time spent on different programs. #!/usr/bin/python3 Import time def procedure (): Time.sleep (2.5) # Time.clock T0 = Time.clock () Procedure () Print (Time.clock ()-T0) # Time.time T0 = Time.time () Procedure () Print (Time.time ()-T0) The result of the above example output is: 5.000000000000143e-05 2.5020556449890137 |
Time.ctime ([secs])
1 2 3 4 |
function equivalent to Asctime (localtime (secs)), parameter is not given equal to Asctime () >>> Import Time >>> Print ("Time.ctime ():%s"% time.ctime ()) Time.ctime (): Thu APR 7 10:51:58 2016 |
Time.sleep (secs)
1 2 3 4 5 6 7 |
Postpone the call thread's run, secs refers to the number of seconds. #!/usr/bin/python3 Import time Print ("Start:%s"% time.ctime ()) Time.sleep (5) Print ("End:%s"% time.ctime ()) |
Time.tzset ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 |
Re-initializes time-related settings according to the environment variable TZ. Standard TZ environment variable format: STD offset [DST [offset [, Start[/time], End[/time]] Parameters: STD and DST: abbreviated letters of three or more times. Passed to Time.tzname. Offset: Offsets from UTC, Format: [+|-]hh[:mm[:ss]] {h=0-23, m/s=0-59}. Start[/time], End[/time]: date when DST begins to take effect. The format is m.w.d-, which represents the month, week, and date of the date. W=1 refers to the first week of the month, while W=5 refers to the last week of the month. ' Start ' and ' end ' can be in one of the following formats: Jn: Julian Day N (1 <= n <= 365). Leap Year Day (February 29) is not counted. N: Julian Day (0 <= n <= 365). Leap Year Day (February 29) counted MM.N.D: The month, week number, and date of the date. W=1 refers to the first week of the month, while W=5 refers to the last week of the month. Time: (optional) When DST begins to take effect (24-hour system). The default value is 02:00 (the local time for the specified time zone). #!/usr/bin/python3 Import time Import OS os.environ[' TZ '] = ' est+05edt,m4.1.0,m10.5.0 ' Time.tzset () Print (Time.strftime ('%x%x%Z ')) os.environ[' TZ '] = ' aest-10aedt-11,m10.5.0,m3.5.0 ' Time.tzset () Print (Time.strftime ('%x%x%Z ')) The result of the above example output is: 23:25:45 04/06/16 EDT 13:25:45 04/07/16 AEST |
Time.timezone
1 |
The attribute Time.timezone is the number of seconds (>0, Americas; <=0 most of Europe, Asia, and Africa) from Greenwich in the local time zone (daylight saving is not started). |
Time.tzname
1 |
The Time.tzname property contains a pair of different strings depending on the situation, which is the local time zone name with daylight savings, and No. |
Time Change chart:
Python3 Time Module