Time module, pythontime Module

Source: Internet
Author: User

Time module, pythontime Module

I. Introduction

1. in python, these methods are usually used to indicate the time:

1) Timestamp

2) formatted time string

3) Time tuples (struct_time) have nine elements

 

2. UTC is the Greenwich Mean Time, the world standard time. UTC + 8 in China.

3. timestamp: Generally, the timestamp indicates the offset calculated in seconds from 00:00:00, January 1, January 1, 1970. Functions that return timestamp mainly include time.

4. struct_time: struct_time has nine elements. The main functions that return struct_time include gmtime (), localtime (), and strptime (). As follows:

  

 

Ii. Time Functions

import time

1. time. time () returns the timestamp of the current time.

T = time. time () print (t) # output 1495016507.4327872

  

2. time. localtime () converts a timestamp to struct_time of the current time zone. If the secs parameter is not provided, the current time prevails.

T1 = time. localtime () print (t1) # output time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 17, tm_hour = 18, tm_min = 22, tm_sec = 54, tm_wday = 2, tm_yday = 137, tm_isdst = 0)

  

3. The time. gmtime () and localtime () methods are similar. The gmtime () method converts a timestamp to UTC _time of the UTC time zone (0 time zone.

T3 = time. gmtime () print (t3) # output time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 17, tm_hour = 10, tm_min = 22, tm_sec = 54, tm_wday = 2, tm_yday = 137, tm_isdst = 0)

We can see that the difference between tm_hour and above is 8 hours.

 

4. time. mktime () converts a struct_time to a timestamp.

Print (time. mktime (t1) # output 1495016896.0

 

5. time. asctime () indicates a time tuples or struct_time as follows: 'sun Jun 20 23:21:05 123 '. If no parameter exists, time. localtime () is passed in as the parameter.

Print (time. asctime () # output Wed May 17 19:09:05 2017

 

6. time. ctime () Same as above

 

7. time. strftime (format, t)

Convert a time string or struct_time (such as returned by time. localtime () and time. gmtime () that represents time. If t is not specified, time. localtime () is passed in (). If any element in the tuples crosses the border, the ValueError will be thrown.

T = time. localtime () print (t) print (time. strftime ("% Y-% m-% d % X", t) # output time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 17, tm_hour = 19, tm_min = 16, tm_sec = 4, tm_wday = 2, tm_yday = 137, tm_isdst = 0) 19:16:04

  

 

8. time. strptime (string [, format]): converts a formatted time string to struct_time. In fact, it and strftime () are inverse operations.

T = "17:23:12" print (time. strptime (t, "% Y-% m-% d % H: % M: % S") # output time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 16, tm_hour = 17, tm_min = 23, tm_sec = 12, tm_wday = 1, tm_yday = 136, tm_isdst =-1)

  

Iii. Time Conversion

1. format the string-Timestamp

Import timeprint ("formatted string ". center (20, '-') t = "17:23:12" print (t) print ("time tuples ". center (20, '-') t1 = time. strptime (t, "% Y-% m-% d % H: % M: % S") print (t1) print ("timestamp ". center (20, '-') t2 = time. mktime (t1) print (t2) # output ------- formatted string ------- 17:23:12 -------- time tuples -------- time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 16, tm_hour = 17, tm_min = 23, tm_sec = 12, tm_wday = 1, tm_yday = 136, tm_isdst =-1) -------- timestamp --------- 1494926592.0

  

2. timestamp --- format the string

Import timeprint ("timestamp ". center (20, '-') t = time. time () print (t) print ("time tuples ". center (20, '-') t1 = time. localtime (t) # if time is used here. gmtime () indicates that the generated time tuples have a UTC time difference with the local time. print (t1) print ("Format String ". center (20, '-') t2 = time. strftime ("% Y-% m-% d % X", t1) print (t2) # output -------- timestamp --------- 1495020952.5951612 -------- time tuples -------- time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 17, tm_hour = 19, tm_min = 35, tm_sec = 52, tm_wday = 2, tm_yday = 137, tm_isdst = 0) ------- format the string ------- 19:35:52

  

 

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.