Fixed #12472 - Correctly show TimeFields values when using locale-aware formatting. Thanks to Ramiro Morales for providing the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12050 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-01 21:36:36 +00:00
parent 0e88350893
commit 20fbba7f90
4 changed files with 24 additions and 12 deletions

View File

@ -158,7 +158,7 @@ class EasyInstanceField(object):
if isinstance(self.field, models.DateTimeField):
objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT'))
elif isinstance(self.field, models.TimeField):
objs = capfirst(formats.date_format(self.raw_value, 'TIME_FORMAT'))
objs = capfirst(formats.time_format(self.raw_value, 'TIME_FORMAT'))
else:
objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT'))
else:

View File

@ -15,10 +15,10 @@ except ImportError:
from django.template import Variable, Library
from django.conf import settings
from django.utils import formats
from django.utils.translation import ugettext, ungettext
from django.utils.encoding import force_unicode, iri_to_uri
from django.utils.safestring import mark_safe, SafeData
from django.utils.formats import date_format, number_format
register = Library()
@ -167,14 +167,14 @@ def floatformat(text, arg=-1):
return input_val
if not m and p < 0:
return mark_safe(number_format(u'%d' % (int(d)), 0))
return mark_safe(formats.number_format(u'%d' % (int(d)), 0))
if p == 0:
exp = Decimal(1)
else:
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
try:
return mark_safe(number_format(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)), abs(p)))
return mark_safe(formats.number_format(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)), abs(p)))
except InvalidOperation:
return input_val
floatformat.is_safe = True
@ -686,7 +686,7 @@ def date(value, arg=None):
if arg is None:
arg = settings.DATE_FORMAT
try:
return date_format(value, arg)
return formats.date_format(value, arg)
except AttributeError:
try:
return format(value, arg)
@ -696,16 +696,16 @@ date.is_safe = False
def time(value, arg=None):
"""Formats a time according to the given format."""
from django.utils.dateformat import time_format
from django.utils import dateformat
if value in (None, u''):
return u''
if arg is None:
arg = settings.TIME_FORMAT
try:
return date_format(value, arg)
return formats.time_format(value, arg)
except AttributeError:
try:
return time_format(value, arg)
return dateformat.time_format(value, arg)
except AttributeError:
return ''
time.is_safe = False

View File

@ -55,6 +55,12 @@ def date_format(value, format=None):
"""
return dateformat.format(value, get_format(format or 'DATE_FORMAT'))
def time_format(value, format=None):
"""
Formats a datetime.time object using a localizable format
"""
return dateformat.time_format(value, get_format(format or 'TIME_FORMAT'))
def number_format(value, decimal_pos=None):
"""
Formats a numeric value using localization settings
@ -84,7 +90,7 @@ def localize(value):
elif isinstance(value, datetime.date):
return date_format(value)
elif isinstance(value, datetime.time):
return date_format(value, 'TIME_FORMAT')
return time_format(value, 'TIME_FORMAT')
return value
def localize_input(value, default=None):

View File

@ -4,7 +4,7 @@ import datetime
from django.template import Template, Context
from django.conf import settings
from django.utils.formats import get_format, date_format, number_format, localize, localize_input
from django.utils.formats import get_format, date_format, time_format, number_format, localize, localize_input
from django.utils.numberformat import format
from django.test import TestCase, client
from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy
@ -91,8 +91,10 @@ class FormattingTests(TestCase):
self.f = 99999.999
self.d = datetime.date(2009, 12, 31)
self.dt = datetime.datetime(2009, 12, 31, 20, 50)
self.t = datetime.time(10, 15, 48)
self.ctxt = Context({
'n': self.n,
't': self.t,
'd': self.d,
'dt': self.dt,
'f': self.f
@ -103,10 +105,10 @@ class FormattingTests(TestCase):
settings.USE_I18N = self._use_i18n
settings.USE_L10N = self._use_l10n
settings.USE_THOUSAND_SEPARATOR = self._use_thousand_separator
def test_locale_independent(self):
"""
Localization of dates and numbers
Localization of numbers
"""
settings.USE_L10N = True
settings.USE_THOUSAND_SEPARATOR = False
@ -130,6 +132,7 @@ class FormattingTests(TestCase):
self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
self.assertEqual(u'10:15 a.m.', time_format(self.t))
self.assertEqual(u'des. 31, 2009', date_format(self.d))
self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
@ -144,6 +147,7 @@ class FormattingTests(TestCase):
self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
self.assertEqual(u'10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
@ -184,6 +188,7 @@ class FormattingTests(TestCase):
self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
self.assertEqual(u'10:15:48', time_format(self.t))
self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
@ -210,6 +215,7 @@ class FormattingTests(TestCase):
self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
self.assertEqual(u'100000,0', Template('{{ f|floatformat }}').render(self.ctxt))
self.assertEqual(u'10:15:48', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))