summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2012-02-09 18:57:31 +0000
committerJannis Leidel <jannis@leidel.info>2012-02-09 18:57:31 +0000
commita6b6c6e17129379fa1f9ffce1ed7eaaa5d6f1127 (patch)
tree090663b15fce2c07992d1fd5afdd22c58408cf21
parente445b66fd8a2b5e931debad3c920be1345871ad9 (diff)
Fixed #16416 -- Added two new date formatting options for timezones and ISO week numbers. Thanks, poirier.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17473 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/utils/dateformat.py17
-rw-r--r--docs/ref/templates/builtins.txt10
-rw-r--r--tests/regressiontests/templates/filters.py6
3 files changed, 30 insertions, 3 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py
index d87fb13105..81c2d87f91 100644
--- a/django/utils/dateformat.py
+++ b/django/utils/dateformat.py
@@ -22,7 +22,7 @@ from django.utils.translation import ugettext as _
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 @@ class DateFormat(TimeFormat):
"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 @@ class DateFormat(TimeFormat):
"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()
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index afee499830..0bc293dcd9 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -1276,8 +1276,8 @@ date
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 @@ c ISO 8601 format. (Note: unlike others ``2008-01-02T10:30:0
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 @@ M Month, textual, 3 letters. ``'Jan'``
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
diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py
index 54c12673a2..41f8e43c9e 100644
--- a/tests/regressiontests/templates/filters.py
+++ b/tests/regressiontests/templates/filters.py
@@ -345,6 +345,12 @@ def get_filter_tests():
'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'),