diff -r 1c03bc321e39 django/contrib/databrowse/datastructures.py
a
|
b
|
|
158 | 158 | if isinstance(self.field, models.DateTimeField): |
159 | 159 | objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT')) |
160 | 160 | elif isinstance(self.field, models.TimeField): |
161 | | objs = capfirst(formats.date_format(self.raw_value, 'TIME_FORMAT')) |
| 161 | objs = capfirst(formats.time_format(self.raw_value, 'TIME_FORMAT')) |
162 | 162 | else: |
163 | 163 | objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT')) |
164 | 164 | else: |
diff -r 1c03bc321e39 django/template/defaultfilters.py
a
|
b
|
|
18 | 18 | from django.utils.translation import ugettext, ungettext |
19 | 19 | from django.utils.encoding import force_unicode, iri_to_uri |
20 | 20 | from django.utils.safestring import mark_safe, SafeData |
21 | | from django.utils.formats import date_format, number_format |
| 21 | from django.utils.formats import date_format, time_format, number_format |
22 | 22 | |
23 | 23 | register = Library() |
24 | 24 | |
… |
… |
|
696 | 696 | |
697 | 697 | def time(value, arg=None): |
698 | 698 | """Formats a time according to the given format.""" |
699 | | from django.utils.dateformat import time_format |
| 699 | from django.utils.dateformat import time_format as df_time_format |
700 | 700 | if value in (None, u''): |
701 | 701 | return u'' |
702 | 702 | if arg is None: |
703 | 703 | arg = settings.TIME_FORMAT |
704 | 704 | try: |
705 | | return date_format(value, arg) |
| 705 | return time_format(value, arg) |
706 | 706 | except AttributeError: |
707 | 707 | try: |
708 | | return time_format(value, arg) |
| 708 | return df_time_format(value, arg) |
709 | 709 | except AttributeError: |
710 | 710 | return '' |
711 | 711 | time.is_safe = False |
diff -r 1c03bc321e39 django/utils/formats.py
a
|
b
|
|
53 | 53 | """ |
54 | 54 | return dateformat.format(value, get_format(format or 'DATE_FORMAT')) |
55 | 55 | |
| 56 | def time_format(value, format=None): |
| 57 | """ |
| 58 | Formats a datetime.time object using a localizable format |
| 59 | """ |
| 60 | return dateformat.time_format(value, get_format(format or 'TIME_FORMAT')) |
| 61 | |
56 | 62 | def number_format(value, decimal_pos=None): |
57 | 63 | """ |
58 | 64 | Formats a numeric value using localization settings |
… |
… |
|
90 | 96 | return value.strftime(get_format('DATE_INPUT_FORMATS')[0]) |
91 | 97 | elif isinstance(value, datetime.time): |
92 | 98 | if not is_input: |
93 | | return date_format(value, 'TIME_FORMAT') |
| 99 | return time_format(value, 'TIME_FORMAT') |
94 | 100 | else: |
95 | 101 | return value.strftime(get_format('TIME_INPUT_FORMATS')[0]) |
96 | 102 | return value |
diff -r 1c03bc321e39 tests/regressiontests/i18n/tests.py
a
|
b
|
|
4 | 4 | |
5 | 5 | from django.template import Template, Context |
6 | 6 | from django.conf import settings |
7 | | from django.utils.formats import get_format, date_format, number_format, localize |
| 7 | from django.utils.formats import get_format, date_format, time_format, number_format, localize |
8 | 8 | from django.utils.numberformat import format |
9 | 9 | from django.test import TestCase, client |
10 | 10 | from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy |
… |
… |
|
90 | 90 | self.f = 99999.999 |
91 | 91 | self.d = datetime.date(2009, 12, 31) |
92 | 92 | self.dt = datetime.datetime(2009, 12, 31, 20, 50) |
| 93 | self.t = datetime.time(10, 15, 48) |
93 | 94 | self.ctxt = Context({ |
94 | 95 | 'n': self.n, |
| 96 | 't': self.t, |
95 | 97 | 'd': self.d, |
96 | 98 | 'dt': self.dt, |
97 | 99 | 'f': self.f |
… |
… |
|
102 | 104 | settings.USE_I18N = self._use_i18n |
103 | 105 | settings.USE_L10N = self._use_l10n |
104 | 106 | settings.USE_THOUSAND_SEPARATOR = self._use_thousand_separator |
105 | | |
| 107 | |
106 | 108 | def test_locale_independent(self): |
107 | 109 | """ |
108 | | Localization of dates and numbers |
| 110 | Localization of numbers |
109 | 111 | """ |
110 | 112 | settings.USE_L10N = True |
111 | 113 | settings.USE_THOUSAND_SEPARATOR = False |
… |
… |
|
129 | 131 | self.assertEqual('N j, Y', get_format('DATE_FORMAT')) |
130 | 132 | self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) |
131 | 133 | self.assertEqual('.', get_format('DECIMAL_SEPARATOR')) |
| 134 | self.assertEqual(u'10:15 a.m.', time_format(self.t)) |
132 | 135 | self.assertEqual(u'des. 31, 2009', date_format(self.d)) |
133 | 136 | self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT')) |
134 | 137 | self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT')) |
… |
… |
|
143 | 146 | self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt)) |
144 | 147 | self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt)) |
145 | 148 | self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt)) |
| 149 | self.assertEqual(u'10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt)) |
146 | 150 | self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt)) |
147 | 151 | self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)) |
148 | 152 | |
… |
… |
|
183 | 187 | self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT')) |
184 | 188 | self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK')) |
185 | 189 | self.assertEqual(',', get_format('DECIMAL_SEPARATOR')) |
| 190 | self.assertEqual(u'10:15:48', time_format(self.t)) |
186 | 191 | self.assertEqual(u'31 de desembre de 2009', date_format(self.d)) |
187 | 192 | self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT')) |
188 | 193 | self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT')) |
… |
… |
|
209 | 214 | self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt)) |
210 | 215 | self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt)) |
211 | 216 | self.assertEqual(u'100000,0', Template('{{ f|floatformat }}').render(self.ctxt)) |
| 217 | self.assertEqual(u'10:15:48', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt)) |
212 | 218 | self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt)) |
213 | 219 | self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)) |
214 | 220 | |