Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102803 > unrolled thread

Re: timedelta and float multiplication in python 2

Started byPeter Otten <__peter__@web.de>
First post2016-02-11 12:24 +0100
Last post2016-02-11 12:24 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: timedelta and float multiplication in python 2 Peter Otten <__peter__@web.de> - 2016-02-11 12:24 +0100

#102803 — Re: timedelta and float multiplication in python 2

FromPeter Otten <__peter__@web.de>
Date2016-02-11 12:24 +0100
SubjectRe: timedelta and float multiplication in python 2
Message-ID<mailman.46.1455189873.22075.python-list@python.org>
Nagy László Zsolt wrote:

> I ran into a problem today where I had to determine the mean point
> between two datetimes. Here is an example:
> 
>>>> start = datetime.datetime(2016,2,11)
>>>> stop = datetime.datetime.now()
>>>> mean = start + (stop-start)*0.5
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and
> 'float'
> 
> A workaround could be something like this:
> 
>>>> start + datetime.timedelta(seconds=0.5*((stop-start).total_seconds()))
> datetime.datetime(2016, 2, 11, 5, 45, 45, 818009)

How about

mean = start + (stop - start) / 2

?
 
> Interesting thing is that total_seconds() already returns fractions of
> seconds, so it would be almost trivial to implement timedelta
> multiplication with floats.
> 
> I have checked and it does work with Python 3. But it does not work with
> Python 2 - is there a good reason for this?
> 
> Thanks,
> 
>    Laszlo

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web