2005-10-18 09:42:00 +04:00
|
|
|
"""tzinfo implementations for psycopg2
|
|
|
|
|
2005-11-26 10:47:48 +03:00
|
|
|
This module holds two different tzinfo implementations that can be used as
|
2005-10-18 09:42:00 +04:00
|
|
|
the 'tzinfo' argument to datetime constructors, directly passed to psycopg
|
2016-10-11 02:10:53 +03:00
|
|
|
functions or used to set the .tzinfo_factory attribute in cursors.
|
2005-10-18 09:42:00 +04:00
|
|
|
"""
|
2004-10-19 07:17:12 +04:00
|
|
|
# psycopg/tz.py - tzinfo implementation
|
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2003-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
|
|
|
ZERO = datetime.timedelta(0)
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
class FixedOffsetTimezone(datetime.tzinfo):
|
|
|
|
"""Fixed offset in minutes east from UTC.
|
|
|
|
|
2010-02-13 06:22:02 +03:00
|
|
|
This is exactly the implementation__ found in Python 2.3.x documentation,
|
2010-02-26 03:17:52 +03:00
|
|
|
with a small change to the `!__init__()` method to allow for pickling
|
2010-02-13 06:22:02 +03:00
|
|
|
and a default name in the form ``sHH:MM`` (``s`` is the sign.).
|
|
|
|
|
2012-01-25 20:23:19 +04:00
|
|
|
The implementation also caches instances. During creation, if a
|
|
|
|
FixedOffsetTimezone instance has previously been created with the same
|
|
|
|
offset and name that instance will be returned. This saves memory and
|
|
|
|
improves comparability.
|
|
|
|
|
2018-09-23 04:54:55 +03:00
|
|
|
.. __: https://docs.python.org/library/datetime.html
|
2004-10-19 07:17:12 +04:00
|
|
|
"""
|
|
|
|
_name = None
|
|
|
|
_offset = ZERO
|
2012-01-25 20:23:19 +04:00
|
|
|
|
|
|
|
_cache = {}
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
def __init__(self, offset=None, name=None):
|
|
|
|
if offset is not None:
|
2016-10-11 02:10:53 +03:00
|
|
|
self._offset = datetime.timedelta(minutes=offset)
|
2004-10-19 07:17:12 +04:00
|
|
|
if name is not None:
|
|
|
|
self._name = name
|
|
|
|
|
2012-01-25 20:23:19 +04:00
|
|
|
def __new__(cls, offset=None, name=None):
|
|
|
|
"""Return a suitable instance created earlier if it exists
|
|
|
|
"""
|
|
|
|
key = (offset, name)
|
|
|
|
try:
|
|
|
|
return cls._cache[key]
|
|
|
|
except KeyError:
|
2012-10-22 00:16:52 +04:00
|
|
|
tz = super(FixedOffsetTimezone, cls).__new__(cls, offset, name)
|
2012-01-25 20:23:19 +04:00
|
|
|
cls._cache[key] = tz
|
|
|
|
return tz
|
|
|
|
|
2010-05-05 03:01:24 +04:00
|
|
|
def __repr__(self):
|
2012-01-17 19:51:32 +04:00
|
|
|
offset_mins = self._offset.seconds // 60 + self._offset.days * 24 * 60
|
2010-05-05 03:01:24 +04:00
|
|
|
return "psycopg2.tz.FixedOffsetTimezone(offset=%r, name=%r)" \
|
2012-01-17 19:51:32 +04:00
|
|
|
% (offset_mins, self._name)
|
2010-05-05 03:01:24 +04:00
|
|
|
|
2012-10-22 00:47:32 +04:00
|
|
|
def __getinitargs__(self):
|
|
|
|
offset_mins = self._offset.seconds // 60 + self._offset.days * 24 * 60
|
2017-11-28 11:32:55 +03:00
|
|
|
return offset_mins, self._name
|
2012-10-22 00:47:32 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
def utcoffset(self, dt):
|
|
|
|
return self._offset
|
|
|
|
|
|
|
|
def tzname(self, dt):
|
|
|
|
if self._name is not None:
|
|
|
|
return self._name
|
|
|
|
else:
|
|
|
|
seconds = self._offset.seconds + self._offset.days * 86400
|
|
|
|
hours, seconds = divmod(seconds, 3600)
|
2016-10-11 02:10:53 +03:00
|
|
|
minutes = seconds / 60
|
2004-10-19 07:17:12 +04:00
|
|
|
if minutes:
|
|
|
|
return "%+03d:%d" % (hours, minutes)
|
|
|
|
else:
|
|
|
|
return "%+03d" % hours
|
2012-10-22 00:47:32 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
def dst(self, dt):
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
STDOFFSET = datetime.timedelta(seconds=-time.timezone)
|
2004-10-19 07:17:12 +04:00
|
|
|
if time.daylight:
|
2016-10-11 02:10:53 +03:00
|
|
|
DSTOFFSET = datetime.timedelta(seconds=-time.altzone)
|
2004-10-19 07:17:12 +04:00
|
|
|
else:
|
|
|
|
DSTOFFSET = STDOFFSET
|
|
|
|
DSTDIFF = DSTOFFSET - STDOFFSET
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
class LocalTimezone(datetime.tzinfo):
|
|
|
|
"""Platform idea of local timezone.
|
|
|
|
|
2010-05-05 02:44:33 +04:00
|
|
|
This is the exact implementation from the Python 2.3 documentation.
|
2004-10-19 07:17:12 +04:00
|
|
|
"""
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
if self._isdst(dt):
|
|
|
|
return DSTOFFSET
|
|
|
|
else:
|
|
|
|
return STDOFFSET
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
if self._isdst(dt):
|
|
|
|
return DSTDIFF
|
|
|
|
else:
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
LOCAL = LocalTimezone()
|
|
|
|
|
|
|
|
# TODO: pre-generate some interesting time zones?
|