changeset 223:49b089453f81

Implement day-of-year date format field. Closes #49.
author cmlenz
date Mon, 16 Jul 2007 20:51:49 +0000
parents bd8b1301b27e
children 9d0a19b4518b
files babel/dates.py babel/tests/dates.py
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -452,6 +452,8 @@
             return self.format_week(char, num)
         elif char == 'd':
             return self.format(self.value.day, num)
+        elif char == 'D':
+            return self.format_day_of_year(num)
         elif char in ('E', 'e', 'c'):
             return self.format_weekday(char, num)
         elif char == 'a':
@@ -518,6 +520,10 @@
         context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
         return get_day_names(width, context, self.locale)[weekday]
 
+    def format_day_of_year(self, num):
+        delta = self.value - date(self.value.year, 1, 1)
+        return self.format(delta.days + 1, num)
+
     def format_period(self, char):
         period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
         return get_period_names(locale=self.locale)[period]
--- a/babel/tests/dates.py
+++ b/babel/tests/dates.py
@@ -32,6 +32,21 @@
         fmt = dates.DateTimeFormat(d, locale='en_US')
         self.assertEqual('1', fmt['W'])
 
+    def test_day_of_year(self):
+        d = date(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)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('001', fmt['DDD'])
+
+    def test_day_of_year_last(self):
+        d = date(2007, 12, 31)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('365', fmt['DDD'])
+
     def test_local_day_of_week(self):
         d = date(2007, 4, 1) # a sunday
         fmt = dates.DateTimeFormat(d, locale='de_DE')
Copyright (C) 2012-2017 Edgewall Software