diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-07-04 12:11:04 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-07-04 12:11:04 +0000 |
| commit | 953badbea5a04159adbfa970f5805c0232b6a401 (patch) | |
| tree | 9569f74b5d382b222613a1085efd0de21937e95f /django/utils/dateformat.py | |
| parent | 4c958b15b250866b70ded7d82aa532f1e57f96ae (diff) | |
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/dateformat.py')
| -rw-r--r-- | django/utils/dateformat.py | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index a558e3a69f..8a90e21cb1 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -11,9 +11,10 @@ Usage: >>> """ -from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS +from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR from django.utils.tzinfo import LocalTimezone -from django.utils.translation import gettext as _ +from django.utils.translation import string_concat, ugettext as _ +from django.utils.encoding import force_unicode from calendar import isleap, monthrange import re, time @@ -23,12 +24,12 @@ re_escaped = re.compile(r'\\(.)') class Formatter(object): def format(self, formatstr): pieces = [] - for i, piece in enumerate(re_formatchars.split(formatstr)): + for i, piece in enumerate(re_formatchars.split(force_unicode(formatstr))): if i % 2: - pieces.append(str(getattr(self, piece)())) + pieces.append(force_unicode(getattr(self, piece)())) elif piece: pieces.append(re_escaped.sub(r'\1', piece)) - return ''.join(pieces) + return u''.join(pieces) class TimeFormat(Formatter): def __init__(self, t): @@ -52,13 +53,14 @@ class TimeFormat(Formatter): def f(self): """ - Time, in 12-hour hours and minutes, with minutes left off if they're zero. + Time, in 12-hour hours and minutes, with minutes left off if they're + zero. Examples: '1', '1:30', '2:05', '2' Proprietary extension. """ if self.data.minute == 0: return self.g() - return '%s:%s' % (self.g(), self.i()) + return u'%s:%s' % (self.g(), self.i()) def g(self): "Hour, 12-hour format without leading zeros; i.e. '1' to '12'" @@ -74,15 +76,15 @@ class TimeFormat(Formatter): def h(self): "Hour, 12-hour format; i.e. '01' to '12'" - return '%02d' % self.g() + return u'%02d' % self.g() def H(self): "Hour, 24-hour format; i.e. '00' to '23'" - return '%02d' % self.G() + return u'%02d' % self.G() def i(self): "Minutes; i.e. '00' to '59'" - return '%02d' % self.data.minute + return u'%02d' % self.data.minute def P(self): """ @@ -95,11 +97,11 @@ class TimeFormat(Formatter): return _('midnight') if self.data.minute == 0 and self.data.hour == 12: return _('noon') - return '%s %s' % (self.f(), self.a()) + return u'%s %s' % (self.f(), self.a()) def s(self): "Seconds; i.e. '00' to '59'" - return '%02d' % self.data.second + return u'%02d' % self.data.second class DateFormat(TimeFormat): year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] @@ -117,11 +119,11 @@ class DateFormat(TimeFormat): def d(self): "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'" - return '%02d' % self.data.day + return u'%02d' % self.data.day def D(self): "Day of the week, textual, 3 letters; e.g. 'Fri'" - return WEEKDAYS[self.data.weekday()][0:3] + return WEEKDAYS_ABBR[self.data.weekday()] def F(self): "Month, textual, long; e.g. 'January'" @@ -130,9 +132,9 @@ class DateFormat(TimeFormat): def I(self): "'1' if Daylight Savings Time, '0' otherwise." if self.timezone.dst(self.data): - return '1' + return u'1' else: - return '0' + return u'0' def j(self): "Day of the month without leading zeros; i.e. '1' to '31'" @@ -148,7 +150,7 @@ class DateFormat(TimeFormat): def m(self): "Month; i.e. '01' to '12'" - return '%02d' % self.data.month + return u'%02d' % self.data.month def M(self): "Month, textual, 3 letters; e.g. 'Jan'" @@ -165,7 +167,7 @@ class DateFormat(TimeFormat): def O(self): "Difference to Greenwich time in hours; e.g. '+0200'" tz = self.timezone.utcoffset(self.data) - return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60) + return u"%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60) def r(self): "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" @@ -174,26 +176,26 @@ class DateFormat(TimeFormat): def S(self): "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'" if self.data.day in (11, 12, 13): # Special case - return 'th' + return u'th' last = self.data.day % 10 if last == 1: - return 'st' + return u'st' if last == 2: - return 'nd' + return u'nd' if last == 3: - return 'rd' - return 'th' + return u'rd' + return u'th' def t(self): "Number of days in the given month; i.e. '28' to '31'" - return '%02d' % monthrange(self.data.year, self.data.month)[1] + return u'%02d' % monthrange(self.data.year, self.data.month)[1] def T(self): "Time zone of this machine; e.g. 'EST' or 'MDT'" name = self.timezone.tzname(self.data) if name is None: name = self.format('O') - return name + return unicode(name) def U(self): "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" @@ -232,7 +234,7 @@ class DateFormat(TimeFormat): def y(self): "Year, 2 digits; e.g. '99'" - return str(self.data.year)[2:] + return unicode(self.data.year)[2:] def Y(self): "Year, 4 digits; e.g. '1999'" |
