Ticket #4121: dateformat.py.2.diff

File dateformat.py.2.diff, 2.7 KB (added by Jari Pennanen, 17 years ago)

DateFormat, TimeFormat features for locale reprs. (Few code-beauty errors fixed.)

  • django/utils/dateformat.py

     
    1313
    1414from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS
    1515from django.utils.tzinfo import LocalTimezone
    16 from django.utils.translation import gettext as _
     16from django.utils.translation import get_date_formats, get_partial_date_formats, gettext as _
    1717from calendar import isleap, monthrange
    1818import re, time
    1919
    20 re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
     20re_formatchars = re.compile(r'(?<!\\)([aAbcBdDfFgGhHiIjkKlLmMnNOPrsStTUwWxXyYzZ])')
    2121re_escaped = re.compile(r'\\(.)')
    2222
    2323class Formatter(object):
     
    101101        "Seconds; i.e. '00' to '59'"
    102102        return '%02d' % self.data.second
    103103
     104    def X(self):
     105        "Time, appropriate time representation. e.g. '8:00'"
     106        (date_format, datetime_format, time_format) = get_date_formats()
     107        return format(self.data, time_format)     
     108
    104109class DateFormat(TimeFormat):
    105110    year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
    106111
     
    115120        "Month, textual, 3 letters, lowercase; e.g. 'jan'"
    116121        return MONTHS_3[self.data.month]
    117122
     123    def c(self):
     124        "Datetime, appropriate date and time representation.; e.g. '2007-04-23 05:00'"
     125        (date_format, datetime_format, time_format) = get_date_formats()
     126        return format(self.data, datetime_format)
     127
    118128    def d(self):
    119129        "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
    120130        return '%02d' % self.data.day
     
    138148        "Day of the month without leading zeros; i.e. '1' to '31'"
    139149        return self.data.day
    140150
     151    def k(self):
     152        "Month day, appropriate month day representation. e.g. 'January 5'"
     153        (year_month_format, month_day_format) = get_partial_date_formats()
     154        return format(self.data, month_day_format)           
     155
     156    def K(self):
     157        "Year month, appropriate year month representation. e.g. '2007 January'"
     158        (year_month_format, month_day_format) = get_partial_date_formats()
     159        return format(self.data, year_month_format)   
     160
    141161    def l(self):
    142162        "Day of the week, textual, long; e.g. 'Friday'"
    143163        return WEEKDAYS[self.data.weekday()]
     
    230250                    week_number -= 1
    231251        return week_number
    232252
     253    def x(self):
     254        "Date, appropriate date representation. e.g. '2007-04-23'"
     255        (date_format, datetime_format, time_format) = get_date_formats()
     256        return format(self.data, date_format)       
     257
    233258    def y(self):
    234259        "Year, 2 digits; e.g. '99'"
    235260        return str(self.data.year)[2:]
Back to Top