Datetime utility functions
Christos TZOTZIOY Georgiou
tzot at sil-tec.gr
Tue Sep 16 10:19:50 EDT 2003
On Mon, 15 Sep 2003 20:44:08 +0100, rumours say that Paul Moore
<paul.moore at atosorigin.com> might have written:
[find the end of the month]
>The best solution I could find was
>
>def month_end(dt):
> # Get the next month
> y, m = dt.year, dt.month
> if m == 12:
> y += 1
> m = 1
> else:
> m += 1
>
> # Use replace to cater for both datetime and date types. This
> # leaves the time component of a datetime unchanged - it's
> # arguable whether this is the right thing.
>
> return dt.replace(year=y, month=m, day=1) - datetime.timedelta(days=1)
I sent my own version without having seen your own --and mine might seem
obfuscated, compared to yours. Only a minor suggestion: don't use
dt.replace, use dt.__class__ instead, since you wouldn't want your
function to have side-effects (that is, don't destroy the actual object
that dt is bound to.)
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
More information about the Python-list
mailing list