Common methods in the time module of Python

Source: Internet
Author: User

Common methods in the time module of Python

This article mainly introduces the common methods in the time module of Python. The time module is a module used to process the date and time. For more information, see

In the process of application development, 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:

?

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

 

# ---- 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:

?

1

2

3

4

5

6

7

8

9

10

11

Import time

 

Print time. gmtime ()

# Get the struct_time object of the current time

Print 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:

?

1

2

3

4

5

Import time

 

# The following two functions return the same (or similar) results.

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:

?

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

#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:

?

1

2

3

4

5

6

Import time

 

Print time. strptime ('2017-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.