Author: jezdez
Date: 2012-02-09 10:57:31 -0800 (Thu, 09 Feb 2012)
New Revision: 17473

Modified:
   django/trunk/django/utils/dateformat.py
   django/trunk/docs/ref/templates/builtins.txt
   django/trunk/tests/regressiontests/templates/filters.py
Log:
Fixed #16416 -- Added two new date formatting options for timezones and ISO 
week numbers. Thanks, poirier.

Modified: django/trunk/django/utils/dateformat.py
===================================================================
--- django/trunk/django/utils/dateformat.py     2012-02-09 18:57:22 UTC (rev 
17472)
+++ django/trunk/django/utils/dateformat.py     2012-02-09 18:57:31 UTC (rev 
17473)
@@ -22,7 +22,7 @@
 from django.utils.encoding import force_unicode
 from django.utils.timezone import is_aware, is_naive
 
-re_formatchars = 
re.compile(r'(?<!\\)([aAbBcdDEfFgGhHiIjlLmMnNOPrsStTUuwWyYzZ])')
+re_formatchars = 
re.compile(r'(?<!\\)([aAbBcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])')
 re_escaped = re.compile(r'\\(.)')
 
 class Formatter(object):
@@ -144,6 +144,17 @@
         "Day of the week, textual, 3 letters; e.g. 'Fri'"
         return WEEKDAYS_ABBR[self.data.weekday()]
 
+    def e(self):
+        "Timezone name if available"
+        try:
+            if self.data.tzinfo:
+                # Have to use tzinfo.tzname and not datetime.tzname
+                # because datatime.tzname does not expect Unicode
+                return self.data.tzinfo.tzname(self.data) or ""
+        except NotImplementedError:
+            pass
+        return ""
+
     def E(self):
         "Alternative month names as required by some locales. Proprietary 
extension."
         return MONTHS_ALT[self.data.month]
@@ -187,6 +198,10 @@
         "Month abbreviation in Associated Press style. Proprietary extension."
         return MONTHS_AP[self.data.month]
 
+    def o(self):
+        "ISO 8601 year number matching the ISO week number (W)"
+        return self.data.isocalendar()[0]
+
     def O(self):
         "Difference to Greenwich time in hours; e.g. '+0200', '-0430'"
         seconds = self.Z()

Modified: django/trunk/docs/ref/templates/builtins.txt
===================================================================
--- django/trunk/docs/ref/templates/builtins.txt        2012-02-09 18:57:22 UTC 
(rev 17472)
+++ django/trunk/docs/ref/templates/builtins.txt        2012-02-09 18:57:31 UTC 
(rev 17473)
@@ -1276,8 +1276,8 @@
 
 Formats a date according to the given format.
 
-Uses the same format as PHP's ``date()`` function (http://php.net/date)
-with some custom extensions.
+Uses a similar format as PHP's ``date()`` function (http://php.net/date)
+with some differences.
 
 Available format strings:
 
@@ -1299,6 +1299,9 @@
 d                 Day of the month, 2 digits with           ``'01'`` to 
``'31'``
                   leading zeros.
 D                 Day of the week, textual, 3 letters.      ``'Fri'``
+e                 Timezone name. Could be in any format,
+                  or might return an empty string,          ``''``, ``'GMT'``, 
``'-500'``, ``'US/Eastern'``, etc.
+                  depending on the datetime.
 E                 Month, locale specific alternative
                   representation usually used for long
                   date representation.                      ``'listopada'`` 
(for Polish locale, as opposed to ``'Listopad'``)
@@ -1323,6 +1326,9 @@
 n                 Month without leading zeros.              ``'1'`` to ``'12'``
 N                 Month abbreviation in Associated Press    ``'Jan.'``, 
``'Feb.'``, ``'March'``, ``'May'``
                   style. Proprietary extension.
+o                 ISO-8601 week-numbering year,             ``'1999'``
+                  corresponding to
+                  the ISO-8601 week number (W)
 O                 Difference to Greenwich time in hours.    ``'+0200'``
 P                 Time, in 12-hour hours, minutes and       ``'1 a.m.'``, 
``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
                   'a.m.'/'p.m.', with minutes left off

Modified: django/trunk/tests/regressiontests/templates/filters.py
===================================================================
--- django/trunk/tests/regressiontests/templates/filters.py     2012-02-09 
18:57:22 UTC (rev 17472)
+++ django/trunk/tests/regressiontests/templates/filters.py     2012-02-09 
18:57:31 UTC (rev 17473)
@@ -345,6 +345,12 @@
         'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 
2008'),
         #Ticket 9520: Make sure |date doesn't blow up on non-dates
         'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''),
+        # ISO date formats
+        'date04': (r'{{ d|date:"o" }}', {'d': datetime(2008, 12, 29)}, '2009'),
+        'date05': (r'{{ d|date:"o" }}', {'d': datetime(2010, 1, 3)}, '2009'),
+        # Timezone name
+        'date06': (r'{{ d|date:"e" }}', {'d': datetime(2009, 3, 12, 
tzinfo=FixedOffset(30))}, '+0030'),
+        'date07': (r'{{ d|date:"e" }}', {'d': datetime(2009, 3, 12)}, ''),
 
          # Tests for #11687 and #16676
          'add01': (r'{{ i|add:"5" }}', {'i': 2000}, '2005'),

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to