Common methods in Python's Time module

Source: Internet
Author: User
Tags manual function prototype sleep in python

This article mainly introduces the Python time module in the common method of collation, the timing module is dedicated to the processing of the date of the module, the need for friends can refer to the

In the development of the application process, it is inevitable to deal with the date, time processing. Such as: Record the execution time of a complex algorithm, the delay of packets in network communication, and so on. The Python provides time, DateTime calendar, and other modules to handle the date, and today is an introduction to several of the most commonly used functions in the timing module.

Time.time

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

Time.sleep

You can suspend the current process by calling Time.sleep. Time.sleep receives a floating-point parameter indicating when the process hangs.

Time.clock

On the Windows operating system, Time.clock () returns the number of seconds that the method was first called to now, with a precision greater than 1 microseconds. You can use this function to record when a program executes. The following is a simple example:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 Import time Print time.clock () #1 time.sleep (2) print time.clock () #2 time.sleep (3) Print time.clock () #3 #---- #3.91111160776e-06 #1.99919151736 #4.99922364435

Time.gmtime

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

?

1 2 3 4 5 6 7 8 9 10 11 Import time Print time.gmtime () #获取当前时间的struct_time对象 print time.gmtime (Time.time ()-* * *) #获取昨天这个时间的struct_tim E Object #----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_yd ay=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 viewed as a localized version of Gmtime ().

Time.mktime

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

?

1 2 3 4 5 Import time #下面两个函数返回相同 (or similar) results 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 parameter format is a format string (the knowledge of the format string can refer 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:

?

1 2 3 4 5 6 7 8 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 a time in the specified format, returning the Struct_time object. The function prototype is: Time.strptime (string, format), two parameters are strings, and here is a simple example of parsing a string into a Struct_time object:

?

1 2 3 4 5 6 Import time Print time.strptime (' 2009-06-23 15:30:53 ', '%y-%m-%d%h:%m:%s ') #----result #time. Struct_time (tm_year=200 9, 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 method described above is one 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.

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.