Analysis of Some Key Usage of the time module in Python Programming

Source: Internet
Author: User
Tags time zones
This article mainly introduces some Key Usage Analysis of the time module in Python programming, such as common methods such as mktime, localtime, and gmtime, for more information about the time module in python, it is not difficult to understand the relationship conversion. First, we can explain the relationship between several time objects. for reference and understanding.

  • Black arrow indicates input value, parameter
  • The thick arrow in the dark yellow indicates the return value, and the output format is
  • Green circles indicate various objects
  • The blue box indicates the specific method (first import time, In the use time module)

Time. time (): Get the timestamp of the current time

Time. localtime (): receives a timestamp and converts it to a tuples of the current time. If no parameter is specified, the time is set by default. time () is passed in as a parameter. localtime returns the time in tuple format. There is a function similar to it called gmtime (). The difference between the two functions is the time zone, which will be discussed below.

>>> time.ctime()

'Wed Jan 18 19:54:12 2016'

>>> time.localtime()

(2016, 1, 18, 19, 54, 19, 2,100, 1) year, month, day, hour, minute, second, Week, the day of the year, time zone ID.

>>> time.gmtime()

(2016, 1, 18, 23, 54, 25, 2, 100, 0)

  • Time. mktime (): opposite to time. localtime (), it converts a time tuple into a timestamp (this must be a parameter ).
  • Time. asctime (): indicates a time tuple in the format of "Sun Jul 28 03:35:26 2013". If no parameter is specified, time. localtime () is passed in as the parameter by default.
  • Time. ctime (): converts a timestamp to the expression format of time. asctime (). If the parameter is not set, time. time () is passed as the parameter by default.
  • Time. gmtime (): converts a timestamp to a timestamp in UTC + 0 (China should be a + 8 time zone, with a difference of 8 hours, if no parameter is specified, the time is set by default. time () is passed in as a parameter. Gmtime () returns the value of 0 time zone, and localtime returns the value of the current time zone.
  • Time. strftime (format, time. localtime (): converts a time tuple to a formatted time character. If the time tuple parameter is not provided, the time is set by default. localtime () is passed in as a parameter.

For example, the time format in the web log is time. strftime ('% d/% B/% Y: % x ')

Returned results:

Sun Jul 28 04:37:38 2013

Format:

Time. strptime (stringtime, format): converts a time string to an array of time based on the specified formatting character,
Example: time. strptime ('28/Jul/2013: 04: 33: 29', '% d/% B/% Y: % x ')
Returned results:

The Code is as follows:

Time. struct_time (tm_year = 2013, tm_mon = 7, tm_mday = 28, tm_hour = 4, tm_min = 33, tm_sec = 29, tm_wday = 6, tm_yday = 209, tm_isdst =-1)

Time. clock (): return the processor clock time, which is generally used for performance testing and Benchmark Testing. because they reflect the actual time used by the program, this is usually not used.

Time. sleep (): delay the specified time, in seconds.

Import timeprint time. time () # print the timestamp print time. localtime () # print the local time tuples print time. gmtime () # time tuples that promise UTC + 0 time zones print time. ctime () # print asctime format time print time. mktime (time. localtime () # convert the time tuples to the print time stamp. asctime () # print the formatting time print time. strftime ('% d/% B/% Y: % x') # print the time format in the specified format # translate the time string and its format into print time. strptime ('28/Jul/2013: 04: 33: 29', '% d/% B/% Y: % x') print' % 0.5f' % time. clock () # print the processor time for I in range (100000): passprint '% 0.5f' % time. clock () # print the processor time

###### Running result ######

[root@localhost ~]# python time1.py

1364028568.55time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=4, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=1)time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=8, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=0)Sat Mar 23 04:49:28 20131364028568.0Sat Mar 23 04:49:28 201323/Mar/2013:04:49:28time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)0.020000.03000
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.