[Python-Dev] dateutil
Kevin Jacobs
jacobs at theopalgroup.com
Mon Mar 15 07:54:22 EST 2004
Greg Ewing wrote:
>>>>>now.replace(month=2, day=30)
>>>>>
>>>>>
>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()
>
>
After using such a system in a large system with non-trivial date
manipulations, I think
you'd change your mind about that approach -- it can be excessively
verbose. Being
able to change a full array of date and time components in one call is
_very_ useful.
However, in my datetime classes, I also have a very complete set of
calls like:
startOfMonth, endOfMonth, startOfYear, endOfYear, lastWeekday, nextWeekday,
etc... That way your code that needs the power and benefits from the
compactness of the powerful relative and absolute adjustment functions
(with many
parameters), but also has very readable simple adjustments for common
cases and
for novice programmers to get started.
Best regards,
-Kevin
More information about the Python-Dev
mailing list