Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24913
| References | <CAHzaPEO5D498zepGC0EVVURdUE7qxYJk6ksd5yS_Xj_hh5iiyQ@mail.gmail.com> <CAHzaPEMKGbUPhpfaB5bFkxJkMsmHrCzPZojYB7ZVO2r8P0MGAg@mail.gmail.com> |
|---|---|
| Date | 2012-07-05 23:56 +1000 |
| Subject | Re: simpler increment of time values? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1823.1341496599.4697.python-list@python.org> (permalink) |
On Thu, Jul 5, 2012 at 11:18 PM, Vlastimil Brom <vlastimil.brom@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.) ChrisA
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: simpler increment of time values? Chris Angelico <rosuav@gmail.com> - 2012-07-05 23:56 +1000
Re: simpler increment of time values? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 15:19 +0000
Re: simpler increment of time values? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-07-05 08:39 -0700
Re: simpler increment of time values? Chris Angelico <rosuav@gmail.com> - 2012-07-06 01:59 +1000
Re: simpler increment of time values? Chris Angelico <rosuav@gmail.com> - 2012-07-06 01:53 +1000
Re: simpler increment of time values? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-06 00:42 -0400
csiph-web