Connecting Tech Pros Worldwide Help | Site Map

difference between two times in seconds

Newbie
 
Join Date: Apr 2009
Location: Cambridge, UK
Posts: 6
#1: Apr 24 '09
Hi,

It should be simple - but I am trying to find the difference between two datetimes as an integer in seconds. Using pywin32 build 212, python 2.5, Windows XP Pro, MySQL 5.0.

Expand|Select|Wrap|Line Numbers
  1. import time
  2. import datetime
  3.  
  4. def date4string(string):
  5.     return datetime.date(*time.strptime(string, "%Y-%m-%d %H:%M:%S")[0:3])
  6.  
  7. date1="2009-01-01 00:00:00"
  8. date2="2009-01-02 00:00:00"
  9. d1=date4string(date1)
  10. d2=date4string(date2)
  11. diff=d2-d1
  12. print diff
gives >>> 1 day, 0:00:00

what I want is the answer 86400

Sorry for being an idiot. I have looked at time.mktime;

Expand|Select|Wrap|Line Numbers
  1. diff1=time.mktime(diff)
  2. print diff1
but that gives

TypeError: argument must be 9-item sequence, not datetime.timedelta
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: Apr 24 '09

re: difference between two times in seconds


I suggest using time.mktime().
Expand|Select|Wrap|Line Numbers
  1. def comp_dates(d1, d2):
  2.     # Date format: %Y-%m-%d %H:%M:%S
  3.     return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\
  4.            time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))
Expand|Select|Wrap|Line Numbers
  1. >>> date1="2009-01-01 00:00:00"
  2. >>> date2="2009-01-02 00:00:00"
  3. >>> comp_dates(date1, date2)
  4. 86400.0
  5. >>> 
Newbie
 
Join Date: Apr 2009
Location: Cambridge, UK
Posts: 6
#3: Apr 25 '09

re: difference between two times in seconds


Hi,

Thanks for the response. It doesn't help though becuase I have failed to describe the problem properly.

I a writing a script to create a MySQL table using data from an existing set of large tables.

I am pulling two dates (its slightly more complex than that) from the existing tables, and I want to put the difference in seconds back in the resulting table.

I can handle all the MyQSL stuff - I am just miserably stuck on how to get the difference between the two dates - in seconds as an integer.

Regards,

Ben
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#4: Apr 25 '09

re: difference between two times in seconds


Why did my post not help? You wanted an answer of 86400 and I showed you how to get an answer of 86400.0. Are you not aware that objects can be type cast in Python?
Expand|Select|Wrap|Line Numbers
  1. >>> int(comp_dates(date1, date2))
  2. 86400
  3. >>> 
Newbie
 
Join Date: Apr 2009
Location: Cambridge, UK
Posts: 6
#5: Apr 26 '09

re: difference between two times in seconds


I am not making myself very clear.

I don't have strings at all. I have two dates being returned as dates from a MySQL query. They are in date format. I need to be able to subtract one date from another and get a difference in seconds.
Newbie
 
Join Date: Apr 2009
Location: Cambridge, UK
Posts: 6
#6: Apr 26 '09

re: difference between two times in seconds


I have solved the problem by changing the initial query to return unix_timestamp(date) from the database for each date in question rather than the date. Then subtracting one seconds from epoch from the other seconds from epoch.

I can't belive its not possible to just subtract one date from another and return the number of seconds difference though.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#7: Apr 26 '09

re: difference between two times in seconds


OK, maybe it's me that's a bit slow. You have two datetime.date objects, d1 and d2, right?
Expand|Select|Wrap|Line Numbers
  1. def comp_dates2(d1, d2):
  2.     # Date format: %Y-%m-%d %H:%M:%S
  3.     return time.mktime(time.strptime(d2,"%Y-%m-%d"))-\
  4.            time.mktime(time.strptime(d1, "%Y-%m-%d"))
  5.  
  6. print repr(d1)
  7. print repr(d2)
  8. print int(comp_dates2(str(d1), str(d2)))
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> datetime.date(2009, 1, 1)
  2. datetime.date(2009, 1, 2)
  3. 86400
  4. >>>
Another way:
Expand|Select|Wrap|Line Numbers
  1. >>> def comp_dates3(d1, d2):
  2. ...     delta = d2-d1
  3. ...     return delta.days*86400+delta.seconds
  4. ... 
  5. >>> comp_dates3(d1, d2)
  6. 86400
  7. >>> 
Newbie
 
Join Date: May 2009
Posts: 3
#8: May 6 '09

re: difference between two times in seconds


I am not sure that I understood, but a solution can be :
Converting to epoch time, doing all the stuff you need and than reconverting it back ?

I hope it helped, regards.
Reply