Thread: [pywin32-checkins] pywin32/win32/Lib win32timezone.py,1.4,1.5
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2006-01-02 22:12:45
|
Update of /cvsroot/pywin32/pywin32/win32/Lib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20629 Modified Files: win32timezone.py Log Message: Patch from Jason to fix pywin32 bug 1392316. Added support for non-English versions of Windows. This required a restructure of the way time zones are retrieved. In particular, the "StandardName" key for the configured time zone is in the regional language, whereas all of the TimeZones keys are in English. I re-wrote GetIndexedTimeZones to make it more flexible so time zones can be retrieved by the "Standard" name. This internationalization support required the use of _winreg over win32api.Reg*, due to the fact that _winreg returns Unicode results. Note that 2 of 27 tests will fail on a non-English platform due to the regional translation of the time zone's display name. These failures do not affect the functionality of the module. Index: win32timezone.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/Lib/win32timezone.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** win32timezone.py 30 Dec 2005 08:10:18 -0000 1.4 --- win32timezone.py 2 Jan 2006 22:12:38 -0000 1.5 *************** *** 57,65 **** >>> est = win32timezone.TimeZoneInfo( 'Eastern Standard Time' ) >>> est.displayName ! '(GMT-05:00) Eastern Time (US & Canada)' >>> gmt = win32timezone.TimeZoneInfo( 'GMT Standard Time', True ) >>> gmt.displayName ! '(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London' TimeZoneInfo now supports being pickled and comparison --- 57,65 ---- >>> est = win32timezone.TimeZoneInfo( 'Eastern Standard Time' ) >>> est.displayName ! u'(GMT-05:00) Eastern Time (US & Canada)' >>> gmt = win32timezone.TimeZoneInfo( 'GMT Standard Time', True ) >>> gmt.displayName ! u'(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London' TimeZoneInfo now supports being pickled and comparison *************** *** 76,80 **** __date__ = '$Modtime: 04-04-14 10:52 $'[10:-2] ! import os, win32api, win32con, struct, datetime class TimeZoneInfo( datetime.tzinfo ): --- 76,80 ---- __date__ = '$Modtime: 04-04-14 10:52 $'[10:-2] ! import os, _winreg, struct, datetime class TimeZoneInfo( datetime.tzinfo ): *************** *** 92,105 **** def __init__( self, timeZoneName, fixedStandardTime=False ): self.timeZoneName = timeZoneName tzRegKeyPath = os.path.join( self.tzRegKey, timeZoneName ) try: ! key = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE, ! tzRegKeyPath, ! 0, ! win32con.KEY_READ ) except: raise ValueError, 'Timezone Name %s not found.' % timeZoneName ! self._LoadInfoFromKey( key ) ! self.fixedStandardTime = fixedStandardTime def __getinitargs__( self ): --- 92,113 ---- def __init__( self, timeZoneName, fixedStandardTime=False ): self.timeZoneName = timeZoneName + key = self._FindTimeZoneKey() + self._LoadInfoFromKey( key ) + self.fixedStandardTime = fixedStandardTime + + def _FindTimeZoneKey( self ): + """Find the registry key for the time zone name (self.timeZoneName).""" + # for multi-language compatability, match the time zone name in the + # "Std" key of the time zone key. + zoneNames = dict( GetIndexedTimeZoneNames( 'Std' ) ) + # Also match the time zone key name itself, to be compatible with + # English-based hard-coded time zones. + timeZoneName = zoneNames.get( self.timeZoneName, self.timeZoneName ) tzRegKeyPath = os.path.join( self.tzRegKey, timeZoneName ) try: ! key = _winreg.OpenKeyEx( _winreg.HKEY_LOCAL_MACHINE, tzRegKeyPath ) except: raise ValueError, 'Timezone Name %s not found.' % timeZoneName ! return key def __getinitargs__( self ): *************** *** 109,118 **** """Loads the information from an opened time zone registry key into relevant fields of this TZI object""" ! self.displayName = win32api.RegQueryValueEx( key, "Display" )[0] ! self.standardName = win32api.RegQueryValueEx( key, "Std" )[0] ! self.daylightName = win32api.RegQueryValueEx( key, "Dlt" )[0] # TZI contains a structure of time zone information and is similar to # TIME_ZONE_INFORMATION described in the Windows Platform SDK ! winTZI, type = win32api.RegQueryValueEx( key, "TZI" ) winTZI = struct.unpack( '3l8h8h', winTZI ) makeMinuteTimeDelta = lambda x: datetime.timedelta( minutes = x ) --- 117,126 ---- """Loads the information from an opened time zone registry key into relevant fields of this TZI object""" ! self.displayName = _winreg.QueryValueEx( key, "Display" )[0] ! self.standardName = _winreg.QueryValueEx( key, "Std" )[0] ! self.daylightName = _winreg.QueryValueEx( key, "Dlt" )[0] # TZI contains a structure of time zone information and is similar to # TIME_ZONE_INFORMATION described in the Windows Platform SDK ! winTZI, type = _winreg.QueryValueEx( key, "TZI" ) winTZI = struct.unpack( '3l8h8h', winTZI ) makeMinuteTimeDelta = lambda x: datetime.timedelta( minutes = x ) *************** *** 149,160 **** def utcoffset( self, dt ): "Calculates the utcoffset according to the datetime.tzinfo spec" ! if dt is None: ! return None return -( self.bias + self.dst( dt ) ) def dst( self, dt ): "Calculates the daylight savings offset according to the datetime.tzinfo spec" ! if dt is None: ! return None assert dt.tzinfo is self result = self.standardBiasOffset --- 157,166 ---- def utcoffset( self, dt ): "Calculates the utcoffset according to the datetime.tzinfo spec" ! if dt is None: return return -( self.bias + self.dst( dt ) ) def dst( self, dt ): "Calculates the daylight savings offset according to the datetime.tzinfo spec" ! if dt is None: return assert dt.tzinfo is self result = self.standardBiasOffset *************** *** 216,245 **** def _RegKeyEnumerator( key ): "Enumerates an open registry key as an iterable generator" index = 0 try: while 1: ! yield win32api.RegEnumKey( key, index ) index += 1 ! except win32api.error: pass def GetTimeZoneNames( ): "Returns the names of the time zones as defined in the registry" ! key = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE, ! TimeZoneInfo.tzRegKey, ! 0, ! win32con.KEY_READ ) return _RegKeyEnumerator( key ) ! def GetIndexedTimeZoneNames( ): ! """Returns the names of the time zones as defined in the registry, but includes ! the index by which they may be sorted longitudinally.""" for timeZoneName in GetTimeZoneNames(): tzRegKeyPath = os.path.join( TimeZoneInfo.tzRegKey, timeZoneName ) ! key = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE, ! tzRegKeyPath, ! 0, ! win32con.KEY_READ ) ! tzIndex, type = win32api.RegQueryValueEx( key, 'Index' ) yield ( tzIndex, timeZoneName ) --- 222,257 ---- def _RegKeyEnumerator( key ): + return _RegEnumerator( key, _winreg.EnumKey ) + + def _RegValueEnumerator( key ): + return _RegEnumerator( key, _winreg.EnumValue ) + + def _RegEnumerator( key, func ): "Enumerates an open registry key as an iterable generator" index = 0 try: while 1: ! yield func( key, index ) index += 1 ! except WindowsError: pass ! ! def _RegKeyDict( key ): ! values = _RegValueEnumerator( key ) ! values = tuple( values ) ! return dict( map( lambda (name,value,type): (name,value), values ) ) def GetTimeZoneNames( ): "Returns the names of the time zones as defined in the registry" ! key = _winreg.OpenKeyEx( _winreg.HKEY_LOCAL_MACHINE, TimeZoneInfo.tzRegKey ) return _RegKeyEnumerator( key ) ! def GetIndexedTimeZoneNames( index_key = 'Index' ): ! """Returns the names of the time zones as defined in the registry, but ! includes an index by which they may be sorted. Default index is "Index" ! by which they may be sorted longitudinally.""" for timeZoneName in GetTimeZoneNames(): tzRegKeyPath = os.path.join( TimeZoneInfo.tzRegKey, timeZoneName ) ! key = _winreg.OpenKeyEx( _winreg.HKEY_LOCAL_MACHINE, tzRegKeyPath ) ! tzIndex, type = _winreg.QueryValueEx( key, index_key ) yield ( tzIndex, timeZoneName ) *************** *** 252,256 **** def GetLocalTimeZone( ): ! """Returns the local time zone as defined by the operating system in the registry >>> localTZ = GetLocalTimeZone() >>> nowLoc = datetime.datetime.now( localTZ ) --- 264,271 ---- def GetLocalTimeZone( ): ! """Returns the local time zone as defined by the operating system in the ! registry. ! Note that this will only work if the TimeZone in the registry has not been ! customized. It should have been selected from the Windows interface. >>> localTZ = GetLocalTimeZone() >>> nowLoc = datetime.datetime.now( localTZ ) *************** *** 268,275 **** """ tzRegKey = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation' ! key = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE, ! tzRegKey, ! 0, ! win32con.KEY_READ ) ! tzName, type = win32api.RegQueryValueEx( key, 'StandardName' ) ! return TimeZoneInfo( tzName ) --- 283,293 ---- """ tzRegKey = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation' ! key = _winreg.OpenKeyEx( _winreg.HKEY_LOCAL_MACHINE, tzRegKey ) ! local = _RegKeyDict( key ) ! # if the user has not checked "Automatically adjust clock for daylight ! # saving changes" in the Date and Time Properties control, the standard ! # and daylight values will be the same. If this is the case, create a ! # timezone object fixed to standard time. ! fixStandardTime = local['StandardName'] == local['DaylightName'] and \ ! local['StandardBias'] == local['DaylightBias'] ! return TimeZoneInfo( local['StandardName'], fixStandardTime ) |