Re: simpler increment of time values?

Discussion in 'Python' started by Ian Kelly, Jul 5, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Thu, Jul 5, 2012 at 7:56 AM, Chris Angelico <> wrote:
    > 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


    The "+ 86400" is redundant; you'll get the same answer with or without
    it. There is nothing to fear from going negative when doing modulo
    arithmetic, because unlike C, Python actually has well-defined
    semantics regarding modulo division of negative numbers.

    >>> (13382 + 86400 - 42597) % 86400

    57185
    >>> (13382 - 42597) % 86400

    57185

    Cheers,
    Ian
    Ian Kelly, Jul 5, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vlastimil Brom

    simpler increment of time values?

    Vlastimil Brom, Jul 5, 2012, in forum: Python
    Replies:
    5
    Views:
    226
    Steven D'Aprano
    Jul 6, 2012
  2. Chris Angelico

    Re: simpler increment of time values?

    Chris Angelico, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    162
    Chris Angelico
    Jul 5, 2012
  3. Mark Lawrence

    Re: simpler increment of time values?

    Mark Lawrence, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    169
    Mark Lawrence
    Jul 5, 2012
  4. Jason Friedman

    Re: simpler increment of time values?

    Jason Friedman, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    155
    Jason Friedman
    Jul 5, 2012
  5. Devin Jeanpierre

    Re: simpler increment of time values?

    Devin Jeanpierre, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    176
    Devin Jeanpierre
    Jul 5, 2012
Loading...

Share This Page