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


Groups > comp.lang.python > #102803

Re: timedelta and float multiplication in python 2

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: timedelta and float multiplication in python 2
Date Thu, 11 Feb 2016 12:24:20 +0100
Organization None
Lines 36
Message-ID <mailman.46.1455189873.22075.python-list@python.org> (permalink)
References <56BC64DD.4090908@shopzeus.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace news.uni-berlin.de EEVe3tP0CAmRpXB5yj/ryQJykWVumD9NN5tpJqeU07XA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operand': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:m 10': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'subject:python': 0.14; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'seconds,': 0.16; 'skip:d 60': 0.16; 'unsupported': 0.16; 'wrote:': 0.16; '"",': 0.22; 'this:': 0.23; 'thanks,': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'workaround': 0.29; 'checked': 0.31; 'implement': 0.32; 'point': 0.33; 'problem': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'this?': 0.34; 'skip:d 20': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'mean': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'determine': 0.61; 'between': 0.65; 'today': 0.65; 'here': 0.66; 'nagy': 0.84; 'type(s)': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8d57.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102803

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web