help with dates

John Machin sjmachin at lexicon.net
Sat Mar 31 20:18:52 EDT 2007


On Apr 1, 7:51 am, "Pierre Quentel" <quentel.pie... at wanadoo.fr> wrote:
> > Four groups of firefighters (group1, group2, group3, group4). Each
> > group works a 24 hr shift. So group1 works April 1, group2 works April
> > 2, group3 works April 3, group4 works April 4, group 1 works April 5,
> > etc. It just keeps rolling like this forever into next year, etc.
>
> > I need to come up with a methodology for the following:
>
> > If given a date, find out what group is working on that date.
>
> Hello,
>
> You can use method datetime.date.toordinal() :
>
> from datetime import date
> start = date(2007,4,1) # Arpil 1, 2007
>
> def group(_date):
>     return (date.toordinal(_date)-date.toordinal(start)) % 4

... whose possible outcomes are (0, 1, 2, 3) whereas the groups are
numbered 1 to 4. However I'm sure the OP can make the necessary
adjustment :-)

>
> print group(date(2007,4,15))
>
> Regards,
> Pierre





More information about the Python-list mailing list