[Python-Dev] dateutil (original) (raw)

Greg Ewing greg at cosc.canterbury.ac.nz
Mon Mar 15 00:01:40 EST 2004


>>> now.replace(month=2, day=30) Traceback (most recent call last): File "", line 1, in ? ValueError: day is out of range for month >>>

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()

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg at cosc.canterbury.ac.nz +--------------------------------------+



More information about the Python-Dev mailing list