views:

102

answers:

3

This is a very noobish question, so I apologize in advance!

I have two time stamps for start and end of the event. They are stored in as datetime.datetime in UTC. What I need to do is figure out the duration of the event.

I tried subtracting one from the other, but receive error:

Traceback (most recent call last):
02.
File '/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py', line 509, in __call__
03.
handler.post(*groups)
04.
File '/base/data/home/apps/.../3.340324527833140591/main.py', line 441, in post
05.
call_record.Duration = call_record.CallStartTime - call_record.CallEndTime
06.
File '/base/python_lib/versions/1/google/appengine/ext/db/__init__.py', line 472, in __set__
07.
value = self.validate(value)
08.
File '/base/python_lib/versions/1/google/appengine/ext/db/__init__.py', line 2322, in validate
09.
(self.name, self.data_type.__name__))
10.
BadValueError: Property Duration must be a datetime
11.

CallStartTime, CallEndTime and Duration are all db.DateTimeProperty() types in GAE.

I had previously used django timesince to display the duration, but I need to do some additional calculations to figure out avg. duration of the events.

Any suggestions or pointers at what additional info might help are greatly appreciated!

+4  A: 

Subtracting one datetime from another will give you a timedelta. You can use that to create another datetime if you need to by adding it to or subtracting it from another datetime object.

How can you represent a duration with a single datetime object, though?

Will McCutchen
Thought I was doing something wrong! I don't need to store duration as a datetime, just thought it was the right type.What I really want is number of seconds that has passed. Can I derive it from timedelta?
Sologoub
Yes, if you look at the docs (http://docs.python.org/library/datetime.html#timedelta-objects), you'll see that `timedelta` objects have a `seconds` attribute.
Will McCutchen
+1  A: 

The difference of two datetime.datetime objects is a datetime.timedelta object:

In [2]: t1=datetime.datetime.now()

In [3]: t1
Out[3]: datetime.datetime(2010, 3, 5, 12, 34, 6, 402507)

In [4]: t2=datetime.datetime.now()

In [5]: dt=t2-t1

In [6]: dt
Out[6]: datetime.timedelta(0, 8, 911129)

timedeltas have days, seconds and microseconds attributes.

In [7]: dt.seconds
Out[7]: 8

If the timedelta spans a duration of days, then you'll need to the days to seconds too:

In [8]: dt.days*(3600*24)+dt.seconds
Out[8]: 8

For more info a timedeltas, see http://docs.python.org/library/datetime.html#timedelta-objects

unutbu
A: 

For stuff like this I always use time.time() that gives back a nice float and then mostly format it something like this:

import time

t1 = time.time()

someLongTakingFunction()

print "Function took %.2f" % (time.time() - t1)

This is nice for quick and dirty checks, but supposedly there are way better ways of measuring performance. 90% of the time this works all the time for me though.

Koen Bok