By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

difference between two times in seconds

P: 6

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
  4. def date4string(string):
  5.     return*time.strptime(string, "%Y-%m-%d %H:%M:%S")[0:3])
  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
Apr 24 '09 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 2.5K+
P: 2,851
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. >>> 
Apr 24 '09 #2

P: 6

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.


Apr 25 '09 #3

Expert Mod 2.5K+
P: 2,851
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. >>> 
Apr 25 '09 #4

P: 6
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.
Apr 26 '09 #5

P: 6
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.
Apr 26 '09 #6

Expert Mod 2.5K+
P: 2,851
OK, maybe it's me that's a bit slow. You have two 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"))
  6. print repr(d1)
  7. print repr(d2)
  8. print int(comp_dates2(str(d1), str(d2)))
Expand|Select|Wrap|Line Numbers
  1. >>>, 1, 1)
  2., 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. >>> 
Apr 26 '09 #7

P: 3
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.
May 6 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.