Python Time-time module

Source: Internet
Author: User
Tags julian day local time month name

Time is a Python-brought module that handles timing problems and provides a series of functions for operating time.

  The following instructions are for python2.7 and may vary from one version to another.

The module provides two kinds of formats for representing time:

1. The timestamp is the time, in seconds , from the "New era" to the present, called UTC or GMT. This "new Era" refers to the January 1, 1970. So the time stamp refers to how many seconds have elapsed since the "New era" to a certain time, possibly an integer, or a floating-point number. As to why this is so, it is interesting to read this post: poke here

2. A ganso consisting of 9 elements, the 9 elements are:

Year:4 digits, expressed in years, for example: 2016

Month: Represents the months, the range is 1-12

Day: Indicates days, range is 1-31

Hours: Hour, Range is 0-23

minute: minutes, Range is 0-59

Seconds: seconds, Range is 0-59

Weekday: Week, Range is 0-6, Monday is 0, etc.

Julian Day: Is the days of the year, the range is 1-366

DST: A flag that determines whether to use daylight saving time (about Daylight saving: Stamp here), 0 indicates no use, 1 is used, or 1, the Mktime () method is inferred from date and time. In general, no need.

FUNCTIONS

1. asctime ([tuple]), string

Converts the time of the tuple format to a string format.

Example: time.asctime ((2016,5,11,12,30,50,5,163,0))

  

If the tuple is not given, the LocalTime () method is called to get the current time.

2. clock (), floating point number

This is a bit special, because the system is different, in the win platform, the first call, return is the actual time the process is running. The second time after the call is the first call after the call time. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)

code example:

Import Timeif __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 ()

Output:

The so-called process run time, I feel more like the time required to run the first call of this code, because no matter the first call with sleep pause or perform another operation, its output is not small.

Import Timeif __name__=='__main__': b= []     forXinchRange (1000): B.append (x) time.sleep (1)    Print "clock1:%s"%Time.clock () time.sleep (1)    Print "clock2:%s"%Time.clock () time.sleep (1)    Print "clock3:%s"% Time.clock ()

I do not know whether I understand the mistake or the other, now this understanding, there are problems I will correct later.

In Unix systems (although win is also developed by Unix), it returns "process time", which is a floating-point number (timestamp) in seconds.

The same code, run in CentOS6.7:

Its output is:

Here is an experimental code:

ImportTime,urllibif __name__=='__main__':    Print 'start at:', Time.ctime ()Try: Ur_open= Urllib.urlopen ('http://www.facebook.com')    except:        Print 'Error', Time.ctime () Time.sleep (1)    Print "clock1:%s"%Time.clock ()Try: Ur_open= Urllib.urlopen ('http://www.facebook.com')    except:        Print 'Error', Time.ctime () Time.sleep (1)    Print "clock2:%s"%Time.clock ()Try: Ur_open= Urllib.urlopen ('http://www.facebook.com')    except:        Print 'Error', Time.ctime () Time.sleep (1)    Print "clock3:%s"% Time.clock ()

I tried to open a "nonexistent" website several times to see what the so-called "process Time" is:

Can see my time spent 1 of different, but the process time to the last but only 0.03 seconds, referring to this article (poke here), I think it should be the process CPU time, because the Remote Web page is open for remote I/O operation, does not require a lot of CPU computation, so the process time is very short. Of course, this is my speculation, has not found the corresponding article description, the first to understand it, and then the wrong to change.

3. CTime (seconds), string

Converts a timestamp (the default is the current time) into a time string. Equivalent to asctime (localtime (seconds)) .

4. gmtime ([seconds]) (Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday, Tm_yday, TM_ISDST)

Converts a timestamp format to the Ganso format of the UTC time zone (0 time zone , China utc+8). If no argument is given, the default is local time.

However my actual time is 14 points, 14 = 6 + 8. So pay attention to the time zone.

5. localtime ([seconds]) (tm_year,tm_mon,tm_mday,tm_hour,tm_min, TM_SEC,TM_WDAY,TM_YDAY,TM_ISDST)

Converts a timestamp to the Ganso format of the current time zone . If no argument is given, the default is local time.

  

6. mktime (tuple), floating point number

Converts a time in a tuple format to a timestamp format.

7. Sleep (seconds)

The thread will postpone the specified time after the unit is run, in seconds. Its precision is sub-second.

About precision levels:

Minute level: In minutes, speed is calculated by minute, 7200 rpm
Seconds: In seconds, that is, the speed is calculated in seconds, 1ghz/seconds
Sub-second: the speed of not reaching the second, that is 1ghz/1.2 seconds

8. Time (), floating point number

Returns the timestamp of the current time.

If the system's clock is supported, fractional form may occur.

9. strftime (format[, tuple]), string

Converts a tuple representing time to a string of the specified format, and calls LocalTime () if no tuple is passed in. If any of the elements in the tuple are out of bounds (not in scope), a ValueError error is thrown.

Table about format:

Format Meaning Note
%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 1
%s Seconds (01-61) 2
%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. 3
%w The day of the one week (0-6,0 is Sunday) 3
%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

Note:

1. "%p" only works in conjunction with "%I".

2. The emphasis in the documentation is indeed 0-61, not 59, and leap year seconds for two seconds.

3. When using the Strptime () function,%u and%w are calculated only when the number of weeks and days in the year is determined.

Reference: Poke here

Example:

strptime (string, format), Struct_time

Converts the time of the string format to the Chengyuanzug type. Is the inverse of the above method.

Summarize:

Python Time-time module

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.