PyICU

Module PyICU |
[hide private]
[frames] | no frames]

# Source Code for Module PyICU

      1   # ====================================================================
      2   # Copyright (c) 2004-2006 Open Source Applications Foundation.
      3   #
      4   # Permission is hereby granted, free of charge, to any person obtaining a
      5   # copy of this software and associated documentation files (the "Software"),
      6   # to deal in the Software without restriction, including without limitation
      7   # the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8   # and/or sell copies of the Software, and to permit persons to whom the
      9   # Software is furnished to do so, subject to the following conditions:
     10   #
     11   # The above copyright notice and this permission notice shall be included
     12   # in all copies or substantial portions of the Software.
     13   #
     14   # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     15   # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16   # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17   # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18   # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19   # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     20   # DEALINGS IN THE SOFTWARE.
     21   # ====================================================================
     22
     23
     24 -class ICUError(Exception):
     25      messages = {}
     26
     27 -    def __str__(self):
     28          return "%s, error code: %d" %(self.args[1], self.args[0])
     29
     30 -    def getErrorCode(self):
     31          return self.args[0]
     32
     33
     34 -class InvalidArgsError(Exception):
     35      pass
     36
     37
     38  from _PyICU import *
     39
     40
     41  from datetime import tzinfo, timedelta
     42  FLOATING_TZNAME = "World/Floating"
     43
     44 -class ICUtzinfo(tzinfo):
     45
     46      instances = {}
     47
     48 -    def _resetDefault(cls):
     49          cls.default = ICUtzinfo(TimeZone.createDefault())
     50      _resetDefault = classmethod(_resetDefault)
     51
     52 -    def getInstance(cls, id):
     53          try:
     54              return cls.instances[id]
     55          except KeyError:
     56              if id == FLOATING_TZNAME:
     57                  instance = cls.floating
     58              else:
     59                  instance = cls(TimeZone.createTimeZone(id))
     60              cls.instances[id] = instance
     61              return instance
     62      getInstance = classmethod(getInstance)
     63
     64 -    def getDefault(cls):
     65          return cls.default
     66      getDefault = classmethod(getDefault)
     67
     68 -    def getFloating(cls):
     69          return cls.floating
     70      getFloating = classmethod(getFloating)
     71
     72 -    def __init__(self, timezone):
     73          if not isinstance(timezone, TimeZone):
     74              raise TypeError, timezone
     75          super(ICUtzinfo, self).__init__()
     76          self._timezone = timezone
     77
     78 -    def __repr__(self):
     79          return "<ICUtzinfo: %s>" %(self._timezone.getID())
     80
     81 -    def __str__(self):
     82          return str(self._timezone.getID())
     83
     84 -    def __eq__(self, other):
     85          if isinstance(other, ICUtzinfo):
     86              return str(self) == str(other)
     87          return False
     88
     89 -    def __ne__(self, other):
     90          if isinstance(other, ICUtzinfo):
     91              return str(self) != str(other)
     92          return True
     93
     94 -    def __hash__(self):
     95          return hash(self.tzid)
     96
     97 -    def _notzsecs(self, dt):
     98          return ((dt.toordinal() - 719163) * 86400.0 +
     99                  dt.hour * 3600.0 + dt.minute * 60.0 +
    100                  float(dt.second) + dt.microsecond / 1e6)
    101
    102 -    def utcoffset(self, dt):
    103          raw, dst = self._timezone.getOffset(self._notzsecs(dt), True)
    104          return timedelta(seconds = (raw + dst) / 1000)
    105
    106 -    def dst(self, dt):
    107          raw, dst = self._timezone.getOffset(self._notzsecs(dt), True)
    108          return timedelta(seconds = dst / 1000)
    109
    110 -    def tzname(self, dt):
    111          return str(self._timezone.getID())
    112
    113 -    def _getTimezone(self):
    114          return TimeZone.createTimeZone(self._timezone.getID())
    115
    116      tzid = property(__str__)
    117      timezone = property(_getTimezone)
    118
    119
    120 -class FloatingTZ(ICUtzinfo):
    121
    122 -    def __init__(self):
    123          pass
    124
    125 -    def __repr__(self):
    126          return "<FloatingTZ: %s>" %(ICUtzinfo.default._timezone.getID())
    127
    128 -    def __str__(self):
    129          return FLOATING_TZNAME
    130
    131 -    def __hash__(self):
    132          return hash(FLOATING_TZNAME)
    133
    134 -    def utcoffset(self, dt):
    135          tz = ICUtzinfo.default._timezone
    136          raw, dst = tz.getOffset(self._notzsecs(dt), True)
    137          return timedelta(seconds = (raw + dst) / 1000)
    138
    139 -    def dst(self, dt):
    140          tz = ICUtzinfo.default._timezone
    141          raw, dst = tz.getOffset(self._notzsecs(dt), True)
    142          return timedelta(seconds = dst / 1000)
    143
    144 -    def _getTimezone(self):
    145          return TimeZone.createTimeZone(ICUtzinfo.default._timezone.getID())
    146
    147 -    def __getTimezone(self):
    148          return ICUtzinfo.default._timezone
    149
    150 -    def tzname(self, dt):
    151          return FLOATING_TZNAME
    152
    153      tzid = FLOATING_TZNAME
    154      _timezone = property(__getTimezone)
    155
    156
    157  ICUtzinfo.default = ICUtzinfo(TimeZone.createDefault())
    158  ICUtzinfo.floating = FloatingTZ()
    159