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!
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)
timedelta
s 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 timedelta
s, see http://docs.python.org/library/datetime.html#timedelta-objects
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.