Package psycopg2 :: Module tz
[hide private]
[frames] | no frames]

Source Code for Module psycopg2.tz

  1  """tzinfo implementations for psycopg2 
  2   
  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.  
  6  """ 
  7  # psycopg/tz.py - tzinfo implementation 
  8  # 
  9  # Copyright (C) 2003-2004 Federico Di Gregorio  <fog@debian.org> 
 10  # 
 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 
 14  # version. 
 15  # 
 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 
 19  # for more details. 
 20   
 21  import datetime 
 22  import time 
 23   
 24   
 25  ZERO = datetime.timedelta(0) 
 26   
27 -class FixedOffsetTimezone(datetime.tzinfo):
28 """Fixed offset in minutes east from UTC. 29 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.) 33 """ 34 _name = None 35 _offset = ZERO 36
37 - def __init__(self, offset=None, name=None):
38 if offset is not None: 39 self._offset = datetime.timedelta(minutes = offset) 40 if name is not None: 41 self._name = name
42
43 - def utcoffset(self, dt):
44 return self._offset
45
46 - def tzname(self, dt):
47 if self._name is not None: 48 return self._name 49 else: 50 seconds = self._offset.seconds + self._offset.days * 86400 51 hours, seconds = divmod(seconds, 3600) 52 minutes = seconds/60 53 if minutes: 54 return "%+03d:%d" % (hours, minutes) 55 else: 56 return "%+03d" % hours
57
58 - def dst(self, dt):
59 return ZERO
60 61 62 STDOFFSET = datetime.timedelta(seconds = -time.timezone) 63 if time.daylight: 64 DSTOFFSET = datetime.timedelta(seconds = -time.altzone) 65 else: 66 DSTOFFSET = STDOFFSET 67 DSTDIFF = DSTOFFSET - STDOFFSET 68
69 -class LocalTimezone(datetime.tzinfo):
70 """Platform idea of local timezone. 71 72 This is the exact implementation from the Pyhton 2.3 documentation. 73 """ 74
75 - def utcoffset(self, dt):
76 if self._isdst(dt): 77 return DSTOFFSET 78 else: 79 return STDOFFSET
80
81 - def dst(self, dt):
82 if self._isdst(dt): 83 return DSTDIFF 84 else: 85 return ZERO
86
87 - def tzname(self, dt):
88 return time.tzname[self._isdst(dt)]
89
90 - def _isdst(self, dt):
91 tt = (dt.year, dt.month, dt.day, 92 dt.hour, dt.minute, dt.second, 93 dt.weekday(), 0, -1) 94 stamp = time.mktime(tt) 95 tt = time.localtime(stamp) 96 return tt.tm_isdst > 0
97 98 LOCAL = LocalTimezone() 99 100 # TODO: pre-generate some interesting time zones? 101