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
This commit is contained in:
Jannis Leidel 2010-01-01 21:39:26 +00:00
parent ac371ccac8
commit d981cb4e66
4 changed files with 20 additions and 1 deletions

View File

@ -486,6 +486,7 @@ answer newbie questions, and generally made Django that much better:
ymasuda@ethercube.com
Jesse Young <adunar@gmail.com>
Mykola Zamkovoi <nickzam@gmail.com>
zegor
Gasper Zejn <zejn@kiberpipa.org>
Jarek Zgoda <jarek.zgoda@gmail.com>
Cheng Zhang

View File

@ -19,7 +19,7 @@ from django.utils.tzinfo import LocalTimezone
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
re_formatchars = re.compile(r'(?<!\\)([aAbBcdDfFgGhHiIjlLmMnNOPrsStTUuwWyYzZ])')
re_escaped = re.compile(r'\\(.)')
class Formatter(object):
@ -104,6 +104,11 @@ class TimeFormat(Formatter):
"Seconds; i.e. '00' to '59'"
return u'%02d' % self.data.second
def u(self):
"Microseconds"
return self.data.microsecond
class DateFormat(TimeFormat):
year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
@ -118,6 +123,13 @@ class DateFormat(TimeFormat):
"Month, textual, 3 letters, lowercase; e.g. 'jan'"
return MONTHS_3[self.data.month]
def c(self):
"""
ISO 8601 Format
Example : '2008-01-02T10:30:00.000123'
"""
return self.data.isoformat(' ')
def d(self):
"Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
return u'%02d' % self.data.day

View File

@ -637,6 +637,7 @@ Available format strings:
A ``'AM'`` or ``'PM'``. ``'AM'``
b Month, textual, 3 letters, lowercase. ``'jan'``
B Not implemented.
c ISO 8601 Format. ``2008-01-02 10:30:00.000123``
d Day of the month, 2 digits with ``'01'`` to ``'31'``
leading zeros.
D Day of the week, textual, 3 letters. ``'Fri'``
@ -673,6 +674,7 @@ Available format strings:
month, 2 characters.
t Number of days in the given month. ``28`` to ``31``
T Time zone of this machine. ``'EST'``, ``'MDT'``
u Microseconds. ``0`` to ``999999``
U Seconds since the Unix Epoch
(January 1 1970 00:00:00 UTC).
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)

View File

@ -39,8 +39,10 @@ class DateFormatTests(TestCase):
def test_date_formats(self):
my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
timestamp = datetime.datetime(2008, 5, 19, 11, 45, 23, 123456)
self.assertEquals(dateformat.format(my_birthday, 'A'), u'PM')
self.assertEquals(dateformat.format(timestamp, 'c'), u'2008-05-19 11:45:23.123456')
self.assertEquals(dateformat.format(my_birthday, 'd'), u'08')
self.assertEquals(dateformat.format(my_birthday, 'j'), u'8')
self.assertEquals(dateformat.format(my_birthday, 'l'), u'Sunday')
@ -79,12 +81,14 @@ class DateFormatTests(TestCase):
my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
summertime = datetime.datetime(2005, 10, 30, 1, 00)
wintertime = datetime.datetime(2005, 10, 30, 4, 00)
timestamp = datetime.datetime(2008, 5, 19, 11, 45, 23, 123456)
if self.tz_tests:
self.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100')
self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET')
self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600')
self.assertEquals(dateformat.format(timestamp, 'u'), u'123456')
self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600')
self.assertEquals(dateformat.format(summertime, 'I'), u'1')
self.assertEquals(dateformat.format(summertime, 'O'), u'+0200')