Python time module detailed (GO)

Source: Internet
Author: User

In the usual code, we often need to deal with time. In Python, the modules related to time processing include: Time,datetime and Calendar. This article mainly explains the time module.

Before you begin, you should first explain these points:

    1. In Python, there are usually several ways to represent time: 1) timestamp 2) formatted time string 3) tuples (struct_time) a total of nine elements. Because Python's time module implementation primarily calls the C library, each platform may be different.
    2. UTC (coordinated Universal time, world co-ordination) is GMT, world standard Time. In China for Utc+8. DST (daylight saving time) is daylight saving time.
    3. Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds starting January 1, 1970 00:00:00 . We run "type (Time.time ())" and return the float type. The function that returns the Timestamp method mainly has time (), clock () and so on.
    4. Tuple (struct_time) mode: There are 9 elements in the Struct_time tuple, and the functions that return struct_time are mainly gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:

indexing (Index) Properties (Attribute) value (values)
0 Tm_year (year) Like 2011.
1 Tm_mon (month) 1-12
2 Tm_mday (Sun) 1-31
3 Tm_hour (Time) 0-23
4 Tm_min (min) 0-59
5 Tm_sec (SEC) 0-61
6 Tm_wday (Weekday) 0-6 (0 = Sunday)
7 Tm_yday (day ordinal of the year) 1-366
8 TM_ISDST (whether it is daylight saving time) Default is-1

Then introduce several functions commonly used in the time module:

1)time. localtime ([secs]): Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time.

>>> 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 is the struct_time of converting a timestamp to the UTC time zone (0 o'clock 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 defers the specified time run. Unit is seconds.

6)time. clock (): This needs to be noted on different systems with different meanings . On a UNIX system, it returns "process time", which is a floating-point number (timestamp) in seconds. In Windows, the first call returns the actual time that the process is running. The second subsequent call is the elapsed time since the first call to the present. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)

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()

Operation Result:

clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

Where the first clock () output is the program run time
The second to third clock () output is the time interval from the first clock

7)time. asctime ([t]): A tuple or struct_time representing time is expressed in this form:' Sun June 23:21:05 1993 '. If there are no parameters, Time.localtime () will be passed in as a parameter.

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

8)time. CTime ([secs]): Converts a timestamp (floating point number in seconds) to the form of Time.asctime (). If the parameter is not given or is none, the default Time.time () is the parameter. Its function is equivalent to Time.asctime (time.localtime (secs)).

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

9)time. strftime (format[, T]): A tuple that represents time or struct_time (such as by Time.localtime () and Time.gmtime () return) into a formatted time string. If T is not specified, the Time.localtime () is passed in. If any of the elements in the tuple are out of bounds, the ValueError error will be thrown.

format meaning Notes
%a Local (locale) simplified week name
%A Local Full week name
%b Local Simplified month name
%B Local Full month name
%c Local corresponding date and time representation
%d Day of the one months (01-31)
%H Hours of the day (24-hour, 00-23)
%I Number of hours (12-hour, 01-12)
%j Day of the Year (001-366)
%m Month (01-12)
%M Number of minutes (00-59)
%p Local AM or PM's corresponding character One
%s Seconds (01-61) Two
%u The number of weeks in a year. (00-53 weeks is the beginning of one weeks.) All days before the first Sunday are placed in the No. 0 week. Three
%w The day of the one week (0-6,0 is Sunday) Three
%W and%u basically the same, the difference is%w with Monday for one weeks start.
%x Local corresponding date
%x Local corresponding time
%y The year of the century was removed (00-99)
%Y The full year
%Z The name of the time zone (if it does not exist as a null character)
%% '% ' character

Remark :

    1. 1. "%p" only works in conjunction with "%I".
    2. 2. The emphasis in the documentation is indeed 0-61, not 59, leap year seconds for two seconds (sweat one).
    3. 3. When using the Strptime () function,%u and%w are calculated only when the number of weeks and days in the year is determined.

As an example:

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

strptime (String[, format]): Converts a formatted time string to Struct_time. In fact it is inverse operation with strftime ().

>>> time.strptime (' 2011-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, format defaults to:"%a%b%d%h:%m:%s%Y".

Finally, let's take a summary of the time module. According to the previous description, there are three types of expressions in Python: 1) timestamp 2) tuple or Struct_time 3) formatted string.

The transformation between them:

Python time module detailed (GO)

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.