Python module introduction-time access and conversion

Source: Internet
Author: User

Introduction

Time-related functions provided by this module. Related modules include datetime and calendar. Note that not all functions are cross-platform. Most functions call functions of the same name in the c library function, so refer to the platform documentation for help.

The following describes some terms and conventions:

Epoch is the start point of time. For Unix, the ERA was in January 1, 1970. You can use time. gmtime (0) to view the start point of the time:

In [2]: time.gmtime(0)Out[2]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

For 32-bit linux systems, the time can only be processed until 2038.

>>> time.gmtime(time.time() + 786041553)Traceback (most recent call last):  File "
 
  ", line 1, in 
  
   ValueError: timestamp out of range for platform time_t
  
 

UTC is the Coordinated Universal Time (formerly Greenwich Mean Time or GMT ).

When DST is enabled, it is usually adjusted by one hour in part of the year according to local laws. Database C contains tables with local rules.

The actual function may be less accurate than the recommended one. For example, in most Unix systems, the clock "tick" is only 50 or 100 times per second.

The returned values of gmtime (), localtime (), and strptime () are sequences of 9 integers. They can be used as inputs of asctime (), mktime () and strftime, each domain has its own attribute, which is actually a _time struct. See the example above.

Time conversion: gmtime () converts the floating point time to struct_time of UTC, whereas calendar. timegm () and localtime () converts the floating point time to struct_time of local, and mktime (). In fact, calendar. timegm () and mktime () are equivalent, but the former returns an integer, and the latter returns a floating point number.

Function: Time access and conversion. Type: standard module related modules: datetime standard module. Calendar standard module. Python-dateutil downloads about 20 thousand per day. It is strongly recommended to generate and convert the time.

Generate epoch floating point number

In [30]: time.time()Out[30]: 1386057314.581948

Note that different systems have different precision. linux generally has 7 decimal places, while windows generally has 3 decimal places. The Time function has no parameters. The returned floating point number can be calculated directly:

In [31]: time.gmtime(time.time() + 786041553)Out[31]: time.struct_time(tm_year=2038, tm_mon=10, tm_mday=31, tm_hour=0, tm_min=57, tm_sec=18, tm_wday=6, tm_yday=304, tm_isdst=0)

The preceding Command reports an error on 32-bit linux because of the processing duration:

>>> time.gmtime(time.time() + 786041553)Traceback (most recent call last):  File "
 
  ", line 1, in 
  
   ValueError: timestamp out of range for platform time_t
  
 

In the above command, gmtime ([secs]) converts the floating point time to UTC _time of UTC. If no input parameter is null, time () is called to read the current time, for example:

In [32]: time.gmtime()Out[32]: time.struct_time(tm_year=2013, tm_mon=12, tm_mday=3, tm_hour=8, tm_min=8, tm_sec=46, tm_wday=1, tm_yday=337, tm_isdst=0)

The above shows the world coordination time. localtime ([secs]) can display the local time:

 In [34]: time.localtime()Out[34]: time.struct_time(tm_year=2013, tm_mon=12, tm_mday=3, tm_hour=16, tm_min=14, tm_sec=40, tm_wday=1, tm_yday=337, tm_isdst=0)

Note that dst should be set in the daylight saving time system. Asctime ([t]) is displayed in a readable format. The struct_time type returned by gmtime (), localtime (), and strptime () is converted to a readable format. If the input parameter is null, the returned result of localtime () is called. Different from the c function, it does not add line breaks at the end. Asctime does not use Locale information.

In [35]: time.asctime()Out[35]: 'Tue Dec  3 16:18:02 20

Ctime ([secs]) goes further on asctime and converts floating point numbers to a readable format, which is equivalent to asctime (localtime (secs). This function is very commonly used. Ctime does not use Locale information.

In [33]: time.ctime()Out[33]: 'Tue Dec  3 16:12:37 2013'
Sleep

Sleep (secs) is suspended for a specified number of seconds. The parameter can be an integer or floating point number. The actual stop time may be less than the request time because regular signal capturing may terminate sleep. In addition, the suspension time may be longer than the request time because System Scheduling also takes time.

In [36]: time.sleep(3)
Processor time

See http://automationtesting.sinaapp.com/blog/m_time

Struct_time class

Struct_time is the name tuples with the following structure:

Index) Attribute) Value (Values)
0 Tm_year (Year For example, 2013
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.

See http://automationtesting.sinaapp.com/blog/m_time
Time Zone

Reset the time conversion rules of the library function. Actually, it is to modify the environment variable TZ. python 2.3 and later linux support this function, which is relatively less commonly used. The format of TZ environment variables is as follows:

std offset [dst [offset [,start[/time], end[/time]]]]

STD and DST are short for time zones. Hh [: mm [: ss] indicates that the UTC time can be obtained by adding this time. The offset is in the format of HH [: MM [: SS], and the value of the Daylight Saving Time is increased by 1 hour.

Starttime, endtime indicates the interval in the daylight time format. Time is similar to offset. The default time is 02:00:00. For example:

See http://automationtesting.sinaapp.com/blog/m_timeformat

Time. strftime (format [, t]): converts a time string that represents the time or struct_tim into a formatted time string. If t is not specified, the return of time. localtime () is called as the input. If any element in the input is out of bounds, A ValueError exception is reported. The formatting parameters are as follows:

Format Description Remarks
% Simplified local week name
% Complete local week name
% B Simplified local month name
% B Local full month name
% C Local Date and Time Representation
% D Date (01-31)
% H Hour (in 24-hour format, 00-23)
% I Hour (in 12-hour format, 01-12)
% J Days (based on years) (001-366)
% M Month (01-12)
% M Minute (00-59)
% P Displays the ID of an am or pm.
% S Seconds (01-61)
% U Week (year-based) (00-53 Sunday is the beginning of the week .) All days before the first Sunday are placed in week 0th.
% W Day of the week (0-6, 0 is Sunday)
% W It is basically the same as % U, starting from Monday.
% X Local date Representation
% X Local Time Representation
% 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

Note:

See http://automationtesting.sinaapp.com/blog/m_time

The altzone attribute is used to view the offset of the current Daylight Time. Run the daylight attribute to check whether the daylight Saving Time is used. Timezone to view the offset of the current time zone. Tzname returns the time zone corresponding to the local time zone and the Daylight Saving Time System.

In [3]: time. altzone see http://automationtesting.sinaapp.com/blog/m_time
At http://automationtesting.sinaapp.com/blog/m_timereference? Http://docs.python.org/2/library/time.html? Http://pymotw.com/2/time/

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.