Time module in Python

Source: Internet
Author: User
Tags month name

Http://qinxuye.me/article/details-about-time-module-in-python/

In common code, we often need to deal with time. In python, time processing-related modules include time, datetime, and calendar. This article mainly describes the time module.

Before you begin, you must first describe these points:

  1. In python, these methods are usually used to represent the time: 1) timestamp 2) formatted time string 3) there are nine elements in the tuples (struct_time. Since the time module of Python mainly calls the C library, different platforms may be different.
  2. UTC (Coordinated Universal Time) is the Greenwich Mean Time, the world standard time. UTC + 8 in China. DST (Daylight Saving Time) is the daylight saving time.
  3. Timestamp: Generally, the timestamp indicatesJanuary 1, 1970 00:00:00The offset that starts to be calculated in seconds. We run "type (Time. Time ()" and return the float type. Functions that return timestamp mainly include time () and clock.
  4. _Time: there are nine elements in struct_time. The main functions that return struct_time include gmtime (), localtime (), and strptime (). The following lists the elements in this method:

 

Index) Attribute) Value (values)
0 Tm_year (year) For example, 2011
1 Tm_mon (month) 1-12
2 Tm_mday) 1-31
3 Tm_hour (hour) 0-23
4 Tm_min (points) 0-59
5 Tm_sec (seconds) 0-61
6 Tm_wday (weekday) 0-6 (0 indicates Sunday)
7 Tm_yday (the day of the year) 1-366
8 Tm_isdst (whether it is timeout) The default value is-1.

Next we will introduce several common functions in the time module:

1)Time.Localtime ([secs]): Converts a timestamp to struct_time of the current time zone. If the secs parameter is not provided, the current time prevails.

>>> Time. localtime ()
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 14, tm_min = 14, tm_sec = 50, tm_wday = 3, tm_yday = 125, tm_isdst = 0)
>>> Time. localtime (1304575584.1361799)
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 14, tm_min = 6, tm_sec = 24, tm_wday = 3, tm_yday = 125, tm_isdst = 0)

2)Time.Gmtime ([Secs]): Similar to the localtime () method, the gmtime () method converts a timestamp to struct_time of the UTC time zone (0 time zone.

>>> Time. gmtime ()
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 6, tm_min = 19, tm_sec = 48, tm_wday = 3, tm_yday = 125, tm_isdst = 0)

3)Time.Time (): Returns the timestamp of the current time.

>>> Time. Time ()
1304575584.1361799

4)Time.Mktime (T): Converts a struct_time to a timestamp.

>>> Time. mktime (time. localtime ())
1304576839.0

5)Time.Sleep (Secs): The thread delays running at a specified time. The Unit is seconds.

6)Time.Clock (): It should be noted that on different systemsDifferent meanings. On UNIX systems, it returns "process time", which is a floating point number (timestamp) expressed in seconds ). In Windows, the first call returns the actual running time of the process. The second call is the running time since the first call. (In fact, it is based on queryperformancecounter () on Win32, which is more accurate than millisecond)

?
12345678 import time  if __name__ == '__main__'    time.sleep(1    print "clock1:%s" % time.clock()      time.sleep(1    print "clock2:%s" % time.clock()      time.sleep(1    print "clock3:%s" % time.clock()

Running result:

Clock1: 3.35238137808e-006
Clock2: 1.00004944763
Clock3: 2.00012040636

The first clock () outputs the program running time.
The second and third clock () Outputs are the time interval from the first clock

7)Time.Asctime ([T]): Represents a time tuples or struct_time in this form:'Sun Jun 20 23:21:05 123'. If no parameter exists, time. localtime () is passed in as the parameter.

>>> Time. asctime ()
'Thu May 5 14:55:43 123'

8)Time.Ctime ([Secs]): Converts a timestamp (floating point number calculated in seconds) to time. asctime. If the parameter is not provided or is none, the default time. Time () is used as the parameter. It is equivalent to time. asctime (time. localtime (SECs )).

>>> Time. ctime ()
'Thu May 5 14:58:09 123'
>>> Time. ctime (Time. Time ())
'Thu May 5 14:58:39 123'
>>> Time. ctime (1304579615)
'Thu May 5 15:13:35 123'

9)Time.Strftime (Format[,T]): Convert a time string or struct_time (such as returned by time. localtime () and time. gmtime () that represents time. If t is not specified, time. localtime () is passed in (). If any element in the tuples crosses the border, the valueerror will be thrown.

Format Description Remarks
% Local (locale) Simplified week name  
% Local full week name  
% B Simplified local month name  
% B Local full month name  
% C Local Date and Time Representation  
% D The day of the month (01-31)  
% H The hour of the day (in 24-hour format, 00-23)  
% I Hour (in 12-hour format, 01-12)  
% J Day of the year (001-366)  
% M Month (01-12)  
% M Minutes (00-59)  
% P The identifier of the local am or PM. I
% S Seconds (01-61) II
% U The number of weeks in a year. (00-53 Sunday is the beginning of a week .) All days before the first Sunday are placed in week 0th. 3.
% W The day of a week (0-6, 0 is Sunday) 3.
% W Similar to % u, % W starts from Monday as a week.  
% X Local date  
% X Local time  
% Y Remove the year of the century (00-99)  
% Y Complete year  
% Z Name of the time zone (if it does not exist, it is a null character)  
% '%' Character  

Remarks:

  1. "% P" is effective only when used with "% I.
  2. The article emphasizes that it is indeed 0-61, rather than 59, leap second occupies two seconds (SWEAT ).
  3. When the strptime () function is used, % u and % W are calculated only when the number of weeks and days in the year are determined.

For example:

>>> Time. strftime ("% Y-% m-% d % x", time. localtime ())
'2017-05-05 16:37:06'

10)Time. Strptime (STring[,Format]): Converts a formatted time string to struct_time. In fact, it and strftime () are inverse operations.

>>> Time. strptime ('2017-05-05 16:37:06 ',' % Y-% m-% d % x ')
Time. struct_time (tm_year = 2011, tm_mon = 5, tm_mday = 5, tm_hour = 16, tm_min = 37, tm_sec = 6, tm_wday = 3, tm_yday = 125, tm_isdst =-1)

In this function, the default format is:"% A % B % d % H: % m: % S % Y".

Finally, let's summarize the time module. According to the previous descriptions, there are three expressions in Python: 1) timestamp 2) tuple or struct_time 3) format the string.

Conversion between them:

For more information, see the official documents of the time module.

Part of reference: http://qlj.sh.cn/python/20100402/python-time/

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.