Ticket #479: timezone.2.patch
File timezone.2.patch, 6.6 KB (added by , 19 years ago) |
---|
-
django/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 -
django/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 self.timezone = getattr(self.date, 'tzinfo', None) 30 if not self.timezone: 31 self.timezone = LocalTimezone(self.date) 32 23 33 def a(self): 24 34 "'a.m.' or 'p.m.'" 25 35 if self.date.hour > 11: … … 84 94 85 95 def I(self): 86 96 "'1' if Daylight Savings Time, '0' otherwise." 87 r aise NotImplementedError97 return self.timezone.dst() 88 98 89 99 def j(self): 90 100 "Day of the month without leading zeros; i.e. '1' to '31'" … … 116 126 117 127 def O(self): 118 128 "Difference to Greenwich time in hours; e.g. '+0200'" 119 raise NotImplementedError 129 tz = self.timezone.utcoffset(self.date) 130 return "%+03d%02d" % (tz.seconds / 3600, (tz.seconds / 60) % 60) 120 131 121 132 def P(self): 122 133 """ … … 158 169 159 170 def T(self): 160 171 "Time zone of this machine; e.g. 'EST' or 'MDT'" 161 r aise NotImplementedError172 return self.timezone.tzname(self.date) 162 173 163 174 def U(self): 164 175 "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" … … 216 227 """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset 217 228 for timezones west of UTC is always negative, and for those east of UTC 218 229 is always positive.""" 219 r aise NotImplementedError230 return self.timezone.utcoffset(self.date).seconds 220 231 221 232 def format(self, formatstr): 222 233 result = '' 223 234 for char in formatstr: 224 try:235 if hasattr(self, char): 225 236 result += str(getattr(self, char)()) 226 e xcept AttributeError:237 else: 227 238 result += char 228 239 return result 229 240 -
django/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 # … … 16 17 microseconds = '0' 17 18 return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000)) 18 19 20 def parse_tz(tz): 21 if ':' in tz: 22 h, m = tz.split(':') 23 else: 24 h, m = tz, '0' 25 return int(h) * 60 + int(m) 19 26 def typecast_timestamp(s): # does NOT store time zone information 20 27 # "2005-07-29 15:48:00.590358-05" 21 28 # "2005-07-29 09:56:00-05" … … 25 32 # it away, but in the future we may make use of it. 26 33 if '-' in t: 27 34 t, tz = t.split('-', 1) 28 tz = '-' + tz35 tz = - parse_tz(tz) 29 36 elif '+' in t: 30 37 t, tz = t.split('+', 1) 31 tz = '+' + tz38 tz = parse_tz(tz) 32 39 else: 33 tz = ''40 tz = 0 34 41 dates = d.split('-') 35 42 times = t.split(':') 36 43 seconds = times[2] … … 39 46 else: 40 47 microseconds = '0' 41 48 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)) 49 int(times[0]), int(times[1]), int(seconds), 50 int(float('.'+microseconds) * 1000000), 51 tzinfo=FixedOffset(tz)) 43 52 44 53 def typecast_boolean(s): 45 54 if s is None: return None -
django/utils/timesince.py
1 1 import time, math, datetime 2 from tzinfo import LocalTimezone 2 3 3 4 def timesince(d, now=None): 4 5 """ … … 6 7 as a nicely formatted string, e.g "10 minutes" 7 8 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 8 9 """ 9 original = time.mktime(d.timetuple())10 10 chunks = ( 11 11 (60 * 60 * 24 * 365, 'year'), 12 12 (60 * 60 * 24 * 30, 'month'), … … 14 14 (60 * 60, 'hour'), 15 15 (60, 'minute') 16 16 ) 17 if not now: 18 now = time.time() 19 since = now - original 17 if now: 18 t = time.mktime(now) 19 else: 20 t = time.localtime() 21 if d.tzinfo: 22 tz = LocalTimezone() 23 else: 24 tz = None 25 now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 26 delta = now - d 27 since = delta.days * 24 * 60 * 60 + delta.seconds 20 28 # Crazy iteration syntax because we need i to be current index 21 29 for i, (seconds, name) in zip(range(len(chunks)), chunks): 22 30 count = math.floor(since / seconds)