Python time module Learning

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.