Python Time module Introduction and application

Source: Internet
Author: User
Tags element groups timedelta

1, where the format_string type of time and struct_time between can be converted, timestamp time stamp can be converted between struct_time, but time stamp and format time is not directly converted.

The functions that the time module contains include the ability to get current times, operation times and dates, read time from strings, and format time as strings.

Timestamp timestamp, which represents the offset that is calculated in seconds from 00:00:00 on January 1, 1970;

A function that returns a timestamp consists of a time (), clock (), and other struct_time, a total of nine element groups. Gmtime (), localtime (), strptime () format time, the formatted structure makes time more readable.

Includes custom formats and fixed formats

2. Two ways to get the current date

= Time.strftime ("%y-%m-%d"= Datetime.datetime.now (). Strftime ("% y-%m-%d") print (now_date)


3. Get the time before or after the current time

Import Datetimeimport time# Gets the current hour, the day before, the week before, the first one months Now_date=Datetime.datetime.now () print (Now_date.strftime ("%y-%m-%d%h:%s:%m") # 1 hours ago D1= Now_date-datetime.timedelta (hours=1) Print (D1.strftime ("%y-%m-%d%h:%s:%m"))# 1Days ago (1 days later +) D2= Now_date-datetime.timedelta (days=1) Print (D2.strftime ("%y-%m-%d%h:%s:%m"))


4. Calculate Program Run time

=  time.time () time.sleep (2=-start_time)

5. Time string converted to timestamp, string converted to time

 # string time-to-time rub datestr1  =  " 2015-06-06 10:10:10  "   Print (Time.strptime (datestr1,   " %y-%m-%d %h:%m:%s   "   " %y-%m-%d%h:%m: %s   -struct_ Time (localtime)-strtime (strftime)) time1  = Time.time () print (time1) print (Time.localtime (time1)) print (Time.strftime (  "  %  

6, Time.clock () function Introduction at the time of the first call, the actual time of the program running is returned;
After the second call, the time interval from the first call to the call is returned
Under the Win32 system, this function returns the real time (wall times), while the CPU time is returned under Unix/linux.

References: 79044905

Python Time module Introduction and application

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.