2 ********************************************************************************
3 * Copyright (C) 2005-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 ********************************************************************************
12 #include "unicode/utypes.h"
14 #if U_PLATFORM_HAS_WIN32_API
21 #include "unicode/ustring.h"
22 #include "unicode/ures.h"
24 # define WIN32_LEAN_AND_MEAN
32 #define MAX_LENGTH_ID 32
34 /* The layout of the Tzi value in the registry */
40 SYSTEMTIME standardDate
;
41 SYSTEMTIME daylightDate
;
45 * Various registry keys and key fragments.
47 static const char CURRENT_ZONE_REGKEY
[] = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\";
48 static const char STANDARD_NAME_REGKEY
[] = "StandardName";
49 static const char STANDARD_TIME_REGKEY
[] = " Standard Time";
50 static const char TZI_REGKEY
[] = "TZI";
51 static const char STD_REGKEY
[] = "Std";
54 * HKLM subkeys used to probe for the flavor of Windows. Note that we
55 * specifically check for the "GMT" zone subkey; this is present on
56 * NT, but on XP has become "GMT Standard Time". We need to
57 * discriminate between these cases.
59 static const char* const WIN_TYPE_PROBE_REGKEY
[] = {
61 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones",
64 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\GMT"
66 /* otherwise: WIN_2K_XP_TYPE */
70 * The time zone root subkeys (under HKLM) for different flavors of
73 static const char* const TZ_REGKEY
[] = {
75 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\",
77 /* WIN_NT_TYPE | WIN_2K_XP_TYPE */
78 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\"
82 * Flavor of Windows, from our perspective. Not a real OS version,
83 * but rather the flavor of the layout of the time zone information in
92 static int32_t gWinType
= 0;
94 static int32_t detectWindowsType()
100 /* Detect the version of windows by trying to open a sequence of
101 probe keys. We don't use the OS version API because what we
102 really want to know is how the registry is laid out.
103 Specifically, is it 9x/Me or not, and is it "GMT" or "GMT
105 for (winType
= 0; winType
< 2; winType
++) {
106 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
107 WIN_TYPE_PROBE_REGKEY
[winType
],
113 if (result
== ERROR_SUCCESS
) {
118 return winType
+1; /* +1 to bring it inline with the enum */
121 static LONG
openTZRegKey(HKEY
*hkey
, const char *winid
)
123 /* subKeyName needs to be long enough for the longest TZ_REGKEY, plus the longest Windows
124 * tzid (current or obsolete), plus an appended STANDARD_TIME_REGKEY, plus a 0 terminator.
125 * Currently this is 111, but the code had 110. Make it 128 for some wiggle room. */
126 char subKeyName
[128];
130 /* This isn't thread safe, but it's good enough because the result should be constant per system. */
132 gWinType
= detectWindowsType();
135 uprv_strcpy(subKeyName
, TZ_REGKEY
[(gWinType
!= WIN_9X_ME_TYPE
)]);
136 name
= &subKeyName
[strlen(subKeyName
)];
137 uprv_strcat(subKeyName
, winid
);
139 if (gWinType
== WIN_9X_ME_TYPE
) {
140 /* Remove " Standard Time" */
141 char *pStd
= uprv_strstr(subKeyName
, STANDARD_TIME_REGKEY
);
147 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
155 static LONG
getTZI(const char *winid
, TZI
*tzi
)
157 DWORD cbData
= sizeof(TZI
);
161 result
= openTZRegKey(&hkey
, winid
);
163 if (result
== ERROR_SUCCESS
) {
164 result
= RegQueryValueExA(hkey
,
178 static LONG
getSTDName(const char *winid
, char *regStdName
, int32_t length
) {
179 DWORD cbData
= length
;
183 result
= openTZRegKey(&hkey
, winid
);
185 if (result
== ERROR_SUCCESS
) {
186 result
= RegQueryValueExA(hkey
,
201 This code attempts to detect the Windows time zone, as set in the
202 Windows Date and Time control panel. It attempts to work on
203 multiple flavors of Windows (9x, Me, NT, 2000, XP) and on localized
204 installs. It works by directly interrogating the registry and
205 comparing the data there with the data returned by the
206 GetTimeZoneInformation API, along with some other strategies. The
207 registry contains time zone data under one of two keys (depending on
208 the flavor of Windows):
210 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones\
211 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
213 Under this key are several subkeys, one for each time zone. These
214 subkeys are named "Pacific" on Win9x/Me and "Pacific Standard Time"
215 on WinNT/2k/XP. There are some other wrinkles; see the code for
216 details. The subkey name is NOT LOCALIZED, allowing us to support
219 Under the subkey are data values. We care about:
221 Std Standard time display name, localized
222 TZI Binary block of data
224 The TZI data is of particular interest. It contains the offset, two
225 more offsets for standard and daylight time, and the start and end
226 rules. This is the same data returned by the GetTimeZoneInformation
227 API. The API may modify the data on the way out, so we have to be
228 careful, but essentially we do a binary comparison against the TZI
229 blocks of various registry keys. When we find a match, we know what
230 time zone Windows is set to. Since the registry key is not
231 localized, we can then translate the key through a simple table
232 lookup into the corresponding ICU time zone.
234 This strategy doesn't always work because there are zones which
235 share an offset and rules, so more than one TZI block will match.
236 For example, both Tokyo and Seoul are at GMT+9 with no DST rules;
237 their TZI blocks are identical. For these cases, we fall back to a
238 name lookup. We attempt to match the display name as stored in the
239 registry for the current zone to the display name stored in the
240 registry for various Windows zones. By comparing the registry data
241 directly we avoid conversion complications.
245 Based on original code by Carl Brown <cbrown@xnetinc.com>
249 * Main Windows time zone detection function. Returns the Windows
250 * time zone, translated to an ICU time zone, or NULL upon failure.
252 U_CFUNC
const char* U_EXPORT2
253 uprv_detectWindowsTimeZone() {
254 UErrorCode status
= U_ZERO_ERROR
;
255 UResourceBundle
* bundle
= NULL
;
257 UChar apiStd
[MAX_LENGTH_ID
];
258 char apiStdName
[MAX_LENGTH_ID
];
259 char regStdName
[MAX_LENGTH_ID
];
260 char tmpid
[MAX_LENGTH_ID
];
261 int32_t apiStdLength
= 0;
267 TIME_ZONE_INFORMATION apiTZI
;
269 /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it
270 to TZI. We could also interrogate the registry directly; we do
271 this below if needed. */
272 uprv_memset(&apiTZI
, 0, sizeof(apiTZI
));
273 uprv_memset(&tziKey
, 0, sizeof(tziKey
));
274 uprv_memset(&tziReg
, 0, sizeof(tziReg
));
275 GetTimeZoneInformation(&apiTZI
);
276 tziKey
.bias
= apiTZI
.Bias
;
277 uprv_memcpy((char *)&tziKey
.standardDate
, (char*)&apiTZI
.StandardDate
,
278 sizeof(apiTZI
.StandardDate
));
279 uprv_memcpy((char *)&tziKey
.daylightDate
, (char*)&apiTZI
.DaylightDate
,
280 sizeof(apiTZI
.DaylightDate
));
282 /* Convert the wchar_t* standard name to char* */
283 uprv_memset(apiStdName
, 0, sizeof(apiStdName
));
284 u_strFromWCS(apiStd
, MAX_LENGTH_ID
, &apiStdLength
, apiTZI
.StandardName
, -1, &status
);
285 u_austrncpy(apiStdName
, apiStd
, apiStdLength
);
289 bundle
= ures_openDirect(NULL
, "windowsZones", &status
);
290 ures_getByKey(bundle
, "mapTimezones", bundle
, &status
);
292 /* Note: We get the winid not from static tables but from resource bundle. */
293 while (U_SUCCESS(status
) && ures_hasNext(bundle
)) {
294 UBool idFound
= FALSE
;
296 UResourceBundle
* winTZ
= ures_getNextResource(bundle
, NULL
, &status
);
297 if (U_FAILURE(status
)) {
300 winid
= ures_getKey(winTZ
);
301 result
= getTZI(winid
, &tziReg
);
303 if (result
== ERROR_SUCCESS
) {
304 /* Windows alters the DaylightBias in some situations.
305 Using the bias and the rules suffices, so overwrite
306 these unreliable fields. */
307 tziKey
.standardBias
= tziReg
.standardBias
;
308 tziKey
.daylightBias
= tziReg
.daylightBias
;
310 if (uprv_memcmp((char *)&tziKey
, (char*)&tziReg
, sizeof(tziKey
)) == 0) {
311 const UChar
* icuTZ
= ures_getStringByKey(winTZ
, "001", &len
, &status
);
312 if (U_SUCCESS(status
)) {
313 /* Get the standard name from the registry key to compare with
314 the one from Windows API call. */
315 uprv_memset(regStdName
, 0, sizeof(regStdName
));
316 result
= getSTDName(winid
, regStdName
, sizeof(regStdName
));
317 if (result
== ERROR_SUCCESS
) {
318 if (uprv_strcmp(apiStdName
, regStdName
) == 0) {
323 /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows.
324 * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching
325 * the current time zone information)
327 if (idFound
|| tmpid
[0] == 0) {
328 uprv_memset(tmpid
, 0, sizeof(tmpid
));
329 u_austrncpy(tmpid
, icuTZ
, len
);
341 * Copy the timezone ID to icuid to be returned.
344 len
= uprv_strlen(tmpid
);
345 icuid
= (char*)uprv_calloc(len
+ 1, sizeof(char));
347 uprv_strcpy(icuid
, tmpid
);
356 #endif /* U_PLATFORM_HAS_WIN32_API */