Ticket #12472: 12472.diff

File 12472.diff, 6.6 KB (added by Ramiro Morales, 15 years ago)
  • django/contrib/databrowse/datastructures.py

    diff -r 1c03bc321e39 django/contrib/databrowse/datastructures.py
    a b  
    158158                if isinstance(self.field, models.DateTimeField):
    159159                    objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT'))
    160160                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'))
    162162                else:
    163163                    objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT'))
    164164            else:
  • django/template/defaultfilters.py

    diff -r 1c03bc321e39 django/template/defaultfilters.py
    a b  
    1818from django.utils.translation import ugettext, ungettext
    1919from django.utils.encoding import force_unicode, iri_to_uri
    2020from django.utils.safestring import mark_safe, SafeData
    21 from django.utils.formats import date_format, number_format
     21from django.utils.formats import date_format, time_format, number_format
    2222
    2323register = Library()
    2424
     
    696696
    697697def time(value, arg=None):
    698698    """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
    700700    if value in (None, u''):
    701701        return u''
    702702    if arg is None:
    703703        arg = settings.TIME_FORMAT
    704704    try:
    705         return date_format(value, arg)
     705        return time_format(value, arg)
    706706    except AttributeError:
    707707        try:
    708             return time_format(value, arg)
     708            return df_time_format(value, arg)
    709709        except AttributeError:
    710710            return ''
    711711time.is_safe = False
  • django/utils/formats.py

    diff -r 1c03bc321e39 django/utils/formats.py
    a b  
    5353    """
    5454    return dateformat.format(value, get_format(format or 'DATE_FORMAT'))
    5555
     56def 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
    5662def number_format(value, decimal_pos=None):
    5763    """
    5864    Formats a numeric value using localization settings
     
    9096                return value.strftime(get_format('DATE_INPUT_FORMATS')[0])
    9197        elif isinstance(value, datetime.time):
    9298            if not is_input:
    93                 return date_format(value, 'TIME_FORMAT')
     99                return time_format(value, 'TIME_FORMAT')
    94100            else:
    95101                return value.strftime(get_format('TIME_INPUT_FORMATS')[0])
    96102    return value
  • tests/regressiontests/i18n/tests.py

    diff -r 1c03bc321e39 tests/regressiontests/i18n/tests.py
    a b  
    44
    55from django.template import Template, Context
    66from django.conf import settings
    7 from django.utils.formats import get_format, date_format, number_format, localize
     7from django.utils.formats import get_format, date_format, time_format, number_format, localize
    88from django.utils.numberformat import format
    99from django.test import TestCase, client
    1010from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy
     
    9090        self.f = 99999.999
    9191        self.d = datetime.date(2009, 12, 31)
    9292        self.dt = datetime.datetime(2009, 12, 31, 20, 50)
     93        self.t = datetime.time(10, 15, 48)
    9394        self.ctxt = Context({
    9495            'n': self.n,
     96            't': self.t,
    9597            'd': self.d,
    9698            'dt': self.dt,
    9799            'f': self.f
     
    102104        settings.USE_I18N = self._use_i18n
    103105        settings.USE_L10N = self._use_l10n
    104106        settings.USE_THOUSAND_SEPARATOR = self._use_thousand_separator
    105    
     107
    106108    def test_locale_independent(self):
    107109        """
    108         Localization of dates and numbers
     110        Localization of numbers
    109111        """
    110112        settings.USE_L10N = True
    111113        settings.USE_THOUSAND_SEPARATOR = False
     
    129131            self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
    130132            self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
    131133            self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
     134            self.assertEqual(u'10:15 a.m.', time_format(self.t))
    132135            self.assertEqual(u'des. 31, 2009', date_format(self.d))
    133136            self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
    134137            self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
     
    143146            self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
    144147            self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
    145148            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))
    146150            self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
    147151            self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
    148152
     
    183187            self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
    184188            self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
    185189            self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
     190            self.assertEqual(u'10:15:48', time_format(self.t))
    186191            self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
    187192            self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
    188193            self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
     
    209214            self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
    210215            self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
    211216            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))
    212218            self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
    213219            self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
    214220
Back to Top