473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

difference between two times in seconds

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
Apr 24 '09 #1
7 29751
bvdet
2,851 Expert Mod 2GB
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
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
Apr 25 '09 #3
bvdet
2,851 Expert Mod 2GB
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
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
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
bvdet
2,851 Expert Mod 2GB
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. >>> 
Apr 26 '09 #7
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

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

Similar topics

1
by: Andrew | last post by:
1) I have mySQL 3.23 installed. I have a record with two date/times fields in YYYY-MM-DD HH:MM:SS format. How do I find the difference between those two date/times? (I would also like to know it...
8
by: DraguVaso | last post by:
Hi, I'm new to WebServices, and I'm doing some tests (with a small VB.NET-application) to know the performance-difference between a WebService and the 'normal'-way of getting data (just...
1
by: mark | last post by:
I'm stuck: In a query I am trying to compare two fields with a date/time data type and a LongTime format to get the difference in hours (minutes and seconds if possible). After that I need to...
87
by: John Rivers | last post by:
Hello everybody, I just wondered if anybody else has noticed this? It takes around 6 seconds to start debugging a very simple ASPX page with VS.NET whereas VB6 takes under 0.5 seconds, even...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
2
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007...
19
by: xianwei | last post by:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main ( int argc, char *argv ) { long i = 10000000L; clock_t start, end; double duration;
7
by: orajat | last post by:
hi, how do i calculate difference in time in seconds (00:09:05 - 00:09:00 = 300) where start time being a field and the difference in time is calculated in AHT field, ie last record minus previous...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.