Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: simpler increment of time values?

Reply
Thread Tools

Re: simpler increment of time values?

 
 
Vlastimil Brom
Guest
Posts: n/a
 
      07-05-2012
Many thanks to all for your suggestions!

@ChrisA
Yes, the calculations with seconds since the Unix epoch is very
convenient for real times, but trying to make it dateless seemed to
make it more complicated for me.

The expected output for the increments asked by Jason was already
correctly stated by Devin; i.e.: 12:45 plus 12 hours is 0:45 and 12:45
minus 13 hours is 23:45.

Thanks for reminding me of dateutil.relativedelta, Mark, I didn't
think of it in this context (I always thought, the "relative" stands
for time and date calculations with regard to the current time and
date). There doesn't seem to be a way to use dateless time either
(unless I missed it),
however, it turns out, that one can probably work with this naive
"times" like with deltas (possibly ignoring other units than hours and
minutes in the result):

>>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30)
>>> "%s.%s" % (td.hours, td.minutes)

'10.15'
>>>

Which is probably the simplest and the most robust way, I found sofar.
It likely isn't the expected use case for relativedelta, but it seems
to work ok. (Are there maybe some drawbacks I am missing?)
(Well I just found one possible pitfall , if floats are passed:
>>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30.5)
>>> "%s.%s" % (td.hours, td.minutes)

'10.0.15.5'
, but its beyond my current use case, and the validation can always be added.)


The same would be doable using the built in timedelta too, but there
are no hours and minutes in its output, hence these are to be
converted from the seconds count.

>>> dttd=datetime.timedelta(hours=9, minutes=45) + datetime.timedelta(minutes=30)
>>> dttd

datetime.timedelta(0, 36900)
>>> dttd.seconds

36900
>>> s = dttd.seconds
>>> h,s = divmod(s,3600)
>>> m,s = divmod(s,60)
>>> h,m,s

(10, 15, 0)
>>> "%s.%s" % (h, m)

'10.15'
>>>


Any thoughts?
thanks again,

vbr
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simpler increment of time values? Vlastimil Brom Python 5 07-06-2012 02:34 AM
Re: simpler increment of time values? Devin Jeanpierre Python 0 07-05-2012 05:05 AM
Re: simpler increment of time values? Jason Friedman Python 0 07-05-2012 04:57 AM
Re: simpler increment of time values? Mark Lawrence Python 0 07-05-2012 02:35 AM
Re: simpler increment of time values? Chris Angelico Python 0 07-05-2012 01:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57