home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2373 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.6 KB  |  156 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import datetime
  5. import struct
  6. import _winreg
  7. __author__ = 'Jeffrey Harris & Gustavo Niemeyer <gustavo@niemeyer.net>'
  8. __all__ = [
  9.     'tzwin',
  10.     'tzwinlocal']
  11. ONEWEEK = datetime.timedelta(7)
  12. TZKEYNAMENT = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones'
  13. TZKEYNAME9X = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones'
  14. TZLOCALKEYNAME = 'SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation'
  15.  
  16. def _settzkeyname():
  17.     global TZKEYNAME, TZKEYNAME
  18.     handle = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
  19.     
  20.     try:
  21.         _winreg.OpenKey(handle, TZKEYNAMENT).Close()
  22.         TZKEYNAME = TZKEYNAMENT
  23.     except WindowsError:
  24.         TZKEYNAME = TZKEYNAME9X
  25.  
  26.     handle.Close()
  27.  
  28. _settzkeyname()
  29.  
  30. class tzwinbase(datetime.tzinfo):
  31.     
  32.     def utcoffset(self, dt):
  33.         if self._isdst(dt):
  34.             return datetime.timedelta(minutes = self._dstoffset)
  35.         return datetime.timedelta(minutes = self._stdoffset)
  36.  
  37.     
  38.     def dst(self, dt):
  39.         if self._isdst(dt):
  40.             minutes = self._dstoffset - self._stdoffset
  41.             return datetime.timedelta(minutes = minutes)
  42.         return datetime.timedelta(0)
  43.  
  44.     
  45.     def tzname(self, dt):
  46.         if self._isdst(dt):
  47.             return self._dstname
  48.         return self._stdname
  49.  
  50.     
  51.     def list():
  52.         handle = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
  53.         tzkey = _winreg.OpenKey(handle, TZKEYNAME)
  54.         result = [ _winreg.EnumKey(tzkey, i) for i in range(_winreg.QueryInfoKey(tzkey)[0]) ]
  55.         tzkey.Close()
  56.         handle.Close()
  57.         return result
  58.  
  59.     list = staticmethod(list)
  60.     
  61.     def display(self):
  62.         return self._display
  63.  
  64.     
  65.     def _isdst(self, dt):
  66.         dston = picknthweekday(dt.year, self._dstmonth, self._dstdayofweek, self._dsthour, self._dstminute, self._dstweeknumber)
  67.         dstoff = picknthweekday(dt.year, self._stdmonth, self._stddayofweek, self._stdhour, self._stdminute, self._stdweeknumber)
  68.         if dston < dstoff:
  69.             if dt.replace(tzinfo = None) <= dt.replace(tzinfo = None):
  70.                 return dt.replace(tzinfo = None) < dstoff
  71.             dt.replace(tzinfo = None) <= dt.replace(tzinfo = None)
  72.             return dt.replace(tzinfo = None)
  73.         return not dston < dstoff if dt.replace(tzinfo = None) <= dt.replace(tzinfo = None) else dt.replace(tzinfo = None) < dston
  74.  
  75.  
  76.  
  77. class tzwin(tzwinbase):
  78.     
  79.     def __init__(self, name):
  80.         self._name = name
  81.         handle = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
  82.         tzkey = _winreg.OpenKey(handle, '%s\\%s' % (TZKEYNAME, name))
  83.         keydict = valuestodict(tzkey)
  84.         tzkey.Close()
  85.         handle.Close()
  86.         self._stdname = keydict['Std'].encode('iso-8859-1')
  87.         self._dstname = keydict['Dlt'].encode('iso-8859-1')
  88.         self._display = keydict['Display']
  89.         tup = struct.unpack('=3l16h', keydict['TZI'])
  90.         self._stdoffset = -tup[0] - tup[1]
  91.         self._dstoffset = self._stdoffset - tup[2]
  92.         (self._stdmonth, self._stddayofweek, self._stdweeknumber, self._stdhour, self._stdminute) = tup[4:9]
  93.         (self._dstmonth, self._dstdayofweek, self._dstweeknumber, self._dsthour, self._dstminute) = tup[12:17]
  94.  
  95.     
  96.     def __repr__(self):
  97.         return 'tzwin(%s)' % repr(self._name)
  98.  
  99.     
  100.     def __reduce__(self):
  101.         return (self.__class__, (self._name,))
  102.  
  103.  
  104.  
  105. class tzwinlocal(tzwinbase):
  106.     
  107.     def __init__(self):
  108.         handle = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
  109.         tzlocalkey = _winreg.OpenKey(handle, TZLOCALKEYNAME)
  110.         keydict = valuestodict(tzlocalkey)
  111.         tzlocalkey.Close()
  112.         self._stdname = keydict['StandardName'].encode('iso-8859-1')
  113.         self._dstname = keydict['DaylightName'].encode('iso-8859-1')
  114.         
  115.         try:
  116.             tzkey = _winreg.OpenKey(handle, '%s\\%s' % (TZKEYNAME, self._stdname))
  117.             _keydict = valuestodict(tzkey)
  118.             self._display = _keydict['Display']
  119.             tzkey.Close()
  120.         except OSError:
  121.             self._display = None
  122.  
  123.         handle.Close()
  124.         self._stdoffset = -keydict['Bias'] - keydict['StandardBias']
  125.         self._dstoffset = self._stdoffset - keydict['DaylightBias']
  126.         tup = struct.unpack('=8h', keydict['StandardStart'])
  127.         (self._stdmonth, self._stddayofweek, self._stdweeknumber, self._stdhour, self._stdminute) = tup[1:6]
  128.         tup = struct.unpack('=8h', keydict['DaylightStart'])
  129.         (self._dstmonth, self._dstdayofweek, self._dstweeknumber, self._dsthour, self._dstminute) = tup[1:6]
  130.  
  131.     
  132.     def __reduce__(self):
  133.         return (self.__class__, ())
  134.  
  135.  
  136.  
  137. def picknthweekday(year, month, dayofweek, hour, minute, whichweek):
  138.     first = datetime.datetime(year, month, 1, hour, minute)
  139.     weekdayone = first.replace(day = (dayofweek - first.isoweekday()) % 7 + 1)
  140.     for n in xrange(whichweek):
  141.         dt = weekdayone + (whichweek - n) * ONEWEEK
  142.         if dt.month == month:
  143.             return dt
  144.     
  145.  
  146.  
  147. def valuestodict(key):
  148.     dict = { }
  149.     size = _winreg.QueryInfoKey(key)[1]
  150.     for i in range(size):
  151.         data = _winreg.EnumValue(key, i)
  152.         dict[data[0]] = data[1]
  153.     
  154.     return dict
  155.  
  156.