Summary of several methods for processing time in Python, and several methods for python

Source: Internet
Author: User
Tags timedelta

Summary of several methods for processing time in Python, and several methods for python

Starting from a string

View the CODE piece derived from my CODE piece on CODE

  >>>time_str='2008-08-08 08:08:08' 


1. 1. Time converted to struct_time format

View the CODE piece derived from my CODE piece on CODE

  >>struct = ime.strptime(time_str,'%Y-%m-%d %H:%M:%S')     time.struct_time(tm_year=2008, tm_mon=8, tm_mday=8, tm_hour=8, tm_min=8, tm_sec=8, tm_wday=4, tm_yday=221, tm_isdst=-1) 

. If you want to obtain the corresponding timestamp (in seconds ):

View the CODE piece derived from my CODE piece on CODE

   >>>sec=time.mktime(struct)   >>> sec   1218154088.0 

1.3.struct _ time format returns the start string:
View the CODE piece derived from my CODE piece on CODE

  >>time_str=time.strftime("%Y-%m-%d %H:%M:%S",struct)   >>> time_str   '2008-08-08 08:08:08'  

. What should I do if the time stamp (in seconds) is returned in the struct_time format?
View the CODE piece derived from my CODE piece on CODE

  <pre name="code" class="python">>> time.gmtime(sec)   time.struct_time(tm_year=2008, tm_mon=8, tm_mday=8, tm_hour=0, tm_min=8, tm_sec=8, tm_wday=4, tm_yday=221, tm_isdst=0) 

. The timestamp (number of seconds) should be returned to the string to know how to get it?

Of course, there are very direct methods, but the time formats for conversion back are different:
View the CODE piece derived from my CODE piece on CODE

  >>> time.ctime(sec)   'Fri Aug 08 08:08:08 2008'  

. To get the current time:

Today:
View the CODE piece derived from my CODE piece on CODE

  >>> datetime.date.today()       datetime.date(2015, 4, 3) 

Now:
View the CODE piece derived from my CODE piece on CODE

  >>> datetime.datetime.now()       datetime.datetime(2015, 4, 3, 15, 19, 47, 361000) 

Current timestamp:

 >>> time.time()1428045689.396

Current struct_time format time:

>>> time.localtime()time.struct_time(tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=21, tm_sec=52, tm_wday=4, tm_yday=93, tm_isdst=0)

The current UTC date format:

 >>> time.ctime()  'Fri Apr 03 15:23:45 2015'

1.7) What should I do if I want to convert datetime. date/datetime/time To struct_time?

 >>> datetime.datetime.now().timetuple()   time.struct_time(tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=31, tm_sec=19, tm_wday=4, tm_yday=93, tm_isdst=-1)

In this way, is it easy to convert the data into seconds when combined with 1.2?

1.8.datetime.date/how to convert a date in the datetime format to a string in the '2017-01-01 00:00:00 'format?

Is it easy to combine 1.3 and 1.7?
1. 9. How to convert a string to datetime. date/datetime/time?
View the CODE piece derived from my CODE piece on CODE

  >>> datetime.datetime.strptime('2014-01-01',"%Y-%m-%d")      datetime.datetime(2014, 1, 1, 0, 0) 

2. 0. then convert struct_time to datetime. date/datetime/time.

Under what circumstances do you need to convert struct_time to datetime. date/datetime/time. After reading 2.1, you will understand
2.1 time calculation-addition and subtraction of time

How was yesterday counted?
View the CODE piece derived from my CODE piece on CODE

  >> today=datetime.date.today() 

View the CODE piece derived from my CODE piece on CODE

  >>> delta=datetime.timedelta(days=1)   >>> yesterday=today-delta   >>> yesterday    datetime.date(2015, 4, 2)  


What about tomorrow? Seven days ago? One minute ago (), one second?

Take a look at this constructor:

Class datetime. timedelta ([days [, seconds [, microseconds [, milliseconds [, minutes [, hours [, weeks]) can help you answer the above questions?

Note that struct_time and string cannot be computed with datetime. timedelta. So it is useful to convert from other forms to datetime. date/datetime/time.

Of course, struct_time can also be used for time calculation. For example, to calculate yesterday:

 >>from time import time,localtime >>day = 24*60*60 >>yesterday = localtime(time()-day)

2.2) time comparison:

This is just one sentence: datetime. (date/datetime/time.) And struct_time can be compared. (Cannot be compared with each other)

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.