[Tutor] Help with range of months spanning across years

Hugo Arts hugo.yoshi at gmail.com
Wed Feb 2 04:11:52 CET 2011


On Wed, Feb 2, 2011 at 2:55 AM, Sean Carolan <scarolan at gmail.com> wrote:
>> This sounds somewhat like homework. If it is, that's fine, mention it,
>> and we will help you. But we won't do your homework for you, so keep
>> that in mind.
>
> A reasonable assumption but this is actually going in a cgi tool that
> I'm using at work.  The input comes from pull-down menus on a web
> page.

I apologize for assuming homework. It's the tutor list, and I try to
focus on effective teaching more than anything. No offense was meant.

> Here's what I came up with, feel free to give suggestions on how this
> might be made more efficient:
>
> [snip code]
>
> I was hoping there was some whiz-bang function that would just iterate
> through months if it was fed a start and end date.  Can datetime or
> calendar do this?

As far as I can tell from quickly going through documentation, no. At
least, not with a quick and easy function. datetime can represent the
dates just fine, and you can add days to that until you hit your end
date, but adding months is harder. timedelta can't represent a month,
which makes sense since they're not of constant length anyway. So
using datetime is not really your best option. Calendar is more about
displaying than doing calculations, making it even less useful.

You can use your code or ian's (or my modification of ian's code) to
your taste, I don't expect any measurable difference in efficiency. If
it's really important, *measure*. But I suspect it isn't, unless
you'll run it either on time spans of thousands of years or if it will
be used by thousands of people at a time.

Hugo


More information about the Tutor mailing list