<s> import _winreg <EOL> import struct <EOL> import datetime <EOL> handle = _winreg . ConnectRegistry ( None , _winreg . HKEY_LOCAL_MACHINE ) <EOL> tzparent = _winreg . OpenKey ( handle , <EOL> "<STR_LIT>" ) <EOL> parentsize = _winreg . QueryInfoKey ( tzparent ) [ <NUM_LIT:0> ] <EOL> localkey = _winreg . OpenKey ( handle , <EOL> "<STR_LIT>" ) <EOL> WEEKS = datetime . timedelta ( <NUM_LIT:7> ) <EOL> def list_timezones ( ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> for i in xrange ( parentsize ) : <EOL> l . append ( _winreg . EnumKey ( tzparent , i ) ) <EOL> return l <EOL> class win32tz ( datetime . tzinfo ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . data = win32tz_data ( name ) <EOL> def utcoffset ( self , dt ) : <EOL> if self . _isdst ( dt ) : <EOL> return datetime . timedelta ( minutes = self . data . dstoffset ) <EOL> else : <EOL> return datetime . timedelta ( minutes = self . data . stdoffset ) <EOL> def dst ( self , dt ) : <EOL> if self . _isdst ( dt ) : <EOL> minutes = self . data . dstoffset - self . data . stdoffset <EOL> return datetime . timedelta ( minutes = minutes ) <EOL> else : <EOL> return datetime . timedelta ( <NUM_LIT:0> ) <EOL> def tzname ( self , dt ) : <EOL> if self . _isdst ( dt ) : return self . data . dstname <EOL> else : return self . data . stdname <EOL> def _isdst ( self , dt ) : <EOL> dat = self . data <EOL> dston = pickNthWeekday ( dt . year , dat . dstmonth , dat . dstdayofweek , <EOL> dat . dsthour , dat . dstminute , dat . dstweeknumber ) <EOL> dstoff = pickNthWeekday ( dt . year , dat . stdmonth , dat . stddayofweek , <EOL> dat . stdhour , dat . stdminute , dat . stdweeknumber ) <EOL> if dston < dstoff : <EOL> if dston <= dt . replace ( tzinfo = None ) < dstoff : return True <EOL> else : return False <EOL> else : <EOL> if dstoff <= dt . replace ( tzinfo = None ) < dston : return False <EOL> else : return True <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . data . display <EOL> def pickNthWeekday ( year , month , dayofweek , hour , minute , whichweek ) : <EOL> """<STR_LIT>""" <EOL> first = datetime . datetime ( year = year , month = month , hour = hour , minute = minute , <EOL> day = <NUM_LIT:1> ) <EOL> weekdayone = first . replace ( day = ( ( dayofweek - first . isoweekday ( ) ) % <NUM_LIT:7> + <NUM_LIT:1> ) ) <EOL> for n in xrange ( whichweek - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> dt = weekdayone + n * WEEKS <EOL> if dt . month == month : return dt <EOL> class win32tz_data ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> keydict = valuesToDict ( _winreg . OpenKey ( tzparent , path ) ) <EOL> self . display = keydict [ '<STR_LIT>' ] <EOL> self . dstname = keydict [ '<STR_LIT>' ] <EOL> self . stdname = keydict [ '<STR_LIT>' ] <EOL> tup = struct . unpack ( '<STR_LIT>' , keydict [ '<STR_LIT>' ] ) <EOL> self . stdoffset = - tup [ <NUM_LIT:0> ] - tup [ <NUM_LIT:1> ] <EOL> self . dstoffset = self . stdoffset - tup [ <NUM_LIT:2> ] <EOL> offset = <NUM_LIT:3> <EOL> self . stdmonth = tup [ <NUM_LIT:1> + offset ] <EOL> self . stddayofweek = tup [ <NUM_LIT:2> + offset ] <EOL> self . stdweeknumber = tup [ <NUM_LIT:3> + offset ] <EOL> self . stdhour = tup [ <NUM_LIT:4> + offset ] <EOL> self . stdminute = tup [ <NUM_LIT:5> + offset ] <EOL> offset = <NUM_LIT:11> <EOL> self . dstmonth = tup [ <NUM_LIT:1> + offset ] <EOL> self . dstdayofweek = tup [ <NUM_LIT:2> + offset ] <EOL> self . dstweeknumber = tup [ <NUM_LIT:3> + offset ] <EOL> self . dsthour = tup [ <NUM_LIT:4> + offset ] <EOL> self . dstminute = tup [ <NUM_LIT:5> + offset ] <EOL> else : <EOL> keydict = valuesToDict ( localkey ) <EOL> self . stdname = keydict [ '<STR_LIT>' ] <EOL> self . dstname = keydict [ '<STR_LIT>' ] <EOL> sourcekey = _winreg . OpenKey ( tzparent , self . stdname ) <EOL> self . display = valuesToDict ( sourcekey ) [ '<STR_LIT>' ] <EOL> self . stdoffset = - keydict [ '<STR_LIT>' ] - keydict [ '<STR_LIT>' ] <EOL> self . dstoffset = self . stdoffset - keydict [ '<STR_LIT>' ] <EOL> tup = struct . unpack ( '<STR_LIT>' , keydict [ '<STR_LIT>' ] ) <EOL> offset = <NUM_LIT:0> <EOL> self . stdmonth = tup [ <NUM_LIT:1> + offset ] <EOL> self . stddayofweek = tup [ <NUM_LIT:2> + offset ] <EOL> self . stdweeknumber = tup [ <NUM_LIT:3> + offset ] <EOL> self . stdhour = tup [ <NUM_LIT:4> + offset ] <EOL> self . stdminute = tup [ <NUM_LIT:5> + offset ] <EOL> tup = struct . unpack ( '<STR_LIT>' , keydict [ '<STR_LIT>' ] ) <EOL> self . dstmonth = tup [ <NUM_LIT:1> + offset ] <EOL> self . dstdayofweek = tup [ <NUM_LIT:2> + offset ] <EOL> self . dstweeknumber = tup [ <NUM_LIT:3> + offset ] <EOL> self . dsthour = tup [ <NUM_LIT:4> + offset ] <EOL> self . dstminute = tup [ <NUM_LIT:5> + offset ] <EOL> def valuesToDict ( key ) : <EOL> """<STR_LIT>""" <EOL> dict = { } <EOL> size = _winreg . QueryInfoKey ( key ) [ <NUM_LIT:1> ] <EOL> for i in xrange ( size ) : <EOL> dict [ _winreg . EnumValue ( key , i ) [ <NUM_LIT:0> ] ] = _winreg . EnumValue ( key , i ) [ <NUM_LIT:1> ] <EOL> return dict <EOL> def _test ( ) : <EOL> import win32tz , doctest <EOL> doctest . testmod ( win32tz , verbose = <NUM_LIT:0> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
