simpler increment of time values?
rosuav at gmail.com
Thu Jul 5 15:56:37 CEST 2012
On Thu, Jul 5, 2012 at 11:18 PM, Vlastimil Brom
<vlastimil.brom at gmail.com> wrote:
> 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.
I'm not familiar with the Python classes (I tend to think in terms of
language-agnostic algorithms first, and specific libraries/modules/etc
second), but if you're working with simple integer seconds, your
datelessness is just modulo arithmetic.
time1 + time2 --> (time1 + time2) % 86400
time1 - time2 --> (time1 + 86400 - time2) % 86400
Or alternatively, bounding afterward:
if time > 86400: time-=86400
if time < 86400: time+=86400
(The "magic number" 86400 is a well-known number, being seconds in a
day. Feel free to replace it with 24*60*60 if it makes you feel
better; I'm pretty sure Python will translate it into a constant at
parse time. Or alternatively, have a module-level constant
SECONDS_IN_A_DAY = 86400, in case that number should ever change.)
More information about the Python-list