Python time-time module and python-time module

Source: Internet
Author: User
Tags julian day month name

Python time-time module and python-time module

Time is a python module used to handle time issues. It provides a series of time functions.

For python2.7, other versions may be different.

The module provides two time formats:

1. The timestamp isSecondsThe time from the new era to the present, called UTC or GMT. This "New Era" refers to the January 1, 1970 s. Therefore, the timestamp refers to the total number of seconds that have elapsed from the "New Era" to a certain time. It may be an integer or a floating point number. If you are interested, read this article:

2. A ancestor with nine elements. The nine elements are:

Year: 4 digits, indicating year, for example, 2016

Month: indicates the month in the range of 1-12.

Day: indicates the day in the range of 1-31

Hours: hour, range: 0-23

Minute: minute, range: 0-59

Seconds: seconds, ranging from 0 to 59

Weekday: Week, ranging from 0 to 6, Monday is 0, and so on

Julian day: the day of the year. The value range is 1-366.

DST: a flag that determines whether to use the hour (about the hour: Timestamp here). If it is 0, it indicates not to use. If it is 1, it indicates to use. If it is-1, mktime () the method is inferred based on date and time. Generally, this is not required.

FUNCTIONS

1. asctime ([tuple])-> string

Converts the time in the original format to the string format.

Example: time. asctime (5,163, 0 ))

  

If tuple is not given, the localtime () method is called to obtain the current time.

 

2. clock ()-> floating point number

This is a bit special and will vary depending on the system. On the win platform, the first call will return the actual time when the process runs. The second call is from the first call to the call time. (In fact, it is based on QueryPerformanceCounter () on WIN32, which is more accurate than millisecond)

Sample Code:

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 running time, I think, is more like the time required to run the code called for the first time, because no matter whether you pause with sleep or perform other operations before the first call, its output has not changed much.

import timeif __name__ == '__main__':    b = []    for x in range(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 don't know whether I understand the error or anything else. Now I understand it like this, and I will correct the problem later.

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

Run the same code in CentOS6.7:

The output is:

The following is an experiment code:

import time,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 multiple times to see what the so-called "process time" is:

It can be seen that I spent more than one minute in time, but the process time was only 0.03 seconds at the end. Refer to this article (here). I think it should be the cpu usage time of the process, opening a remote webpage is a remote I/O operation and does not require a large amount of cpu computing. Therefore, the process time is very short. Of course, this is my speculation. I haven't found any explanation for the time being, so I should understand it first. I will make a mistake later.

 

3. ctime (seconds)-> string

Converts a timestamp (current time by default) into a time string. It is 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 UTC time zone (0 Time ZoneIn UTC + 8) format. If no parameter is provided, the local time is used by default.

However, my actual time is 14 o'clock, 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 timestampCurrent Time Zone. If no parameter is provided, the local time is used by default.

  

 

6. mktime (tuple)-> floating point number

Converts a time in the original format to a timestamp format.

 

7. sleep (seconds)

The thread will run after a specified delay, in seconds. Its precision is sub-second level.

Precision Level:

Minute level: measured in minutes, that is, the speed is calculated by minute, 7200 RPM
Second level: in seconds, that is, the speed is calculated in seconds, 1 GHz/second
Sub-Second level: the speed does not reach the second level, that is, 1 GHz/1.2 seconds

8. time ()-> floating point number

Returns the timestamp of the current time.

If the system clock is supported, scores may appear.

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

Converts a time-represented tuples to a string in the specified format. If tuple is not input, localtime () is called (). If any element in the tuples is out of the range, a ValueError error is thrown.

Format table:

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. 1
% S Seconds (01-61) 2
% 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  

Note:

1. "% p" is effective only when used with "% I.

2. It is emphasized in the document that it is indeed 0-61, rather than 59. The leap second occupies two seconds.

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.

Reference: click here

Example:

 

10. strptime (string, format)-> struct_time

Converts the time in string format to the original format. Is the inverse of the above method.

Summary:

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.