[Python-Dev] dateutil
Tim Peters
tim.one at comcast.net
Mon Mar 15 13:43:37 EST 2004
[Moore, Paul]
> ...
> 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.
"go to weekday N" isn't controversial, it's just that datetime doesn't have
it. The first time I *used* datetime in anger was to write a program to
list PSF Board meeting dates (1pm US Eastern on the second Tuesday of each
month). That's PSF.py in Python CVS's sandbox/datetime module. It uses a
utility function defined in dateutil.py (also in the sandbox):
def weekday_of_month(weekday, dt, index):
"""Return the index'th day of kind weekday in date's month.
All the days of kind weekday (MONDAY .. SUNDAY) are viewed as if a
Python list, where index 0 is the first day of that kind in dt's
month, and index -1 is the last day of that kind in dt's month.
Everything follows from that. The time and tzinfo members (if any)
aren't changed.
Example: Sundays in November. The day part of the date is
irrelevant. Note that a "too large" index simply spills over to
the next month.
>>> base = datetime.datetime(2002, 11, 25, 13, 22, 44)
>>> for index in range(5):
... print index, weekday_of_month(SUNDAY, base, index).ctime()
0 Sun Nov 3 13:22:44 2002
1 Sun Nov 10 13:22:44 2002
2 Sun Nov 17 13:22:44 2002
3 Sun Nov 24 13:22:44 2002
4 Sun Dec 1 13:22:44 2002
Start from the end of the month instead:
>>> for index in range(-1, -6, -1):
... print index, weekday_of_month(SUNDAY, base, index).ctime()
-1 Sun Nov 24 13:22:44 2002
-2 Sun Nov 17 13:22:44 2002
-3 Sun Nov 10 13:22:44 2002
-4 Sun Nov 3 13:22:44 2002
-5 Sun Oct 27 13:22:44 2002
"""
Viewing the dates as a Python list proved easy to work with. Some other
utility functions there:
def first_weekday_on_or_after(weekday, dt):
"""First day of kind MONDAY .. SUNDAY on or after date.
The time and tzinfo members (if any) aren't changed.
>>> base = datetime.date(2002, 12, 28) # a Saturday
>>> base.ctime()
'Sat Dec 28 00:00:00 2002'
>>> first_weekday_on_or_after(SATURDAY, base).ctime()
'Sat Dec 28 00:00:00 2002'
>>> first_weekday_on_or_after(SUNDAY, base).ctime()
'Sun Dec 29 00:00:00 2002'
>>> first_weekday_on_or_after(TUESDAY, base).ctime()
'Tue Dec 31 00:00:00 2002'
>>> first_weekday_on_or_after(FRIDAY, base).ctime()
'Fri Jan 3 00:00:00 2003'
"""
def first_weekday_on_or_before(weekday, dt):
"""First day of kind MONDAY .. SUNDAY on or before date.
The time and tzinfo members (if any) aren't changed.
>>> base = datetime.date(2003, 1, 3) # a Friday
>>> base.ctime()
'Fri Jan 3 00:00:00 2003'
>>> first_weekday_on_or_before(FRIDAY, base).ctime()
'Fri Jan 3 00:00:00 2003'
>>> first_weekday_on_or_before(TUESDAY, base).ctime()
'Tue Dec 31 00:00:00 2002'
>>> first_weekday_on_or_before(SUNDAY, base).ctime()
'Sun Dec 29 00:00:00 2002'
>>> first_weekday_on_or_before(SATURDAY, base).ctime()
'Sat Dec 28 00:00:00 2002'
"""
I think these are all easily spelled as particular patterns of arguments to
Gustavo's relativedelta, but not necessarily *clearly* so spelled. Unsure.
Regardless, it's hard to mistake the intended meaning of a
first_weekday_on_or_after() call <wink>.
More information about the Python-Dev
mailing list