How to operate time functions in the Python stuct_time Module

Source: Internet
Author: User

Do you know how to use the modules in Python stuct_time to provide more than N types? The following article describes how to use the Code related to how to operate the time function in the Python stuct_time module.

I. The module in Python stuct_time provides various operation time functions. There are two methods:

First, the timestamp is unique compared to the offset calculated in seconds at 00:00:00 on January 1 ,.

Second, it is represented as an array (struct_time). There are nine elements, respectively, indicating that the stuct_time of the same timestamp varies with the time zone.

Time zone: Time Zone and UTC time zone functions:

 
 
  1. asctime()  
  2. asctime([tuple]) -> string    

Converts a Python struct_time to a string clock (). This function has two functions: the actual running time of the program is returned during the first call, and the total running time of the entire program is returned ), after the second call, the returned time is the interval from the first call to the current call. Example:

 
 
  1. import time  
  2. if __name__ == '__main__':  
  3. time.sleep(1)  
  4. print "clock1:%s" % time.clock()  
  5. time.sleep(1)  
  6. print "clock1:%s" % time.clock()  
  7. time.sleep(1)  
  8. print "clock1:%s" % time.clock()   

Output:

 
 
  1. clock1:3.07301626324e-006  
  2. clock1:0.997576228264  
  3. clock1:1.99513653272   
  4. 1.1.3 sleep(seconds)  

The thread delays running at a specified time. It has been tested and measured in seconds.

 
 
  1. ctime(seconds)  
  2. ctime(seconds) -> string  

Converts a timestamp Python stuct_time (current time by default) into a time string. Example:

 
 
  1. if __name__ == '__main__':  
  2. print time.ctime()  

Output:

 
 
  1. Thu Mar 04 12:55:03 2010  
  2. 1.1.5 gmtime(...)  
  3. gmtime([seconds]) -> (tm_year, tm_mon, tm_day,
     tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)  

(9 parameter values) converts a timestamp into a struct_time of UTC time zone (0 Time Zone). If the seconds parameter is not input, the current time is the time value of the standard array type) example:

 
 
  1. if __name__ == '__main__':  
  2. print time.gmtime()   

Time tuples output:

 
 
  1. (2010, 3, 4, 4, 58, 53, 3, 63, 0) 

The returned data is an array type. The meanings are as follows:

 
 
  1. (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst) 

After an array is obtained, we can split it like this: print time. gmtime () [0] to get the first element value.

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.