2 import _winreg as winreg
9 from babel.core import get_global
13 # When building the cldr data on windows this module gets imported.
14 # Because at that point there is no global.dat yet this call will
15 # fail. We want to catch it down in that case then and just assume
16 # the mapping was empty.
18 tz_names = get_global('windows_zone_mapping')
23 def valuestodict(key):
24 """Convert a registry key's values to a dictionary."""
26 size = winreg.QueryInfoKey(key)[1]
28 data = winreg.EnumValue(key, i)
29 dict[data[0]] = data[1]
33 def get_localzone_name():
34 # Windows is special. It has unique time zone names (in several
35 # meanings of the word) available, but unfortunately, they can be
36 # translated to the language of the operating system, so we need to
37 # do a backwards lookup, by going through all time zones and see which
39 handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
41 TZLOCALKEYNAME = r'SYSTEM\CurrentControlSet\Control\TimeZoneInformation'
42 localtz = winreg.OpenKey(handle, TZLOCALKEYNAME)
43 keyvalues = valuestodict(localtz)
45 if 'TimeZoneKeyName' in keyvalues:
46 # Windows 7 (and Vista?)
48 # For some reason this returns a string with loads of NUL bytes at
49 # least on some systems. I don't know if this is a bug somewhere, I
50 # just work around it.
51 tzkeyname = keyvalues['TimeZoneKeyName'].split('\x00', 1)[0]
55 # This is the localized name:
56 tzwin = keyvalues['StandardName']
58 # Open the list of timezones to look up the real name:
59 TZKEYNAME = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones'
60 tzkey = winreg.OpenKey(handle, TZKEYNAME)
62 # Now, match this value to Time Zone information
64 for i in range(winreg.QueryInfoKey(tzkey)[0]):
65 subkey = winreg.EnumKey(tzkey, i)
66 sub = winreg.OpenKey(tzkey, subkey)
67 data = valuestodict(sub)
69 if data['Std'] == tzwin:
77 raise LookupError('Can not find Windows timezone configuration')
79 timezone = tz_names.get(tzkeyname)
81 # Nope, that didn't work. Try adding 'Standard Time',
82 # it seems to work a lot of times:
83 timezone = tz_names.get(tzkeyname + ' Standard Time')
85 # Return what we have.
87 raise pytz.UnknownTimeZoneError('Can not find timezone ' + tzkeyname)
94 raise pytz.UnknownTimeZoneError(
95 'Runtime support not available')
96 return pytz.timezone(get_localzone_name())