Ticket #479: django-tzinfo.patch
File django-tzinfo.patch, 4.9 KB (added by , 19 years ago) |
---|
-
utils/tzinfo.py
1 """Implementation of a tzinfo-classes for use with datetime.datetime.""" 2 3 import time 4 from datetime import timedelta, tzinfo 5 6 ZERO = timedelta(0) 7 STDOFFSET = timedelta(seconds=-time.timezone) 8 DSTOFFSET = timedelta(seconds=-time.altzone) 9 DSTDIFF = DSTOFFSET - STDOFFSET 10 11 class FixedOffset(tzinfo): 12 """Fixed offset in minutes east from UTC.""" 13 14 def __init__(self, offset): 15 self.__offset = timedelta(minutes=offset) 16 # FIXME -- Not really a name... 17 self.__name = "%+03d%02d" % (offset / 60, offset % 60) 18 19 def utcoffset(self, dt): 20 return self.__offset 21 22 def tzname(self, dt): 23 return self.__name 24 25 def dst(self, dt): 26 return ZERO 27 28 class LocalTimezone(tzinfo): 29 """Proxy timezone information from time module.""" 30 31 def utcoffset(self, dt): 32 if self._isdst(dt): 33 return DSTOFFSET 34 else: 35 return STDOFFSET 36 37 def dst(self, dt): 38 if self._isdst(dt): 39 return DSTDIFF 40 else: 41 return ZERO 42 43 def tzname(self, dt): 44 return time.tzname[self._isdst(dt)] 45 46 def _isdst(self, dt): 47 tt = (dt.year, dt.month, dt.day, 48 dt.hour, dt.minute, dt.second, 49 dt.weekday(), 0, -1) 50 stamp = time.mktime(tt) 51 tt = time.localtime(stamp) 52 return tt.tm_isdst > 0 -
utils/dateformat.py
11 11 >>> 12 12 """ 13 13 14 # FIXME -- Try this in a template and weep 15 # { now:"\T\h\i\s \i\s \e\s\c\a\p\e\d" } 16 # or 17 # { now:"\\T\\h\\i\\s \\i\\s \\e\\s\\c\\a\\p\\e\\d" } 18 14 19 from calendar import isleap 15 20 from dates import MONTHS, MONTHS_AP, WEEKDAYS 21 from tzinfo import LocalTimezone 16 22 17 23 class DateFormat: 18 24 year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] … … 20 26 def __init__(self, d): 21 27 self.date = d 22 28 29 if self.date.tzinfo: 30 self.timezone = self.date.tzinfo 31 else: 32 self.timezone = LocalTimezone(self.date) 33 23 34 def a(self): 24 35 "'a.m.' or 'p.m.'" 25 36 if self.date.hour > 11: … … 84 95 85 96 def I(self): 86 97 "'1' if Daylight Savings Time, '0' otherwise." 87 r aise NotImplementedError98 return self.timezone.dst() 88 99 89 100 def j(self): 90 101 "Day of the month without leading zeros; i.e. '1' to '31'" … … 116 127 117 128 def O(self): 118 129 "Difference to Greenwich time in hours; e.g. '+0200'" 119 raise NotImplementedError 130 tz = self.timezone.utcoffset(self.date) 131 return "%+03d%02d" % (tz.seconds / 3600, (tz.seconds / 60) % 60) 120 132 121 133 def P(self): 122 134 """ … … 158 170 159 171 def T(self): 160 172 "Time zone of this machine; e.g. 'EST' or 'MDT'" 161 r aise NotImplementedError173 return self.timezone.tzname(self.date) 162 174 163 175 def U(self): 164 176 "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" … … 216 228 """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset 217 229 for timezones west of UTC is always negative, and for those east of UTC 218 230 is always positive.""" 219 r aise NotImplementedError231 return self.timezone.utcoffset(self.date).seconds 220 232 221 233 def format(self, formatstr): 222 234 result = '' 223 235 for char in formatstr: 224 try:236 if hasattr(self, char): 225 237 result += str(getattr(self, char)()) 226 e xcept AttributeError:238 else: 227 239 result += char 228 240 return result 229 241 -
core/db/typecasts.py
1 1 import datetime 2 from django.utils.tzinfo import FixedOffset 2 3 3 4 ############################################### 4 5 # Converters from database (string) to Python # … … 25 26 # it away, but in the future we may make use of it. 26 27 if '-' in t: 27 28 t, tz = t.split('-', 1) 28 tz = '-' + tz29 tz = - int(tz) * 60 29 30 elif '+' in t: 30 31 t, tz = t.split('+', 1) 31 tz = '+' + tz32 tz = int(tz) * 60 32 33 else: 33 tz = ''34 tz = 0 34 35 dates = d.split('-') 35 36 times = t.split(':') 36 37 seconds = times[2] … … 39 40 else: 40 41 microseconds = '0' 41 42 return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]), 42 int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000)) 43 int(times[0]), int(times[1]), int(seconds), 44 int(float('.'+microseconds) * 1000000), 45 tzinfo=FixedOffset(tz)) 43 46 44 47 def typecast_boolean(s): 45 48 if s is None: return None