Python time module details (common function examples very good)

  • 2020-04-02 13:36:32
  • OfStack

Before we begin, here are a few points:

1. In Python, there are usually several ways to represent time: 1) timestamp 2) formatted time string 3) tuple (struct_time) 9 elements. Since Python's implementation of the time module mainly invokes the C library, it may vary from platform to platform.
2.UTC (Coordinated Universal Time) is also known as Greenwich astronomical Time and world standard Time. UTC + 8 in China. DST (Daylight Saving Time) is Daylight Saving Time.
3. Timestamp: generally, a timestamp represents an offset in seconds from 00:00:00 on January 1, 1970. We run "type(time.time())" and return a float. The functions that return the timestamp mainly include time(), clock(), etc.
4. Tuple (struct_time) : there are 9 elements in struct_time tuple, and the functions that return struct_time mainly include gmtime(), localtime(), and strptime(). Here are a few elements in this mode tuple:
Index Attribute Values (Values) 0 Tm_year (years) For example, 2011 1 Tm_mon (month) 1-12 2 Tm_mday (day) 1-31 3 Tm_hour (when) 0-23 4 Tm_min (points) 0-59 5 Tm_sec (in seconds) 0-61. 6 Tm_wday (weekday) 0-6 (0 for Sunday) 7 Tm_yday (the day of the year) 1-366. 8 Tm_isdst (is it daylight saving time) The default is 1


Then I introduce some functions commonly used in the time module:

1) time.localtime([secs]) : converts a timestamp to the struct_time of the current time zone. If the secs parameter is not provided, the current time shall prevail.


>>> 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 is a struct_time that converts a timestamp to a UTC time zone (time zone 0).

>>>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.

>>> time.time() 
1304575584.1361799

4) time.mktime(t) : converts a struct_time to a timestamp.

>>> time.mktime(time.localtime())
1304576839.0

5) time.sleep(secs) : the thread delays the specified time to run. It's in seconds.

This one needs to be noted that it has different meanings on different systems. On UNIX systems, it returns "process time," which is a floating point number (timestamp) in seconds. In WINDOWS, the first call returns the actual time the process ran. The second subsequent call is the elapsed time since the first call. (it's actually based on QueryPerformanceCounter() on WIN32, which is more accurate than the millisecond representation)


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()


Operation results:

Clock1:3.35238137808 e-006
Clock2:1.00004944763
Clock3:2.00012040636

The first clock() outputs the program running time

The second and third clock() output the time interval from the first clock

7) time.asctime([t]) : express a tuple or struct_time that represents time in this form: 'Sun Jun 20 23:21:05 1993'. If there are no parameters, time.localtime() is passed in as a parameter.


>>> time.asctime()
'Thu May 5 14:55:43 2011'

8) time.ctime([secs]) : converts a timestamp (floating point number in seconds) into the form of time.asctime(). If the parameter is not given or is None, time. Time () will be the default parameter. Its function is equivalent to time. Asctime (time. Localtime (secs)).

>>> time.ctime()
'Thu May 5 14:58:09 2011'
>>> time.ctime(time.time())
'Thu May 5 14:58:39 2011'
>>> time.ctime(1304579615)
'Thu May 5 15:13:35 2011'

9) time.strftime(format[, t]) : converts a time tuple or struct_time (such as returned by time.localtime() and time.gmtime()) into a formatted time string. If t is not specified, time.localtime() is passed in. If any element in the tuple crosses the line, the error of ValueError will be thrown.

format meaning note % a. Locales simplify the names of the week % a. Local full week name % b Locally simplified month names % B Local full month name % c The local corresponding date and time representation % d Day of the month (01-31) % H Hours of the day (24-hour system, 00-23) % I Hours (12-hour system, 01-12) % j Days of the year (001-366) % m Month (01-12) % M Minutes (00-59) % p The corresponding character of the local am or PM one % S Seconds (01-61) two The % U The number of weeks in a year. 00-53 Sunday is the beginning of the week. All the days before the first Sunday are placed in week 0. three % w. The days of the week (0-6, 0 is Sunday) three % W. The difference with %U is that %W starts the week on Monday. % x Local corresponding date % X Local corresponding time % y Remove centuries (00-99) % Y Full year % Z Name of time zone (null if not present) % % '%' character

Remark:

"%p" is effective only when used in conjunction with "%I".
The document notes that it is indeed 0-61, not 59, and that leap year seconds account for two.
When using the strptime() function, %U and %W are calculated only when the number of weeks and days in the year are determined.
For example:


>>> time.strftime("%Y-%m-%d %X", time.localtime())
'2011-05-05 16:37:06'

Strptime (string[, format]) : converts a format time string to struct_time. It's actually the inverse of strftime().


>>> time.strptime('2011-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, format defaults to "%a %b %d %H:%M:%S %Y".

Finally, let's summarize the time module. As described previously, there are three ways to express this in Python: 1) timestamp 2) tuple or struct_time 3) format a string.

The transformation between them is shown in the figure:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/2014424120618129.jpg? 201432412651 ">


Related articles: