简体   繁体   English

在Python中找出事件的持续时间

[英]Figuring out duration of an event in Python

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. 它们存储为UTC中的datetime.datetime。 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. GAE中的CallStartTime,CallEndTime和Duration都是db.DateTimeProperty()类型。

I had previously used django timesince to display the duration, but I need to do some additional calculations to figure out avg. 我以前曾使用django timesince来显示持续时间,但是我需要做一些额外的计算才能得出平均值。 duration of the events. 事件的持续时间。

Any suggestions or pointers at what additional info might help are greatly appreciated! 任何有关其他信息可能会有所帮助的建议或指针,将不胜感激!

Subtracting one datetime from another will give you a timedelta . 将一个datetime与另一个datetime时间相减会得到一个timedelta You can use that to create another datetime if you need to by adding it to or subtracting it from another datetime object. 如果需要,可以使用该datetime创建另一个datetime ,方法是将其添加到另一个datetime对象或从另一个datetime对象中减去。

How can you represent a duration with a single datetime object, though? 但是,如何用单个datetime对象表示持续时间呢?

The difference of two datetime.datetime objects is a datetime.timedelta object: 两个datetime.datetime对象的区别是datetime.timedelta对象:

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. timedelta ■找dayssecondsmicroseconds属性。

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: 如果timedelta跨越天的持续时间,那么你就需要未来的日子秒太:

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 有关timedelta的更多信息,请参见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: 对于这样的事情,我总是使用time.time(),它会返回一个不错的浮点数,然后主要将其格式化如下:

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. 90%的时间对我来说一直都是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM