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


Groups > comp.lang.python > #102799

timedelta and float multiplication in python 2

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Nagy László Zsolt <gandalf@shopzeus.com>
Newsgroups comp.lang.python
Subject timedelta and float multiplication in python 2
Date Thu, 11 Feb 2016 11:39:25 +0100
Lines 29
Message-ID <mailman.42.1455187570.22075.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-2
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de sVjw/X+qFJ90/lPY0UaELABXEiCO0hXEprVgo4zypgAg==
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'operand': 0.07; 'subject:skip:m 10': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'subject:python': 0.14; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds,': 0.16; 'skip:d 60': 0.16; 'unsupported': 0.16; '>>>': 0.20; '"",': 0.22; 'this:': 0.23; 'thanks,': 0.24; '(most': 0.24; '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; 'two': 0.37; 'mean': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'determine': 0.61; 'charset:iso-8859-2': 0.64; 'between': 0.65; 'today': 0.65; 'here': 0.66; 'type(s)': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopzeus.com; s=shopzeus_com; t=1455187163; bh=IBJ2nSlsI8/1WrF7VgrKeMH5b6c+5frrcmaLKHT1CzY=; h=To:From:Subject:Date:From; b=m6Z/gVlkcK+K0t9gAxAOEcjimjMzc4C2KCPev4fisRWOdkF8XL2+4gXEglczm3TEd Sq6xhjnWCbndCpNe0TVxj9j3v93iZ6/SqeCgNODaIut5yH2KSdc7YsFoHzAXb1zOcs X9AL5WQEsH+I7kCCevN0A+F8W6fYeZHNsup9SQCjWcPdwAt2zskkWDrPmh3Qri9qY8 8OnXDx2WgyZ9AxsUQesaqw1n5SvLN4QYldGKyny9rpccKieRBjc0nqedY1Qwi+VQwE bntKICLrqC4IgZex9U3LhDyy4sbPvDpDD/JTZ1sklDoik32flacVWvW5MzKEbt+sBu kCTT0/xRUkg8g==
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:102799

Show key headers only | View raw


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)

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

timedelta and float multiplication in python 2 Nagy László Zsolt <gandalf@shopzeus.com> - 2016-02-11 11:39 +0100

csiph-web