Python time module usage example

  • 2020-04-02 14:06:02
  • OfStack

This article details the use of python's embedded time module. Share with you for your reference. Specific analysis is as follows:
 
A list,

The time module provides functions for various operation times

Generally, there are two ways to express time:

The first is the timestamp approach (the offset in seconds relative to 1970.1.1 00:00:00), where the timestamp is unique

The second is represented as an array (struct_time), with nine elements, each representing a struct_time with the same timestamp that is different depending on the time zone

Year (four digits, e.g. 1998)
The month (1-12)
Day (1-31)
Hours (0-23)
59 minutes (0 -)
59 seconds (0 -)
Weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) is Daylight saving Time
If the DST flag is 0, the time is given in the regular time zone;
If it is 1, the time is given in the DST time zone;
If it is -1, mktime() should guess based on the date and time.
     
Second, function introduction

1. The asctime ()

Asctime ([tuple]) > The string
Converts a struct_time, which defaults to the time, to a string
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
Is 2.
             
2. The clock ()

Clock () > Floating point number
This function has two functions,
On the first call, the actual time the program ran is returned;
With a call after the second, the interval between the first call and this call is returned

Example:


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

Output:


clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

The first clock outputs the running time of the program
The second and third clocks output the time interval with the first clock
 
3. Sleep (...).

Sleep (seconds)
The thread runs later than the specified time, tested in seconds, but the following sentence in the help document is not clear
"The argument may be a floating point number for subsecond precision."

4. The ctime (...).

Ctime (seconds) > The string
Converts a timestamp (the default is the current time) to a time string
Such as:
Time. Ctime ()
Output: 'Sat Mar 28 22:24:24 2009'
             
5. Gmtime (...).

Gmtime ([seconds]) > (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)

Converts a timestamp to a UTC time zone (time zone 0) struct_time, with the current time as the conversion criterion if the seconds parameter is not entered

6. A localtime (...).

The localtime ([seconds]) - > (tm_year tm_mon, tm_day tm_hour, tm_min, tm_sec, tm_wday and tm_yday, tm_isdst)

Converts a timestamp to a struct_time of the current time zone, with the current time as the conversion criterion if the seconds parameter is not entered

7. Mktime (...).

Mktime (a tuple) > Floating point number
Converts a struct_time to a timestamp
             
8. Strftime (...).

The strftime (format [, tuple]) - > The string
Outputs the specified struct_time(the default is the current time) according to the specified formatted string
Time and date formatting symbol in python:
The two-digit year of %y is (00-99).
The year of %Y with four digits (000-9999)
Month (01-12)
%d day of the month (0-31)
%H 24-hour hours (0-23)
%I 12 hours (01-12)
%M minutes (00=59)
%S seconds (00-59)

%a locally simplified week name
%A local full week name
%b local simplified month name
%B local full month name
%c local corresponding date and time representations
%j one day in the year (001-366)
Local A.M. or P.M. equivalent of %p
The number of weeks of the year (00-53) Sunday is the beginning of the week
W week (0-6), Sunday is the beginning of the week
The number of weeks in a year (00-53) Monday is the beginning of the week
%x is represented locally by the corresponding date
%X local corresponding time representation
%Z the name of the current time zone
The %% % sign itself  
     
9. Strptime (...).

Strptime (string format) > struct_time

The time to convert a time string into an array based on the specified formatter
Such as:
The 2009-03-20 11:45:39   The corresponding format string is :% Y-%m-%d %H:% m :%S
The corresponding format string for Sat Mar 28 22:24:24 2009 is :% a %b %d %H:%M:%S %Y
     
10. Time (...).

The time ()> Floating point number
Returns the timestamp of the current time

Third, suspects

1. The daylight saving time

In struct_time, daylight saving time doesn't seem to work, for example
A = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
B = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
A and b represent daylight saving time and standard time, respectively. The conversion to timestamp between them should be correlated with 3600, but the output after conversion is 646585714.0
 
Four, small applications

Python gets the current time

Time. time() gets the current timestamp
Time. Localtime () struct_time form of the current time
Time. Ctime () the string form of the current time
   
Python formats strings  

Format into 2009-03-20 11:45:39


time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 

Format as Sat Mar 28 22:24:24 2009


time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 

 
3. Convert the format string to a timestamp


a = "Sat Mar 28 22:24:24 2009"
b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

I believe that this article has some reference value for your Python programming.


Related articles: