diff -r bcb02458ef78 Doc/library/calendar.rst --- a/Doc/library/calendar.rst Tue Sep 21 01:02:10 2010 +0200 +++ b/Doc/library/calendar.rst Mon Sep 20 23:26:49 2010 -0400 @@ -201,6 +201,13 @@ Returns the current setting for the weekday to start each week. +.. function:: dayofyear(year, month, day) + + Returns the integer between 1 and 366 (inclusive) representing the specified + *day* (``1``--``31``) in the specified *month* (``1``--``12``) of the + specified *year*. + + .. function:: isleap(year) Returns :const:`True` if *year* is a leap year, otherwise :const:`False`. diff -r bcb02458ef78 Lib/calendar.py --- a/Lib/calendar.py Tue Sep 21 01:02:10 2010 +0200 +++ b/Lib/calendar.py Mon Sep 20 23:26:49 2010 -0400 @@ -113,6 +113,17 @@ return datetime.date(year, month, day).weekday() +def dayofyear(year, month, day): + """Returns the integer between 1 and 366 (inclusive) representing the + specified day in the specified month of the specified year. + + """ + # Get the ordinal of the desired day and of the first day of that year. + desired_day = datetime.date(year, month, day).toordinal() + first_day_of_year = datetime.date(year, 1, 1).toordinal() + return desired_day - first_day_of_year + 1 + + def monthrange(year, month): """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.""" diff -r bcb02458ef78 Lib/test/test_calendar.py --- a/Lib/test/test_calendar.py Tue Sep 21 01:02:10 2010 +0200 +++ b/Lib/test/test_calendar.py Mon Sep 20 23:26:49 2010 -0400 @@ -250,6 +250,22 @@ # verify it "acts like a sequence" in two forms of iteration self.assertEqual(value[::-1], list(reversed(value))) + def test_dayofyear(self): + """Test for the calendar.dayofyear() function, which computes the + integer between 1 and 366 (inclusive) representing the specified day in + the specified month of the specified year. + + """ + expected_day_of_year = 1 + for month in range(1, 13): + for day in range(1, calendar.mdays[month] + 1): + day_of_year = calendar.dayofyear(2010, month, day) + self.assertEqual(expected_day_of_year, day_of_year) + # The computed day of the year must be between 1 and 366. + self.assertGreaterEqual(day_of_year, 1) + self.assertLessEqual(day_of_year, 366) + expected_day_of_year += 1 + class MonthCalendarTestCase(unittest.TestCase): def setUp(self):