[Python-Dev] dateutil

Moore, Paul Paul.Moore at atosorigin.com
Mon Mar 15 08:49:13 EST 2004


From: Greg Ewing
>> This makes operations like "move to the end of the current month"
>> non-trivial (not *hard*, just non-trivial).
>
> If I were designing something like this, I think I would approach it
> quite differently, and provide a bunch of separate functions or
> methods that transform dates in specific ways. e.g.
>
>  end_of_month(d)
>  forward_months(n, d)
>
> then to "go to the end of the month 3 months from now" would
> be
>
>  end_of_month(forward_months(3, d))
>
> or, if you prefer a method-chaining style,
>
>  d.forward_months(3).end_of_month()

In dateutil, I think it's d + relativedelta(months=+1, day=1, days=-1).
Arguably, this is fairly trivial (highly non-obvious, but trivial...)

It's a trade-off, power at the cost of comprehensibility. I've always
been a fan of flexible, powerful tools (and I'm sure the readability of
my code suffers for it :-)). So I prefer relativedelta.

Some use cases that I find hard with "bare" datetime (all are real
requirements for me):

1. For a date d, get the start and end of the month containing it.
   (many "report this month" jobs)
2. For a date d, get the start and end of the month before it.
   (equivalent "report last month" jobs). This is just (1) plus the
   need to subtract "roughly" a month from a date.
3. 12 noon on the first Thursday of next month. (Don't blame me, that's
   when we have a maintenance slot on one of our systems).
4. The last Friday of the month. (End-of-month reporting time).

The key functionality here is "add N months" and "go to weekday N
(forward or backward)". Both things that the core datetime avoids for
well-explained reasons.

I'm happy with dateutil as it stands, outside of the core. If it's to
go into the core, I agree that a PEP would be the right thing to do.
And probably not just one - separate PEPS for the different areas would
focus discussion better:

1. Expanded datetime "delta" functionality
2. Recurrence rules
3. Flexible date string parser
4. Special date calculations (Easter, any others?)

Paul.



More information about the Python-Dev mailing list