Common methods in the time module of Python

Source: Internet
Author: User
In the application development process, it is unavoidable to deal with the date, time processing. Such as: recording the execution time of a complex algorithm, packet delay in network communication and so on. Python provides modules such as time, DateTime calendar, and so on to process the date, and today is an introduction to several of the most commonly used functions in the Times module.
Time.time

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

You can suspend the current process by calling Time.sleep. Time.sleep receives a floating-point parameter that represents the time that the process was suspended.
Time.clock

On the Windows operating system, Time.clock () returns the number of seconds that the method has been called for the first time, with an accuracy greater than 1 microseconds. You can use this function to log the time that the program executes. 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

The function prototype is: Time.gmtime ([sec]), the optional parameter SEC represents the number of seconds since 1970-1-1. Its default value is Time.time (), and the function returns an object of type Time.struct_time. (Struct_time is an object that is defined in time module to represent times), here is a simple example:

Import time Print time.gmtime () #获取当前时间的struct_time对象print time.gmtime (Time.time ()-* #获取昨天这个时间的struct_time对象 #----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 and returns a Struct_time object that can be seen as a localized version of Gmtime ().
Time.mktime

Time.mktime performs the opposite of Gmtime (), localtime (), which receives the Struct_time object as a parameter, returning a floating-point number that represents the time in seconds. For example:

Import time #下面两个函数返回相同 (or similar) result print time.mktime (Time.localtime ()) print time.time ()
Time.strftime

Time.strftime converts a date to a string representation, and its function prototype is: Time.strftime (format[, T]). The parameters format is a format string (the knowledge of the format string can be referred to: Time.strftime), and the optional parameter T is a Struct_time object. The following example converts a 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

Resolves a string representing time in the specified format, returning the Struct_time object. The function prototype is: Time.strptime (string, format), two arguments are strings, and here is a simple example that demonstrates parsing 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 some of the most commonly used methods in the time module, and other methods and properties are described in the Python manual, such as: Time.timezone, Time.tzname ... Interested friends can refer to the Python manual time module.

  • 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.