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


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

Re: Datetime.timedelta

Started by"Gabriel Genellina" <gagsl-py2@yahoo.com.ar>
First post2011-05-17 08:44 -0300
Last post2011-05-17 08:44 -0300
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: Datetime.timedelta "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-05-17 08:44 -0300

#5559 — Re: Datetime.timedelta

From"Gabriel Genellina" <gagsl-py2@yahoo.com.ar>
Date2011-05-17 08:44 -0300
SubjectRe: Datetime.timedelta
Message-ID<mailman.1667.1305632687.9059.python-list@python.org>
En Tue, 17 May 2011 07:44:08 -0300, Tsolmon Narantsogt <mnts26@gmail.com>  
escribió:

> I'm using datetime.timedelta and i have a problem
>
> delta = 1 day, 2:30:00
> hours = delta.days * 8
>
> how to add 8 + 2:30:00

Just operate with it as it were a number. The timedelta class implements  
all "sane" mathematical operations.

py> from datetime import *
py> def timedelta_from_dhms(days=0, hours=0, mins=0, secs=0):
...   return timedelta(days, hours*3600 + mins*60 + secs)
...
py> delta = timedelta_from_dhms(1, 2, 30)
py> delta
datetime.timedelta(1, 9000)
py> hours = delta.days * 8
py> delta + hours
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'datetime.timedelta' and  
'int'
py> hours = timedelta_from_dhms(0, delta.days * 8)
py> hours
datetime.timedelta(0, 28800)
py> delta + hours
datetime.timedelta(1, 37800)
py> def dhms_from_timedelta(td):
...   return td.days, td.seconds // 3600, (td.seconds % 3600) // 60,  
td.seconds % 60
...
py> dhms_from_timedelta(delta + hours)
(1, 10, 30, 0)

-- 
Gabriel Genellina

[toc] | [standalone]


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


csiph-web