Ticket #479: django-tzinfo.patch

File django-tzinfo.patch, 4.9 KB (added by sune.kirkeby@…, 9 years ago)
  • utils/tzinfo.py

     
     1"""Implementation of a tzinfo-classes for use with datetime.datetime."""
     2
     3import time
     4from datetime import timedelta, tzinfo
     5
     6ZERO = timedelta(0)
     7STDOFFSET = timedelta(seconds=-time.timezone)
     8DSTOFFSET = timedelta(seconds=-time.altzone)
     9DSTDIFF = DSTOFFSET - STDOFFSET
     10
     11class 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
     28class 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

     
    1111>>>
    1212"""
    1313
     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
    1419from calendar import isleap
    1520from dates import MONTHS, MONTHS_AP, WEEKDAYS
     21from tzinfo import LocalTimezone
    1622
    1723class DateFormat:
    1824    year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
     
    2026    def __init__(self, d):
    2127        self.date = d
    2228
     29        if self.date.tzinfo:
     30            self.timezone = self.date.tzinfo
     31        else:
     32            self.timezone = LocalTimezone(self.date)
     33
    2334    def a(self):
    2435        "'a.m.' or 'p.m.'"
    2536        if self.date.hour > 11:
     
    8495
    8596    def I(self):
    8697        "'1' if Daylight Savings Time, '0' otherwise."
    87         raise NotImplementedError
     98        return self.timezone.dst()
    8899
    89100    def j(self):
    90101        "Day of the month without leading zeros; i.e. '1' to '31'"
     
    116127
    117128    def O(self):
    118129        "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)
    120132
    121133    def P(self):
    122134        """
     
    158170
    159171    def T(self):
    160172        "Time zone of this machine; e.g. 'EST' or 'MDT'"
    161         raise NotImplementedError
     173        return self.timezone.tzname(self.date)
    162174
    163175    def U(self):
    164176        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
     
    216228        """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset
    217229        for timezones west of UTC is always negative, and for those east of UTC
    218230        is always positive."""
    219         raise NotImplementedError
     231        return self.timezone.utcoffset(self.date).seconds
    220232
    221233    def format(self, formatstr):
    222234        result = ''
    223235        for char in formatstr:
    224             try:
     236            if hasattr(self, char):
    225237                result += str(getattr(self, char)())
    226             except AttributeError:
     238            else:
    227239                result += char
    228240        return result
    229241
  • core/db/typecasts.py

     
    11import datetime
     2from django.utils.tzinfo import FixedOffset
    23
    34###############################################
    45# Converters from database (string) to Python #
     
    2526    # it away, but in the future we may make use of it.
    2627    if '-' in t:
    2728        t, tz = t.split('-', 1)
    28         tz = '-' + tz
     29        tz = - int(tz) * 60
    2930    elif '+' in t:
    3031        t, tz = t.split('+', 1)
    31         tz = '+' + tz
     32        tz = int(tz) * 60
    3233    else:
    33         tz = ''
     34        tz = 0
    3435    dates = d.split('-')
    3536    times = t.split(':')
    3637    seconds = times[2]
     
    3940    else:
    4041        microseconds = '0'
    4142    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))
    4346
    4447def typecast_boolean(s):
    4548    if s is None: return None
Back to Top