diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-23 10:18:03 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-23 10:18:03 +0000 |
| commit | 174c8a0295c7e3f4782b5a0769de92e3f94bc801 (patch) | |
| tree | f3904f5b731f6314c55341bd5f859484d8cd8ec1 /django/utils | |
| parent | 15f1da053220394061d0f37447e545597cee6cd9 (diff) | |
| parent | 17f62269c2168549b76ee6204a1423a6e6f4e5f5 (diff) | |
i18n: merged to [992] from trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@993 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/dateformat.py | 37 | ||||
| -rw-r--r-- | django/utils/timesince.py | 18 | ||||
| -rw-r--r-- | django/utils/tzinfo.py | 52 |
3 files changed, 93 insertions, 14 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index 3620558f09..9913c3f8c4 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -12,8 +12,9 @@ Usage: """ from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS +from django.utils.tzinfo import LocalTimezone from calendar import isleap -import re +import re, time re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])') re_escaped = re.compile(r'\\(.)') @@ -40,7 +41,9 @@ class TimeFormat(Formatter): def A(self): "'AM' or 'PM'" - return self.a().upper() + if self.data.hour > 11: + return 'PM' + return 'AM' def B(self): "Swatch Internet time" @@ -100,8 +103,12 @@ class TimeFormat(Formatter): class DateFormat(TimeFormat): year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] - def __init__(self, d): - self.data = d + def __init__(self, dt): + # Accepts either a datetime or date object. + self.data = dt + self.timezone = getattr(dt, 'tzinfo', None) + if hasattr(self.data, 'hour') and not self.timezone: + self.timezone = LocalTimezone(dt) def d(self): "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'" @@ -119,6 +126,13 @@ class DateFormat(TimeFormat): "'1' if Daylight Savings Time, '0' otherwise." raise NotImplementedError + def I(self): + "'1' if Daylight Savings Time, '0' otherwise." + if self.timezone.dst(self.data): + return '1' + else: + return '0' + def j(self): "Day of the month without leading zeros; i.e. '1' to '31'" return self.data.day @@ -149,11 +163,12 @@ class DateFormat(TimeFormat): def O(self): "Difference to Greenwich time in hours; e.g. '+0200'" - raise NotImplementedError + tz = self.timezone.utcoffset(self.data) + return "%+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'" - raise NotImplementedError + return self.format('D, j M Y H:i:s O') def S(self): "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'" @@ -174,11 +189,15 @@ class DateFormat(TimeFormat): def T(self): "Time zone of this machine; e.g. 'EST' or 'MDT'" - raise NotImplementedError + name = self.timezone.tzname(self.data) + if name is None: + name = self.format('O') + return name def U(self): "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" - raise NotImplementedError + off = self.timezone.utcoffset(self.data) + return int(time.mktime(self.data.timetuple())) + off.seconds * 60 def w(self): "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)" @@ -229,7 +248,7 @@ class DateFormat(TimeFormat): """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.""" - raise NotImplementedError + return self.timezone.utcoffset(self.data).seconds def format(value, format_string): "Convenience function" diff --git a/django/utils/timesince.py b/django/utils/timesince.py index c11cef0342..5b22fde58c 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -1,4 +1,5 @@ -import time, math, datetime +import datetime, math, time +from django.utils.tzinfo import LocalTimezone def timesince(d, now=None): """ @@ -6,7 +7,6 @@ def timesince(d, now=None): as a nicely formatted string, e.g "10 minutes" Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ - original = time.mktime(d.timetuple()) chunks = ( (60 * 60 * 24 * 365, 'year'), (60 * 60 * 24 * 30, 'month'), @@ -14,9 +14,17 @@ def timesince(d, now=None): (60 * 60, 'hour'), (60, 'minute') ) - if not now: - now = time.time() - since = now - original + if now: + t = time.mktime(now) + else: + t = time.localtime() + if d.tzinfo: + tz = LocalTimezone() + else: + tz = None + now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) + delta = now - d + since = delta.days * 24 * 60 * 60 + delta.seconds # Crazy iteration syntax because we need i to be current index for i, (seconds, name) in zip(range(len(chunks)), chunks): count = math.floor(since / seconds) diff --git a/django/utils/tzinfo.py b/django/utils/tzinfo.py new file mode 100644 index 0000000000..cc9f028e91 --- /dev/null +++ b/django/utils/tzinfo.py @@ -0,0 +1,52 @@ +"Implementation of tzinfo classes for use with datetime.datetime." + +import time +from datetime import timedelta, tzinfo + +class FixedOffset(tzinfo): + "Fixed offset in minutes east from UTC." + def __init__(self, offset): + self.__offset = timedelta(minutes=offset) + self.__name = "%+03d%02d" % (offset // 60, offset % 60) + + def __repr__(self): + return self.__name + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return self.__name + + def dst(self, dt): + return timedelta(0) + +class LocalTimezone(tzinfo): + "Proxy timezone information from time module." + def __init__(self, dt): + tzinfo.__init__(self, dt) + self._tzname = time.tzname[self._isdst(dt)] + + def __repr__(self): + return self._tzname + + def utcoffset(self, dt): + if self._isdst(dt): + return timedelta(seconds=-time.altzone) + else: + return timedelta(seconds=-time.timezone) + + def dst(self, dt): + if self._isdst(dt): + return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone) + else: + return timedelta(0) + + def tzname(self, dt): + return time.tzname[self._isdst(dt)] + + def _isdst(self, dt): + tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1) + stamp = time.mktime(tt) + tt = time.localtime(stamp) + return tt.tm_isdst > 0 |
