From: Felix Schwarz Date: Mon, 30 Jul 2012 21:48:55 +0000 (+0000) Subject: fix format_date() with a datetime parameter (#282) X-Git-Tag: 1.0~170 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=920bb01c6bd84e2693aadb39effa24d3f4722e6f;p=thirdparty%2Fbabel.git fix format_date() with a datetime parameter (#282) --- diff --git a/babel/dates.py b/babel/dates.py index 9b795432..aa021b1a 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -924,7 +924,7 @@ class DateTimeFormat(object): def get_day_of_year(self, date=None): if date is None: date = self.value - return (date - date_(date.year, 1, 1)).days + 1 + return (date - date.replace(month=1, day=1)).days + 1 def get_week_number(self, day_of_period, day_of_week=None): """Return the number of the week of a day within a period. This may be diff --git a/babel/tests/dates.py b/babel/tests/dates.py index 603a440a..fa45a860 100644 --- a/babel/tests/dates.py +++ b/babel/tests/dates.py @@ -83,6 +83,11 @@ class DateTimeFormatTestCase(unittest.TestCase): d = date(2007, 4, 1) fmt = dates.DateTimeFormat(d, locale='en_US') self.assertEqual('91', fmt['D']) + + def test_day_of_year_works_with_datetime(self): + d = datetime(2007, 4, 1) + fmt = dates.DateTimeFormat(d, locale='en_US') + self.assertEqual('91', fmt['D']) def test_day_of_year_first(self): d = date(2007, 1, 1) @@ -230,6 +235,11 @@ class FormatDateTestCase(unittest.TestCase): datetime(2007, 04, 01, 15, 30), "yyyy-MM-dd HH:mm", locale='en_US') + def test_with_day_of_year_in_pattern_and_datetime_param(self): + # format_date should work on datetimes just as well (see #282) + d = datetime(2007, 04, 01) + self.assertEqual('14', dates.format_date(d, 'w', locale='en_US')) + class FormatTimeTestCase(unittest.TestCase):