In common code, we often need to deal with time. In Python, time processing-related modules include time, datetime, and calendar. This article mainly describes the time module. Before you begin, you must first describe these points:
1. in Python, there are usually nine elements in the format of time: 1) timestamp 2) formatted time string 3) tuples (struct_time. Since the time module of Python mainly calls the C library, different platforms may be different.
2. UTC (Coordinated Universal Time, world coordination Time) that is, Greenwich Mean Astronomical Time, World Standard Time. UTC + 8 in China. DST (Daylight Saving Time) is the Daylight Saving Time.
3. timestamp: Generally, the timestamp indicates the offset calculated in seconds from 00:00:00, January 1, January 1, 1970. We run "type (time. time ()" and return the float type. Functions that return timestamp mainly include time () and clock.
4. _time: there are nine elements in struct_time. The main functions that return struct_time include gmtime (), localtime (), and strptime (). The following lists the elements in this method:
Index) |
Attribute) |
Value (Values) |
0 |
Tm_year (year) |
For example, 2011 |
1 |
Tm_mon (month) |
1-12 |
2 |
Tm_mday) |
1-31 |
3 |
Tm_hour (hour) |
0-23 |
4 |
Tm_min (points) |
0-59 |
5 |
Tm_sec (seconds) |
0-61 |
6 |
Tm_wday (weekday) |
0-6 (0 indicates Sunday) |
7 |
Tm_yday (the day of the year) |
1-366 |
8 |
Tm_isdst (whether it is timeout) |
The default value is-1. |
Next we will introduce several common functions in the time module:
1) time. localtime ([secs]): converts a timestamp to struct_time of the current time zone. If the secs parameter is not provided, the current time prevails.
The Code is as follows:
>>> Time. localtime ()
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 14, tm_min = 14, tm_sec = 50, tm_wday = 3, tm_yday = 125, tm_isdst = 0)
>>> Time. localtime (1304575584.1361799)
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 14, tm_min = 6, tm_sec = 24, tm_wday = 3, tm_yday = 125, tm_isdst = 0)
2) time. gmtime ([secs]): similar to the localtime () method, the gmtime () method converts a timestamp to struct_time of the UTC time zone (0 time zone.
The Code is as follows:
>>> Time. gmtime ()
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 6, tm_min = 19, tm_sec = 48, tm_wday = 3, tm_yday = 125, tm_isdst = 0)
3) time. time (): returns the timestamp of the current time.
The Code is as follows:
>>> Time. time ()
1304575584.1361799
4) time. mktime (t): converts a struct_time to a timestamp.
The Code is as follows:
>>> Time. mktime (time. localtime ())
1304576839.0
5) time. sleep (secs): The thread delays the specified time. The Unit is seconds.
6) time. clock (): it must be noted that it has different meanings on different systems. On UNIX systems, it returns "process time", which is a floating point number (timestamp) expressed in seconds ). In WINDOWS, the first call returns the actual running time of the process. The second call is the running time since the first call. (In fact, it is based on QueryPerformanceCounter () on WIN32, which is more accurate than millisecond)
The Code is as follows:
Import time
If _ name _ = '_ main __':
Time. sleep (1)
Print "clock1: % s" % time. clock ()
Time. sleep (1)
Print "clock2: % s" % time. clock ()
Time. sleep (1)
Print "clock3: % s" % time. clock ()
Running result:
Clock1: 3.35238137808e-006
Clock2: 1.00004944763
Clock3: 2.00012040636
The first clock () outputs the program running time.
The second and third clock () Outputs are the time interval from the first clock
7) time. asctime ([t]): indicates a time tuples or struct_time in the form of 'Sun Jun 20 23:21:05 123 '. If no parameter exists, time. localtime () is passed in as the parameter.
The Code is as follows:
>>> Time. asctime ()
'Thu May 5 14:55:43 123'
8) time. ctime ([secs]): converts a timestamp (floating point number calculated by second) into time. asctime () format. If the parameter is not provided or is None, the default time. time () is used as the parameter. It is equivalent to time. asctime (time. localtime (secs )).
The Code is as follows:
>>> Time. ctime ()
'Thu May 5 14:58:09 123'
>>> Time. ctime (time. time ())
'Thu May 5 14:58:39 123'
>>> Time. ctime (1304579615)
'Thu May 5 15:13:35 123'
9) time. strftime (format [, t]): put a time tuples or struct_time (such as by time. localtime () and time. gmtime () returned) is converted to a formatted time string. If t is not specified, time. localtime () is passed in (). If any element in the tuples crosses the border, the ValueError will be thrown.
Format |
Description |
Remarks |
% |
Local (locale) Simplified week name |
|
% |
Local full week name |
|
% B |
Simplified local month name |
|
% B |
Local full month name |
|
% C |
Local Date and Time Representation |
|
% D |
The day of the month (01-31) |
|
% H |
The hour of the day (in 24-hour format, 00-23) |
|
% I |
Hour (in 12-hour format, 01-12) |
|
% J |
Day of the year (001-366) |
|
% M |
Month (01-12) |
|
% M |
Minutes (00-59) |
|
% P |
The identifier of the local am or pm. |
I |
% S |
Seconds (01-61) |
II |
% U |
The number of weeks in a year. (00-53 Sunday is the beginning of a week .) All days before the first Sunday are placed in week 0th. |
3. |
% W |
The day of a week (0-6, 0 is Sunday) |
3. |
% W |
Similar to % U, % W starts from Monday as a week. |
|
% X |
Local date |
|
% X |
Local time |
|
% Y |
Remove the year of the century (00-99) |
|
% Y |
Complete year |
|
% Z |
Name of the time zone (if it does not exist, it is a null character) |
|
% |
'%' Character |
|
Note:
"% P" is effective only when used with "% I.
The article emphasizes that it is indeed 0-61, rather than 59, leap second occupies two seconds (SWEAT ).
When the strptime () function is used, % U and % W are calculated only when the number of weeks and days in the year are determined.
For example:
The Code is as follows:
>>> Time. strftime ("% Y-% m-% d % X", time. localtime ())
'2017-05-05 16:37:06'
10) time. strptime (string [, format]): converts a formatted time string to struct_time. In fact, it and strftime () are inverse operations.
The Code is as follows:
>>> Time. strptime ('2017-05-05 16:37:06 ',' % Y-% m-% d % x ')
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 16, tm_min = 37, tm_sec = 6, tm_wday = 3, tm_yday = 125, tm_isdst =-1)
In this function, the default format is: "% a % B % d % H: % M: % S % Y ".
Finally, let's summarize the time module. According to the previous descriptions, there are three expressions in Python: 1) timestamp 2) tuple or struct_time 3) format the string.
Conversion between them: