SoFunction
Updated on 2024-11-16

Explanation of some key uses of the time module in Python programming

python time module is not difficult, is the relationship between the conversion of a little old can not remember, first look at the following chart can illustrate the relationship between several time objects. For reference understanding.

 (984×445)

  • The thin black arrow indicates the input value, the parameter
  • Bold dark yellow arrows indicate the return value, output format
  • Green circles indicate various types of objects
  • The blue boxes indicate specific methods (import time first, then use the methods in the time module).

():Get the timestamp of the current time

(): accepts a timestamp and converts it to a tuple of the current time. If you don't give it a parameter, you will pass () as a parameter by default. localtime returns the time in tuple format, and there is a similar function called gmtime(), the difference between the 2 functions is the time zone, which will be described below.

>>> ()
'Wed Jan 18 19:54:12 2016'
>>> ()
(2016, 1, 18, 19, 54, 19, 2, 100, 1)
 surname Nian,moon,date,hour,ingredient,unit of angle or arc equivalent one sixtieth of a degree,weekly,surname Nian中的第几date,hour区标识.
>>> ()
(2016, 1, 18, 23, 54, 25, 2, 100, 0)

 (363×414)

  • ():In contrast to (), it converts a time tuple into a timestamp (this must be given a parameter).
  • (): represents a time tuple as "Sun Jul 28 03:35:26 2013", if no parameter is given, () will be passed as a parameter by default.
  • (): converts a timestamp to a () expression, if no parameter is given it will default to passing () as a parameter.
  • (): converts a timestamp to a time tuple in UTC+0 time zone (China should be +8 time zones, a difference of 8 hours), if you don't give a parameter, you will pass () as a parameter by default. gmtime() returns the value of the 0 time zone, localtime returns the value of the current time zone.
  • (format,()): convert a time tuple to a formatted time character, if you don't give a time tuple parameter, you will pass () as a parameter by default.

For example, the time format in the web log is ('%d/%b/%Y:%X').

Returns results:

Sun Jul 28 04:37:38 2013

format:

 (363×414)

(stringtime,format): converts a time string into an array of times according to the specified formatters.
Example: ('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')
Returns results:

Copy Code The code is as follows.

time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)

(): return to the processor clock time, generally used for performance testing and benchmarking, etc., because they reflect the actual time used by the program, usually do not use this.

():Delay running for the specified time in seconds.

import time
print () # Print timestamps
print ()# Print local time tuple
print ()# Promise a time tuple in the UTC+0 time zone.
print ()#Print asctime formatted time
print (())# Convert time tuples to timestamps
print ()# Print formatting time
print ('%d/%b/%Y:%X')# Print the time format in the specified format
# Translate a time string and its format into a time tuple
print ('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')
print '%0.5f'%() # Print processor time
for i in range(100000): 
  pass
print '%0.5f'%()# Print processor time

###### run results ######

[root@localhost ~]# python 
1364028568.55
time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=4, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=1)
time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=8, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=0)
Sat Mar 23 04:49:28 2013
1364028568.0
Sat Mar 23 04:49:28 2013
23/Mar/2013:04:49:28
time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)
0.02000
0.03000