Summary of several ways to handle time in Python

  • 2020-05-07 20:00:03
  • OfStack

We start with 1 string

View the code slice on CODE that is derived to my code slice


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


  1.1. Time to convert to struct_time    

View the code slice on CODE derived to my code slice


  >>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) 

1.2. To get the corresponding timestamp (seconds) :

View the code slice on CODE that is derived to my code slice


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

Time in   1.3.struct_time form returns the starting string:
views the code slice on CODE to derive my code slice


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

What about the time when the timestamp (number of seconds) returns to the form struct_time?
views the code slice on CODE to derive my code slice


  <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) 

1.5. Time stamp (number of seconds) to return to the string should know what to do?

Of course, there is a straightforward way to do this, but the time format is not the same:
View the code slice on CODE derived to my code slice


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

1.6. To get the current time:

Today:
View the code slice on CODE that is derived to my code slice


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

Now:
View the code slice on CODE that is derived to my code slice


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

Current timestamp:


 >>> time.time()

1428045689.396

Current struct_time form 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)

Current UTC date format:


  >>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) 

0

1.7) datetime. date datetime/time to convert struct_time?


  >>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) 

1

So, combined with 1.2, isn't it easy to convert to seconds?

1.8.datetime.date /datetime how do I convert a date in form to a string in '2010-01-01 00:00:00'?

Isn't it easy to combine 1.3 and 1.7?
1.9. How to convert a string into datetime. date/datetime time?
on CODE view the code slice derived to my code slice


  >>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) 

2

2.0. And then to convert struct_time to datetime. date datetime/time has succeeded

Under what circumstances need to converting struct_time datetime date datetime/time. Read the 2.1 will understand
2.1 time operation - time plus or minus

What was  's time yesterday?
View the code slice on CODE derived to my code slice


  >> today=datetime.date.today() 

View the code slice on CODE derived to my code slice


  >>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) 

4


What about tomorrow? And after seven days? How about 1 minute ago (), 1 second?

Look at this constructor:

class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]).

Note that struct_time and strings cannot be evaluated with datetime.timedelta. So know from other forms into datetime. date/datetime/time is very useful.

Of course, struct_time can also be timed in this way. For example, to calculate yesterday:


  >>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) 

5

2.2) time comparison:

datetime.(date/datetime/time.) can be compared with struct_time. (one cannot compare with another)


Related articles: