Common methods in the time module of Python

Source: Internet
Author: User
This article mainly introduces the common methods in the time module of Python. The time module is a module specifically used to process the date and time. If you need it, refer to the development process of the application, it is inevitable to deal with date and time processing. For example, record the execution time of a complex algorithm and the delay of packets in network communication. Python provides time and datetime calendar modules to process time and date. Today, we will introduce the most common functions in the time module.
Time. time

The time. time () function returns the number of seconds since January 1, January 1, 1970. This is a floating point number.
Time. sleep

You can call time. sleep to suspend the current process. Time. sleep receives a floating point parameter, indicating the time when the process is suspended.
Time. clock

In windows, time. clock () returns the number of seconds since the method was called for the first time. The accuracy is higher than 1 microsecond. You can use this function to record the execution time of the program. The following is a simple example:

import time print time.clock() #1time.sleep(2)print time.clock() #2time.sleep(3)print time.clock() #3 #---- result#3.91111160776e-06#1.99919151736#4.99922364435
Time. gmtime

This function is prototype: time. gmtime ([sec]). The optional parameter sec indicates the number of seconds since January 1. The default value is time. time (). The function returns an object of the time. struct_time type. (Struct_time is the time object defined in the time module.) The following is a simple example:

Import time print time. gmtime () # obtain the struct_time object print time for the current time. gmtime (time. time ()-24*60*60) # Get the struct_time object of the time of yesterday # ---- result # time. struct_time (tm_year = 2009, tm_mon = 6, tm_mday = 23, tm_hour = 15, tm_min = 16, tm_sec = 3, tm_wday = 1, tm_yday = 174, tm_isdst = 0) # time. struct_time (tm_year = 2009, tm_mon = 6, tm_mday = 22, tm_hour = 15, tm_min = 16, tm_sec = 3, tm_wday = 0, tm_yday = 173, tm_isdst = 0) time. localtime

Time. localtime is very similar to time. gmtime. It also returns a struct_time object, which can be viewed as a localized version of gmtime.
Time. mktime

Time. mktime: opposite to gmtime () and localtime (), it receives the struct_time object as a parameter and returns a floating point number that represents the time in seconds. For example:

Import time # The following two functions return the same (or similar) Result print time. mktime (time. localtime () print time. time ()
Time. strftime

Time. strftime converts a date to a string representation. Its function prototype is time. strftime (format [, t]). The format parameter is a format string (for details about the format string, refer to time. strftime). The optional parameter t is a struct_time object. The following example converts the struct_time object to a string representation:

import time print time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())print time.strftime('Weekday: %w; Day of the yesr: %j') #---- result#2009-06-23 15:30:53#Weekday: 2; Day of the yesr: 174
Time. strptime

Parses a time string in the specified format and returns the struct_time object. The prototype of this function is time. strptime (string, format). The two parameters are strings. The following is a simple example to parse a string into a struct_time object:

import time print time.strptime('2009-06-23 15:30:53', '%Y-%m-%d %H:%M:%S') #---- result#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=30, tm_sec=53, tm_wday=1, tm_yday=174, tm_isdst=-1)

The methods described above are the most commonly used methods in the time module. Other methods and attributes are introduced in the Python Manual, such as time. timezone, time. tzname... If you are interested, refer to the time module in the Python manual.

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.