]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/wintz.c
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / common / wintz.c
1 /*
2 ********************************************************************************
3 * Copyright (C) 2005-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File WINTZ.CPP
8 *
9 ********************************************************************************
10 */
11
12 #include "unicode/utypes.h"
13
14 #if U_PLATFORM_HAS_WIN32_API
15
16 #include "wintz.h"
17 #include "cmemory.h"
18 #include "cstring.h"
19
20 #include "unicode/ustring.h"
21 #include "unicode/ures.h"
22
23 # define WIN32_LEAN_AND_MEAN
24 # define VC_EXTRALEAN
25 # define NOUSER
26 # define NOSERVICE
27 # define NOIME
28 # define NOMCX
29 #include <windows.h>
30
31 #define MAX_LENGTH_ID 40
32
33 /* The layout of the Tzi value in the registry */
34 typedef struct
35 {
36 int32_t bias;
37 int32_t standardBias;
38 int32_t daylightBias;
39 SYSTEMTIME standardDate;
40 SYSTEMTIME daylightDate;
41 } TZI;
42
43 /**
44 * Various registry keys and key fragments.
45 */
46 static const char CURRENT_ZONE_REGKEY[] = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\";
47 static const char STANDARD_NAME_REGKEY[] = "StandardName";
48 static const char STANDARD_TIME_REGKEY[] = " Standard Time";
49 static const char TZI_REGKEY[] = "TZI";
50 static const char STD_REGKEY[] = "Std";
51
52 /**
53 * HKLM subkeys used to probe for the flavor of Windows. Note that we
54 * specifically check for the "GMT" zone subkey; this is present on
55 * NT, but on XP has become "GMT Standard Time". We need to
56 * discriminate between these cases.
57 */
58 static const char* const WIN_TYPE_PROBE_REGKEY[] = {
59 /* WIN_9X_ME_TYPE */
60 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones",
61
62 /* WIN_NT_TYPE */
63 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\GMT"
64
65 /* otherwise: WIN_2K_XP_TYPE */
66 };
67
68 /**
69 * The time zone root subkeys (under HKLM) for different flavors of
70 * Windows.
71 */
72 static const char* const TZ_REGKEY[] = {
73 /* WIN_9X_ME_TYPE */
74 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\",
75
76 /* WIN_NT_TYPE | WIN_2K_XP_TYPE */
77 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\"
78 };
79
80 /**
81 * Flavor of Windows, from our perspective. Not a real OS version,
82 * but rather the flavor of the layout of the time zone information in
83 * the registry.
84 */
85 enum {
86 WIN_9X_ME_TYPE = 1,
87 WIN_NT_TYPE = 2,
88 WIN_2K_XP_TYPE = 3
89 };
90
91 static int32_t gWinType = 0;
92
93 static int32_t detectWindowsType()
94 {
95 int32_t winType;
96 LONG result;
97 HKEY hkey;
98
99 /* Detect the version of windows by trying to open a sequence of
100 probe keys. We don't use the OS version API because what we
101 really want to know is how the registry is laid out.
102 Specifically, is it 9x/Me or not, and is it "GMT" or "GMT
103 Standard Time". */
104 for (winType = 0; winType < 2; winType++) {
105 result = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
106 WIN_TYPE_PROBE_REGKEY[winType],
107 0,
108 KEY_QUERY_VALUE,
109 &hkey);
110 RegCloseKey(hkey);
111
112 if (result == ERROR_SUCCESS) {
113 break;
114 }
115 }
116
117 return winType+1; /* +1 to bring it inline with the enum */
118 }
119
120 static LONG openTZRegKey(HKEY *hkey, const char *winid)
121 {
122 /* subKeyName needs to be long enough for the longest TZ_REGKEY, plus the longest Windows
123 * tzid (current or obsolete), plus an appended STANDARD_TIME_REGKEY, plus a 0 terminator.
124 * At its max point this was 111, but the code had 110. Make it 128 for some wiggle room. */
125 char subKeyName[128];
126 char *name;
127 LONG result;
128
129 /* This isn't thread safe, but it's good enough because the result should be constant per system. */
130 if (gWinType <= 0) {
131 gWinType = detectWindowsType();
132 }
133
134 uprv_strcpy(subKeyName, TZ_REGKEY[(gWinType != WIN_9X_ME_TYPE)]);
135 name = &subKeyName[strlen(subKeyName)];
136 uprv_strcat(subKeyName, winid);
137
138 if (gWinType == WIN_9X_ME_TYPE) {
139 /* Remove " Standard Time" */
140 char *pStd = uprv_strstr(subKeyName, STANDARD_TIME_REGKEY);
141 if (pStd) {
142 *pStd = 0;
143 }
144 }
145
146 result = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
147 subKeyName,
148 0,
149 KEY_QUERY_VALUE,
150 hkey);
151 return result;
152 }
153
154 static LONG getTZI(const char *winid, TZI *tzi)
155 {
156 DWORD cbData = sizeof(TZI);
157 LONG result;
158 HKEY hkey;
159
160 result = openTZRegKey(&hkey, winid);
161
162 if (result == ERROR_SUCCESS) {
163 result = RegQueryValueExA(hkey,
164 TZI_REGKEY,
165 NULL,
166 NULL,
167 (LPBYTE)tzi,
168 &cbData);
169
170 }
171
172 RegCloseKey(hkey);
173
174 return result;
175 }
176
177 static LONG getSTDName(const char *winid, char *regStdName, int32_t length) {
178 DWORD cbData = length;
179 LONG result;
180 HKEY hkey;
181
182 result = openTZRegKey(&hkey, winid);
183
184 if (result == ERROR_SUCCESS) {
185 result = RegQueryValueExA(hkey,
186 STD_REGKEY,
187 NULL,
188 NULL,
189 (LPBYTE)regStdName,
190 &cbData);
191
192 }
193
194 RegCloseKey(hkey);
195
196 return result;
197 }
198
199 /*
200 This code attempts to detect the Windows time zone, as set in the
201 Windows Date and Time control panel. It attempts to work on
202 multiple flavors of Windows (9x, Me, NT, 2000, XP) and on localized
203 installs. It works by directly interrogating the registry and
204 comparing the data there with the data returned by the
205 GetTimeZoneInformation API, along with some other strategies. The
206 registry contains time zone data under one of two keys (depending on
207 the flavor of Windows):
208
209 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones\
210 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
211
212 Under this key are several subkeys, one for each time zone. These
213 subkeys are named "Pacific" on Win9x/Me and "Pacific Standard Time"
214 on WinNT/2k/XP. There are some other wrinkles; see the code for
215 details. The subkey name is NOT LOCALIZED, allowing us to support
216 localized installs.
217
218 Under the subkey are data values. We care about:
219
220 Std Standard time display name, localized
221 TZI Binary block of data
222
223 The TZI data is of particular interest. It contains the offset, two
224 more offsets for standard and daylight time, and the start and end
225 rules. This is the same data returned by the GetTimeZoneInformation
226 API. The API may modify the data on the way out, so we have to be
227 careful, but essentially we do a binary comparison against the TZI
228 blocks of various registry keys. When we find a match, we know what
229 time zone Windows is set to. Since the registry key is not
230 localized, we can then translate the key through a simple table
231 lookup into the corresponding ICU time zone.
232
233 This strategy doesn't always work because there are zones which
234 share an offset and rules, so more than one TZI block will match.
235 For example, both Tokyo and Seoul are at GMT+9 with no DST rules;
236 their TZI blocks are identical. For these cases, we fall back to a
237 name lookup. We attempt to match the display name as stored in the
238 registry for the current zone to the display name stored in the
239 registry for various Windows zones. By comparing the registry data
240 directly we avoid conversion complications.
241
242 Author: Alan Liu
243 Since: ICU 2.6
244 Based on original code by Carl Brown <cbrown@xnetinc.com>
245 */
246
247 /**
248 * Main Windows time zone detection function. Returns the Windows
249 * time zone, translated to an ICU time zone, or NULL upon failure.
250 */
251 U_CFUNC const char* U_EXPORT2
252 uprv_detectWindowsTimeZone() {
253 UErrorCode status = U_ZERO_ERROR;
254 UResourceBundle* bundle = NULL;
255 char* icuid = NULL;
256 UChar apiStd[MAX_LENGTH_ID];
257 char apiStdName[MAX_LENGTH_ID];
258 char regStdName[MAX_LENGTH_ID];
259 char tmpid[MAX_LENGTH_ID];
260 int32_t apiStdLength = 0;
261 int32_t len;
262 int id;
263 int errorCode;
264 char ISOcode[3]; /* 2 letter iso code */
265
266 LONG result;
267 TZI tziKey;
268 TZI tziReg;
269 TIME_ZONE_INFORMATION apiTZI;
270
271 /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it
272 to TZI. We could also interrogate the registry directly; we do
273 this below if needed. */
274 uprv_memset(&apiTZI, 0, sizeof(apiTZI));
275 uprv_memset(&tziKey, 0, sizeof(tziKey));
276 uprv_memset(&tziReg, 0, sizeof(tziReg));
277 GetTimeZoneInformation(&apiTZI);
278 tziKey.bias = apiTZI.Bias;
279 uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate,
280 sizeof(apiTZI.StandardDate));
281 uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate,
282 sizeof(apiTZI.DaylightDate));
283
284 /* Convert the wchar_t* standard name to char* */
285 uprv_memset(apiStdName, 0, sizeof(apiStdName));
286 u_strFromWCS(apiStd, MAX_LENGTH_ID, &apiStdLength, apiTZI.StandardName, -1, &status);
287 u_austrncpy(apiStdName, apiStd, apiStdLength);
288
289 tmpid[0] = 0;
290
291 id = GetUserGeoID(GEOCLASS_NATION);
292 errorCode = GetGeoInfo(id,GEO_ISO2,ISOcode,3,0);
293
294 bundle = ures_openDirect(NULL, "windowsZones", &status);
295 ures_getByKey(bundle, "mapTimezones", bundle, &status);
296
297 /* Note: We get the winid not from static tables but from resource bundle. */
298 while (U_SUCCESS(status) && ures_hasNext(bundle)) {
299 UBool idFound = FALSE;
300 const char* winid;
301 UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status);
302 if (U_FAILURE(status)) {
303 break;
304 }
305 winid = ures_getKey(winTZ);
306 result = getTZI(winid, &tziReg);
307
308 if (result == ERROR_SUCCESS) {
309 /* Windows alters the DaylightBias in some situations.
310 Using the bias and the rules suffices, so overwrite
311 these unreliable fields. */
312 tziKey.standardBias = tziReg.standardBias;
313 tziKey.daylightBias = tziReg.daylightBias;
314
315 if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) {
316 const UChar* icuTZ = NULL;
317 if (errorCode != 0) {
318 icuTZ = ures_getStringByKey(winTZ, ISOcode, &len, &status);
319 }
320 if (errorCode==0 || icuTZ==NULL) {
321 /* fallback to default "001" and reset status */
322 status = U_ZERO_ERROR;
323 icuTZ = ures_getStringByKey(winTZ, "001", &len, &status);
324 }
325
326 if (U_SUCCESS(status)) {
327 /* Get the standard name from the registry key to compare with
328 the one from Windows API call. */
329 uprv_memset(regStdName, 0, sizeof(regStdName));
330 result = getSTDName(winid, regStdName, sizeof(regStdName));
331 if (result == ERROR_SUCCESS) {
332 if (uprv_strcmp(apiStdName, regStdName) == 0) {
333 idFound = TRUE;
334 }
335 }
336
337 /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows.
338 * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching
339 * the current time zone information)
340 */
341 if (idFound || tmpid[0] == 0) {
342 /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */
343 int index=0;
344 while (! (*icuTZ == '\0' || *icuTZ ==' ')) {
345 tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */
346 }
347 tmpid[index]='\0';
348 }
349 }
350 }
351 }
352 ures_close(winTZ);
353 if (idFound) {
354 break;
355 }
356 }
357
358 /*
359 * Copy the timezone ID to icuid to be returned.
360 */
361 if (tmpid[0] != 0) {
362 len = uprv_strlen(tmpid);
363 icuid = (char*)uprv_calloc(len + 1, sizeof(char));
364 if (icuid != NULL) {
365 uprv_strcpy(icuid, tmpid);
366 }
367 }
368
369 ures_close(bundle);
370
371 return icuid;
372 }
373
374 #endif /* U_PLATFORM_HAS_WIN32_API */