Python time module Learning
Time package
The basic type of the time package is struct_time.
Time. sleep () delays the program, in seconds.
Time. time () returns the timestamp, float type.
Time. strptime (string, format)
Conversion string to struct_time, returns the time structure struct_time
Time_struct = time. strptime ('2017-10-10 22:22:12 ', "% Y-% m-% d % H: % M: % S ")
Time. strftime (format, struct_time)
Struct_time is converted to a string and returns a string.
Time. strftime ("% Y-% m-% d", struct_time)
Time. mktime (time_struct)
Return the timestamp in floating point format.
Time. gmtime (time_stamp)
Converts the timestamp into a time structure and returns the time structure.
Time. gmtime (1812950932)
Time. localtime ()
Converts the timestamp into a time structure and returns the time structure.
Time. localtime ()
Gmtime () and loaltime () both convert timestamp into a time structure. The difference is that gmtime is converted to Greenwich Mean Time, And localtime is converted to local time. For example, we are Beijing time. Therefore, parameters are not added to time. localtime (). By default, the current timestamp is obtained and converted to the current time.
Summary
String to time_struct with time. strptime ()
Time_struct to the string using time. strftime ()
Timestamp to time_struct with time. gmtime (time_stamp) or time. localtime (time_stamp) time_stamp can be omitted
Time_struct to timestamp using time. mktime (time_struct)
We can see that there is no direct Conversion Relationship Between the string and the timestamp, and the time structure can be used as the transition.
Diagram:
Although time. ctime () also converts the timestamp into a string, it cannot be customized by formatting characters and is of limited use. For example
The returned result of time. ctime (1212950932) is Mon Jun 9 02:48:52 2008.
Time. asctime (time_struct) is also a time struct, which is converted to a string. The format of the generated string is the same as that of ctime and cannot be customized. For example
The returned result of time. asctime (time_struct) is Mon Jun 14 05:28:52 2027.