putting date strings in order

John Machin sjmachin at lexicon.net
Tue May 12 12:26:27 EDT 2009


On May 13, 1:58 am, Jaime Fernandez del Rio <jaime.f... at gmail.com>
wrote:
> On Tue, May 12, 2009 at 5:02 PM, MRAB <goo... at mrabarnett.plus.com> wrote:
> > John Machin wrote:
>
> >> MRAB <google <at> mrabarnett.plus.com> writes:
>
> >>> Sort the list, passing a function as the 'key' argument. The function
> >>> should return an integer for the month, eg 0 for 'jan', 1 for 'feb'. If
> >>> you want to have a different start month then add
>
> >> and if you don't like what that produces, try subtract :-)
>
> > Oops!
>
> >>> the appropriate
> >>> integer for that month (eg 0 for 'jan', 1 for 'feb') and then modulo 12
> >>> to make it wrap around (there are only 12 months in a year), returning
> >>> the result.
>
> > Actually, subtract the start month, add 12, and then modulo 12.
>
> Both on my Linux and my Windows pythons, modulos of negative numbers
> are properly taken, returning always the correct positive number
> between 0 and 11. I seem to recall, from my distant past, that Perl
> took pride on this being a language feature. Anyone knows if that is
> not the case with python, and so not adding 12 before taking the
> modulo could result in wrong results in some implementations?

If that happens, it's a bug.
http://docs.python.org/reference/expressions.html#binary-arithmetic-operations

If you look at function i_divmod() in the 2.x branch's Objects/
intobject.c, you'll be reassured to see that it doesn't just take
whatever C serves up :-)



More information about the Python-list mailing list