python time arithmetic with datetime?

Status
Not open for further replies.

elipsey

Wise, Aged Ars Veteran
179
so i want a date/time class that does math on times so i don't have to. like this:

t1 = mytimeclass.time(10, 15)
t2 = mytimeclass.time(4, 20)
t3 = t2 - t1

...or something.

but the python datetime.time class doesn't seem to have defined arithmetic operators, nor does the datetime.timedelta. if there are methods for this, that's not obvious to me either:

Code:
>>> t1 = datetime.time(12,20)
>>> t1
datetime.time(12, 20)
>>> t1.hour
12
>>> t1.minute
20
>>> t2 = datetime.time(12,30)
>>> t1 - t2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
>>> td = datetime.timedelta(t1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported type for timedelta days component: datetime.time
>>> td = datetime.timedelta(t1,t2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported type for timedelta seconds component: datetime.time
>>>
>>> td = datetime.timedelta(15)
>>> t3 = t1 - td
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.timedelta'
>>> print 'sheesh'

of course this is easy enough to write:
Code:
# t_end, t_start are 'HHMM'
if int(t_end[0]) > int(t_start[0]):
    duration = ((int(t_end[0])-int(t_start[0]))*60)+(int(t_end[2])-int(t_start[2]))
else: # if midnight roll-over occurs, hours elapsed is t_end + 24 - t_start 
    duration = ((int(t_end[0])+(24-int(t_start[0])))*60)+(int(t_end[2])-int(t_start[2]))

...but i do stuff like this all the time, and i would rather avoid code duplication, and stupid mistakes.

if i want to do math on times, do i have to write it myself? what is the point of having a datetime class in that case? just input validation? abstraction of dates for different timezones? (i think the docs say you only get that if you write the timezone info yourself in a subclass) if i wanted to write this stuff my self, why would i use datetime???

what am i missing here?
 

elipsey

Wise, Aged Ars Veteran
179
thx guys, perfect answers.

Code:
>>> t1 = mx.DateTime.Time(1,2)
>>> t1
<mx.DateTime.DateTimeDelta object for '01:02:00.00' at 7f6bc772bf80>
>>> t2 = mx.DateTime.Time(1,3)
>>> t2
<mx.DateTime.DateTimeDelta object for '01:03:00.00' at 7f6bc76d8180>
>>> t3 = t2 - t1
>>> t3
<mx.DateTime.DateTimeDelta object for '00:01:00.00' at 7f6bc76d8b58>

mmmmmm... that's good datetime
 

gloucester

Ars Scholae Palatinae
996
You've noticed that datetime.time objects do not support arithmetic, but both datetime.time and datetime.timedelta objects do. You can use combinations of them to do what you want.

If you care only about quantities of time, use only datetime.timedelta objects:
Code:
dt1 = datetime.timedelta(seconds=1)
dt2 = datetime.timedelta(seconds=2)
dt3 = dt1 + dt2  # result is datetime.timedelta(3)
If you want to do time-of-day arthithmetic, the easiest thing to do is start with a datetime.datetime object with dummy values of year, month, and day, and add datetime.timedelta objects to it:
Code:
t = datetime.datetime(year=1, month=1, day=1, hour=12, minute=30, second=30)
dt = datetime.timedelta(hours=12)
t2 = t + dt # result is datetime.datetime(1, 1, 2, 0, 30, 30)
Then use the time() method to get a datetime.time object if needed:
Code:
t3 = t2.time() #result is datetime.time(0, 30, 30)
 

TomRoche

Wise, Aged Ars Veteran
102
elipsey Wed Nov 10, 2010 3:56 pm
> i want a date/time class that does math on times so i don't have to. like this:

> t1 = mytimeclass.time(10, 15)
> t2 = mytimeclass.time(4, 20)
> t3 = t2 - t1

> ...or something.

I wanna do math on times I cut'n'paste from formatted output, e.g. from `date`, and I'm too lazy for much configuration, either. Fortunately, the following Works With Ubuntu (lucid, FWIW):

me@it:~$ sudo aptitude update ; sudo aptitude install python-dateutil
...
me@it:~$ python -c 'from dateutil.parser import * ; print parse("17:28:07") - parse("15:39:16")'
1:48:51
 
Status
Not open for further replies.