classification
Title: datetime.time does not support arithmetic
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, ezio.melotti, gboutsioukis, lemburg, skip.montanaro
Priority: normal Keywords: patch

Created on 2008-07-01 09:48 by cjw296, last changed 2009-07-06 21:13 by lemburg. This issue is now closed.

Files
File name Uploaded Description Edit
datetime.diff gboutsioukis, 2008-07-03 18:17 time arithmetic patch
Messages (8)
msg69042 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2008-07-01 09:48
>>> from datetime import time
>>> time(9,0)-time(8,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'datetime.time' and
'datetime.time'

I'd expect a datetime.timedelta(0,3600)
msg69224 - (view) Author: George Boutsioukis (gboutsioukis) (Python committer) Date: 2008-07-03 18:17
I have also come across this in the past. Although I sense that some
obscure reason might prevent time arithmetic from being included, here's
a patch to add time/timedelta addition and subtraction. It closely
follows the datetime arithmetic functions. To handle overflows, I
figured it should wrap around a 24-hour limit. The timezone stuff is
copied verbatim from the datetime functions, some finger-crossing applied.
msg69251 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2008-07-04 11:43
Hi George,

I haven't looked at your patch but that fact that there are no unit
tests and you talk about copying and pasting code, I'd suggest this
might not be a good patch.

Refactor so code is only in one place rather than copying and pasting,
and make sure you write extensive unit tests, especially for things like
date/time...

cheers,

Chris
msg69260 - (view) Author: George Boutsioukis (gboutsioukis) (Python committer) Date: 2008-07-04 15:01
Hi Chris,
 
 I know copy-pasted sounds horrible--perhaps I should have said 'modeled
afterwards'(better marketing;). The thing is, the datetime & time
classes share a lot of common functionality; it is inevitable that some
code looks like it's repeated, because the same process is followed(take
a look at the datetime & date functions already there). 
 I can't see much room for refactoring the arithmetic functions across
classes(seems too messy). Besides, the existing timezone, normalization
and delta functions are used, so no significant logic is repeated.The
patch indeed requires some cleanup, but overall it's good code(and not a
lot of it). 
 I submitted it without tests/doc because I think there should be a
chance for this functionality to be discussed. Also, the datetime module
looks like for some reason these functions were left out(some discussion
I'm missing?).

So before investing any more time on this some feedback would be
appreciated.
msg69360 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-07-06 23:23
George> To handle overflows, I figured it should wrap around a 24-hour
    George> limit.

That's precisely the reason that time objects don't support arithmetic.
There is no obviously best way to handle overflow.
msg90102 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-07-04 03:12
If there's no obviously best way to handle overflow, can this be closed?
msg90188 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2009-07-06 19:33
I think so.

FWIW, I'd recommend looking at:

http://pypi.python.org/pypi/python-dateutil

...for doing things that python's builtin datetime stuff doesn't cater 
for.
msg90192 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2009-07-06 21:13
Or use the "original" lib on which this is all based :-)

http://www.egenix.com/products/python/mxBase/mxDateTime/

(and which, of course, does allow subtracting times)
History
Date User Action Args
2009-07-06 21:13:54lemburgsetnosy: + lemburg
messages: + msg90192
2009-07-06 19:33:15cjw296setstatus: open -> closed
resolution: wont fix
messages: + msg90188
2009-07-04 03:12:14ezio.melottisetnosy: + ezio.melotti
messages: + msg90102
2008-07-06 23:23:31skip.montanarosetnosy: + skip.montanaro
messages: + msg69360
2008-07-04 15:01:03gboutsioukissetmessages: + msg69260
2008-07-04 11:43:59cjw296setmessages: + msg69251
2008-07-03 18:17:37gboutsioukissetfiles: + datetime.diff
keywords: + patch
messages: + msg69224
nosy: + gboutsioukis
2008-07-01 13:07:14benjamin.petersonsettype: enhancement
versions: + Python 2.7, - Python 2.5, Python 2.4
2008-07-01 09:48:59cjw296create