diff options
| author | Justin Bronn <jbronn@gmail.com> | 2007-08-26 01:10:53 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2007-08-26 01:10:53 +0000 |
| commit | 2052b508eb92c62fc0678efd4936c5ec1e0e735b (patch) | |
| tree | e510109b74b28c8ccef5f6955727cb9dce3da655 /django/utils/dateformat.py | |
| parent | a7297a255f4bb86f608ea251e00253d18c31d9d4 (diff) | |
gis: Made necessary modifications for unicode, manage refactor, backend refactor and merged 5584-6000 via svnmerge from [repos:django/trunk trunk].
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6018 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/dateformat.py')
| -rw-r--r-- | django/utils/dateformat.py | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index a558e3a69f..d5f3499d82 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)" @@ -225,14 +227,14 @@ class DateFormat(TimeFormat): week_number = 1 else: j = day_of_year + (7 - weekday) + (jan1_weekday - 1) - week_number = j / 7 + week_number = j // 7 if jan1_weekday > 4: week_number -= 1 return week_number 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'" |
