Skip to content

Commit d981cb4

Browse files
committed
Fixed #7262 - Added ISO 8601 and microsecond format string to utils.dateformat. Thanks zegor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent ac371cc commit d981cb4

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ answer newbie questions, and generally made Django that much better:
486486
487487
Jesse Young <[email protected]>
488488
Mykola Zamkovoi <[email protected]>
489+
zegor
489490
Gasper Zejn <[email protected]>
490491
Jarek Zgoda <[email protected]>
491492
Cheng Zhang

django/utils/dateformat.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from django.utils.translation import ugettext as _
2020
from django.utils.encoding import force_unicode
2121

22-
re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
22+
re_formatchars = re.compile(r'(?<!\\)([aAbBcdDfFgGhHiIjlLmMnNOPrsStTUuwWyYzZ])')
2323
re_escaped = re.compile(r'\\(.)')
2424

2525
class Formatter(object):
@@ -104,6 +104,11 @@ def s(self):
104104
"Seconds; i.e. '00' to '59'"
105105
return u'%02d' % self.data.second
106106

107+
def u(self):
108+
"Microseconds"
109+
return self.data.microsecond
110+
111+
107112
class DateFormat(TimeFormat):
108113
year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
109114

@@ -118,6 +123,13 @@ def b(self):
118123
"Month, textual, 3 letters, lowercase; e.g. 'jan'"
119124
return MONTHS_3[self.data.month]
120125

126+
def c(self):
127+
"""
128+
ISO 8601 Format
129+
Example : '2008-01-02T10:30:00.000123'
130+
"""
131+
return self.data.isoformat(' ')
132+
121133
def d(self):
122134
"Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
123135
return u'%02d' % self.data.day

docs/ref/templates/builtins.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ Available format strings:
637637
A ``'AM'`` or ``'PM'``. ``'AM'``
638638
b Month, textual, 3 letters, lowercase. ``'jan'``
639639
B Not implemented.
640+
c ISO 8601 Format. ``2008-01-02 10:30:00.000123``
640641
d Day of the month, 2 digits with ``'01'`` to ``'31'``
641642
leading zeros.
642643
D Day of the week, textual, 3 letters. ``'Fri'``
@@ -673,6 +674,7 @@ Available format strings:
673674
month, 2 characters.
674675
t Number of days in the given month. ``28`` to ``31``
675676
T Time zone of this machine. ``'EST'``, ``'MDT'``
677+
u Microseconds. ``0`` to ``999999``
676678
U Seconds since the Unix Epoch
677679
(January 1 1970 00:00:00 UTC).
678680
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)

tests/regressiontests/dateformat/tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ def test_am_pm(self):
3939

4040
def test_date_formats(self):
4141
my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
42+
timestamp = datetime.datetime(2008, 5, 19, 11, 45, 23, 123456)
4243

4344
self.assertEquals(dateformat.format(my_birthday, 'A'), u'PM')
45+
self.assertEquals(dateformat.format(timestamp, 'c'), u'2008-05-19 11:45:23.123456')
4446
self.assertEquals(dateformat.format(my_birthday, 'd'), u'08')
4547
self.assertEquals(dateformat.format(my_birthday, 'j'), u'8')
4648
self.assertEquals(dateformat.format(my_birthday, 'l'), u'Sunday')
@@ -79,12 +81,14 @@ def test_timezones(self):
7981
my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
8082
summertime = datetime.datetime(2005, 10, 30, 1, 00)
8183
wintertime = datetime.datetime(2005, 10, 30, 4, 00)
84+
timestamp = datetime.datetime(2008, 5, 19, 11, 45, 23, 123456)
8285

8386
if self.tz_tests:
8487
self.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100')
8588
self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
8689
self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET')
8790
self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600')
91+
self.assertEquals(dateformat.format(timestamp, 'u'), u'123456')
8892
self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600')
8993
self.assertEquals(dateformat.format(summertime, 'I'), u'1')
9094
self.assertEquals(dateformat.format(summertime, 'O'), u'+0200')

0 commit comments

Comments
 (0)