Python Time Module details (common function examples are explained, very good) _python

Source: Internet
Author: User
Tags function examples month name sleep time interval in python

Before you begin, explain these points first:

1. In Python, there are usually several ways to represent the time: 1) time stamp 2 The formatted time string 3 tuple (struct_time) is a total of nine elements. Because Python's time module implementation mainly calls C library, each platform may be different.
2.UTC (Coordinated Universal Time, world Harmony) that is, Greenwich Astronomical time, world standard times. In China for Utc+8. DST (daylight saving time) is daylight saving time.
3. Timestamp (timestamp): Typically, the timestamp represents the offset that is counted in seconds starting from January 1, 1970 00:00:00. We run "type (Time.time ())", which returns the float type. The function that returns the Timestamp method mainly has time (), clock () and so on.
4. Tuple (Struct_time) way: The Struct_time tuple has 9 elements, the function that returns Struct_time mainly has Gmtime (), localtime (), Strptime (). Several elements in the tuple are listed below:

Indexes (Index) attributes (attribute) value (values)
0 Tm_year (year) Like 2011.
1 Tm_mon (month) 1-12
2 Tm_mday (Japan) 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 expression Sunday)
7 Tm_yday (day ordinal of the year) 1-366
8 TM_ISDST (Daylight saving Time) Default is-1


Then we 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.

Copy Code code as follows:

>>> 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 the struct_time of the UTC time zone (0 o'clock).
Copy Code code as follows:

>>>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.
Copy Code code as follows:

>>> Time.time ()
1304575584.1361799

4) Time.mktime (t): Converts a struct_time into a timestamp.
Copy Code code as follows:

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

5) Time.sleep (secs): The thread delays the specified time running. The unit is seconds.

6) Time.clock (): This needs to be noted for different meanings on different systems. 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 was running. The second call is the elapsed time since the first call. (is actually based on WIN32 QueryPerformanceCounter (), which is more accurate than milliseconds)

Copy Code code as follows:

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


Run 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 with the first clock

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

Copy Code code as follows:

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

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

>>> 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]): Converts a tuple or struct_time representing time (such as Time.localtime () and Time.gmtime () back) 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 The first hours of the day (24-hour system, 00-23)
%I Hours of the first (12-hour system, 01-12)
%j The first 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 days is the beginning of one weeks.) All days before the first Sunday are in week No. 0. Three
%w The first day of the one week (0-6,0 is Sunday) Three
%w And%u are basically the same, the difference is that the%w in Monday as the beginning of one weeks.
%x Local appropriate date
%x Local appropriate time
%y Years removed from the Century (00-99)
%Y The full year
%Z The name of the time zone (if there is no null character)
%% '% ' character

Note:

"%p" only with "%I" with the use of the effect.
The document emphasizes that it is really 0-61, not 59, leap year seconds for two seconds (Khan one).
When using the Strptime () function,%u and%w are counted only when the number of weeks and days in the year is determined.
As an example:

Copy Code code as follows:

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

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

Copy Code code as follows:

>>> 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, we make a summary of the time module. According to previous descriptions, there are three expressions in Python: 1 timestamp 2) tuple or Struct_time 3) format strings.

The transformation between them is as shown in the figure:

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.