]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
fix format_date() with a datetime parameter (#282)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 30 Jul 2012 21:48:55 +0000 (21:48 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 30 Jul 2012 21:48:55 +0000 (21:48 +0000)
babel/dates.py
babel/tests/dates.py

index 9b79543260bef4bcd587d8fa429f6a885c24ab84..aa021b1aff2f85c518e7a5c1c52b7de9b3b012a1 100644 (file)
@@ -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
index 603a440a892ce7f050444d355fed209690ef64a8..fa45a860f5b5f3559a50df369c452c463443c03a 100644 (file)
@@ -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):