1
"""tzinfo implementations for psycopg2
3
This module holds two different tzinfo implementations that can be used as
4
the 'tzinfo' argument to datetime constructors, directly passed to psycopg
5
functions or used to set the .tzinfo_factory attribute in cursors.
7
# psycopg/tz.py - tzinfo implementation
9
# Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
11
# This program is free software; you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by the
13
# Free Software Foundation; either version 2, or (at your option) any later
16
# This program is distributed in the hope that it will be useful, but
17
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
18
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25
ZERO = datetime.timedelta(0)
27
class FixedOffsetTimezone(datetime.tzinfo):
28
"""Fixed offset in minutes east from UTC.
30
This is exactly the implementation found in Python 2.3.x documentation,
31
with a small change to the __init__ method to allow for pickling and a
32
default name in the form 'sHH:MM' ('s' is the sign.)
37
def __init__(self, offset=None, name=None):
38
if offset is not None:
39
self._offset = datetime.timedelta(minutes = offset)
43
def utcoffset(self, dt):
47
if self._name is not None:
50
seconds = self._offset.seconds + self._offset.days * 86400
51
hours, seconds = divmod(seconds, 3600)
54
return "%+03d:%d" % (hours, minutes)
56
return "%+03d" % hours
62
STDOFFSET = datetime.timedelta(seconds = -time.timezone)
64
DSTOFFSET = datetime.timedelta(seconds = -time.altzone)
67
DSTDIFF = DSTOFFSET - STDOFFSET
69
class LocalTimezone(datetime.tzinfo):
70
"""Platform idea of local timezone.
72
This is the exact implementation from the Pyhton 2.3 documentation.
75
def utcoffset(self, dt):
88
return time.tzname[self._isdst(dt)]
91
tt = (dt.year, dt.month, dt.day,
92
dt.hour, dt.minute, dt.second,
94
stamp = time.mktime(tt)
95
tt = time.localtime(stamp)
96
return tt.tm_isdst > 0
98
LOCAL = LocalTimezone()
100
# TODO: pre-generate some interesting time zones?