2005-10-23 05:37:59 +08:00
|
|
|
"Implementation of tzinfo classes for use with datetime.datetime."
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
import time
|
|
|
|
from datetime import timedelta, tzinfo
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2012-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_str, force_text, DEFAULT_LOCALE_ENCODING
|
2005-10-23 05:37:59 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
# Python's doc say: "A tzinfo subclass must have an __init__() method that can
|
|
|
|
# be called with no arguments". FixedOffset and LocalTimezone don't honor this
|
|
|
|
# requirement. Defining __getinitargs__ is sufficient to fix copy/deepcopy as
|
|
|
|
# well as pickling/unpickling.
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
class FixedOffset(tzinfo):
|
|
|
|
"Fixed offset in minutes east from UTC."
|
|
|
|
def __init__(self, offset):
|
2008-08-06 01:38:49 +08:00
|
|
|
if isinstance(offset, timedelta):
|
|
|
|
self.__offset = offset
|
|
|
|
offset = self.__offset.seconds // 60
|
|
|
|
else:
|
|
|
|
self.__offset = timedelta(minutes=offset)
|
|
|
|
|
2011-09-26 01:08:31 +08:00
|
|
|
sign = '-' if offset < 0 else '+'
|
2012-06-08 00:08:47 +08:00
|
|
|
self.__name = "%s%02d%02d" % (sign, abs(offset) / 60., abs(offset) % 60)
|
2005-10-23 05:37:59 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.__name
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def __getinitargs__(self):
|
|
|
|
return self.__offset,
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
def utcoffset(self, dt):
|
|
|
|
return self.__offset
|
|
|
|
|
|
|
|
def tzname(self, dt):
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
return timedelta(0)
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
# This implementation is used for display purposes. It uses an approximation
|
|
|
|
# for DST computations on dates >= 2038.
|
|
|
|
|
|
|
|
# A similar implementation exists in django.utils.timezone. It's used for
|
|
|
|
# timezone support (when USE_TZ = True) and focuses on correctness.
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
class LocalTimezone(tzinfo):
|
|
|
|
"Proxy timezone information from time module."
|
|
|
|
def __init__(self, dt):
|
2008-08-09 02:33:02 +08:00
|
|
|
tzinfo.__init__(self)
|
2011-11-18 21:01:06 +08:00
|
|
|
self.__dt = dt
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self._tzname = self.tzname(dt)
|
2005-10-23 05:37:59 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2012-08-30 04:40:51 +08:00
|
|
|
return force_str(self._tzname)
|
2005-10-23 05:37:59 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
def __getinitargs__(self):
|
|
|
|
return self.__dt,
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
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):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
try:
|
2012-08-30 04:40:51 +08:00
|
|
|
return force_text(time.tzname[self._isdst(dt)],
|
2009-05-08 17:51:05 +08:00
|
|
|
DEFAULT_LOCALE_ENCODING)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
return None
|
2005-10-23 05:37:59 +08:00
|
|
|
|
|
|
|
def _isdst(self, dt):
|
2011-10-14 05:19:48 +08:00
|
|
|
tt = (dt.year, dt.month, dt.day,
|
|
|
|
dt.hour, dt.minute, dt.second,
|
|
|
|
dt.weekday(), 0, 0)
|
2007-11-30 03:39:46 +08:00
|
|
|
try:
|
|
|
|
stamp = time.mktime(tt)
|
2008-07-18 11:45:30 +08:00
|
|
|
except (OverflowError, ValueError):
|
|
|
|
# 32 bit systems can't handle dates after Jan 2038, and certain
|
|
|
|
# systems can't handle dates before ~1901-12-01:
|
|
|
|
#
|
|
|
|
# >>> time.mktime((1900, 1, 13, 0, 0, 0, 0, 0, 0))
|
|
|
|
# OverflowError: mktime argument out of range
|
|
|
|
# >>> time.mktime((1850, 1, 13, 0, 0, 0, 0, 0, 0))
|
|
|
|
# ValueError: year out of range
|
|
|
|
#
|
|
|
|
# In this case, we fake the date, because we only care about the
|
|
|
|
# DST flag.
|
2007-11-30 03:39:46 +08:00
|
|
|
tt = (2037,) + tt[1:]
|
|
|
|
stamp = time.mktime(tt)
|
2005-10-23 05:37:59 +08:00
|
|
|
tt = time.localtime(stamp)
|
|
|
|
return tt.tm_isdst > 0
|