difference between two times in seconds | Newbie | | Join Date: Apr 2009 Location: Cambridge, UK
Posts: 6
| |
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. - import time
-
import datetime
-
-
def date4string(string):
-
return datetime.date(*time.strptime(string, "%Y-%m-%d %H:%M:%S")[0:3])
-
-
date1="2009-01-01 00:00:00"
-
date2="2009-01-02 00:00:00"
-
d1=date4string(date1)
-
d2=date4string(date2)
-
diff=d2-d1
-
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; - diff1=time.mktime(diff)
-
print diff1
but that gives
TypeError: argument must be 9-item sequence, not datetime.timedelta
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,560
| | | re: difference between two times in seconds
I suggest using time.mktime(). - def comp_dates(d1, d2):
-
# Date format: %Y-%m-%d %H:%M:%S
-
return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\
-
time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))
- >>> date1="2009-01-01 00:00:00"
-
>>> date2="2009-01-02 00:00:00"
-
>>> comp_dates(date1, date2)
-
86400.0
-
>>>
| | Newbie | | Join Date: Apr 2009 Location: Cambridge, UK
Posts: 6
| | | 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
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,560
| | | 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? - >>> int(comp_dates(date1, date2))
-
86400
-
>>>
| | Newbie | | Join Date: Apr 2009 Location: Cambridge, UK
Posts: 6
| | | 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
| | | 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.
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,560
| | | 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? - def comp_dates2(d1, d2):
-
# Date format: %Y-%m-%d %H:%M:%S
-
return time.mktime(time.strptime(d2,"%Y-%m-%d"))-\
-
time.mktime(time.strptime(d1, "%Y-%m-%d"))
-
-
print repr(d1)
-
print repr(d2)
-
print int(comp_dates2(str(d1), str(d2)))
Output: - >>> datetime.date(2009, 1, 1)
-
datetime.date(2009, 1, 2)
-
86400
-
>>>
Another way: - >>> def comp_dates3(d1, d2):
-
... delta = d2-d1
-
... return delta.days*86400+delta.seconds
-
...
-
>>> comp_dates3(d1, d2)
-
86400
-
>>>
| | Newbie | | Join Date: May 2009
Posts: 3
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|