2 ********************************************************************************
3 * Copyright (C) 2005-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 ********************************************************************************
12 #include "unicode/utypes.h"
14 #if U_PLATFORM_HAS_WIN32_API
20 #include "unicode/ures.h"
22 # define WIN32_LEAN_AND_MEAN
30 #define MAX_LENGTH_ID 40
32 /* The layout of the Tzi value in the registry */
38 SYSTEMTIME standardDate
;
39 SYSTEMTIME daylightDate
;
43 * Various registry keys and key fragments.
45 static const char CURRENT_ZONE_REGKEY
[] = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\";
46 static const char STANDARD_NAME_REGKEY
[] = "StandardName";
47 static const char STANDARD_TIME_REGKEY
[] = " Standard Time";
48 static const char TZI_REGKEY
[] = "TZI";
49 static const char STD_REGKEY
[] = "Std";
52 * HKLM subkeys used to probe for the flavor of Windows. Note that we
53 * specifically check for the "GMT" zone subkey; this is present on
54 * NT, but on XP has become "GMT Standard Time". We need to
55 * discriminate between these cases.
57 static const char* const WIN_TYPE_PROBE_REGKEY
[] = {
59 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones",
62 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\GMT"
64 /* otherwise: WIN_2K_XP_TYPE */
68 * The time zone root subkeys (under HKLM) for different flavors of
71 static const char* const TZ_REGKEY
[] = {
73 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\",
75 /* WIN_NT_TYPE | WIN_2K_XP_TYPE */
76 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\"
80 * Flavor of Windows, from our perspective. Not a real OS version,
81 * but rather the flavor of the layout of the time zone information in
90 static int32_t gWinType
= 0;
92 static int32_t detectWindowsType()
98 /* Detect the version of windows by trying to open a sequence of
99 probe keys. We don't use the OS version API because what we
100 really want to know is how the registry is laid out.
101 Specifically, is it 9x/Me or not, and is it "GMT" or "GMT
103 for (winType
= 0; winType
< 2; winType
++) {
104 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
105 WIN_TYPE_PROBE_REGKEY
[winType
],
111 if (result
== ERROR_SUCCESS
) {
116 return winType
+1; /* +1 to bring it inline with the enum */
119 static LONG
openTZRegKey(HKEY
*hkey
, const char *winid
)
121 /* subKeyName needs to be long enough for the longest TZ_REGKEY, plus the longest Windows
122 * tzid (current or obsolete), plus an appended STANDARD_TIME_REGKEY, plus a 0 terminator.
123 * At its max point this was 111, but the code had 110. Make it 128 for some wiggle room. */
124 char subKeyName
[128];
128 /* This isn't thread safe, but it's good enough because the result should be constant per system. */
130 gWinType
= detectWindowsType();
133 uprv_strcpy(subKeyName
, TZ_REGKEY
[(gWinType
!= WIN_9X_ME_TYPE
)]);
134 name
= &subKeyName
[strlen(subKeyName
)];
135 uprv_strcat(subKeyName
, winid
);
137 if (gWinType
== WIN_9X_ME_TYPE
) {
138 /* Remove " Standard Time" */
139 char *pStd
= uprv_strstr(subKeyName
, STANDARD_TIME_REGKEY
);
145 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
153 static LONG
getTZI(const char *winid
, TZI
*tzi
)
155 DWORD cbData
= sizeof(TZI
);
159 result
= openTZRegKey(&hkey
, winid
);
161 if (result
== ERROR_SUCCESS
) {
162 result
= RegQueryValueExA(hkey
,
176 static LONG
getSTDName(const char *winid
, char *regStdName
, int32_t length
) {
177 DWORD cbData
= length
;
181 result
= openTZRegKey(&hkey
, winid
);
183 if (result
== ERROR_SUCCESS
) {
184 result
= RegQueryValueExA(hkey
,
199 This code attempts to detect the Windows time zone, as set in the
200 Windows Date and Time control panel. It attempts to work on
201 multiple flavors of Windows (9x, Me, NT, 2000, XP) and on localized
202 installs. It works by directly interrogating the registry and
203 comparing the data there with the data returned by the
204 GetTimeZoneInformation API, along with some other strategies. The
205 registry contains time zone data under one of two keys (depending on
206 the flavor of Windows):
208 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones\
209 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
211 Under this key are several subkeys, one for each time zone. These
212 subkeys are named "Pacific" on Win9x/Me and "Pacific Standard Time"
213 on WinNT/2k/XP. There are some other wrinkles; see the code for
214 details. The subkey name is NOT LOCALIZED, allowing us to support
217 Under the subkey are data values. We care about:
219 Std Standard time display name, localized
220 TZI Binary block of data
222 The TZI data is of particular interest. It contains the offset, two
223 more offsets for standard and daylight time, and the start and end
224 rules. This is the same data returned by the GetTimeZoneInformation
225 API. The API may modify the data on the way out, so we have to be
226 careful, but essentially we do a binary comparison against the TZI
227 blocks of various registry keys. When we find a match, we know what
228 time zone Windows is set to. Since the registry key is not
229 localized, we can then translate the key through a simple table
230 lookup into the corresponding ICU time zone.
232 This strategy doesn't always work because there are zones which
233 share an offset and rules, so more than one TZI block will match.
234 For example, both Tokyo and Seoul are at GMT+9 with no DST rules;
235 their TZI blocks are identical. For these cases, we fall back to a
236 name lookup. We attempt to match the display name as stored in the
237 registry for the current zone to the display name stored in the
238 registry for various Windows zones. By comparing the registry data
239 directly we avoid conversion complications.
243 Based on original code by Carl Brown <cbrown@xnetinc.com>
247 * Main Windows time zone detection function. Returns the Windows
248 * time zone, translated to an ICU time zone, or NULL upon failure.
250 U_CFUNC
const char* U_EXPORT2
251 uprv_detectWindowsTimeZone() {
252 UErrorCode status
= U_ZERO_ERROR
;
253 UResourceBundle
* bundle
= NULL
;
255 char apiStdName
[MAX_LENGTH_ID
];
256 char regStdName
[MAX_LENGTH_ID
];
257 char tmpid
[MAX_LENGTH_ID
];
261 char ISOcode
[3]; /* 2 letter iso code */
266 TIME_ZONE_INFORMATION apiTZI
;
268 /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it
269 to TZI. We could also interrogate the registry directly; we do
270 this below if needed. */
271 uprv_memset(&apiTZI
, 0, sizeof(apiTZI
));
272 uprv_memset(&tziKey
, 0, sizeof(tziKey
));
273 uprv_memset(&tziReg
, 0, sizeof(tziReg
));
274 GetTimeZoneInformation(&apiTZI
);
275 tziKey
.bias
= apiTZI
.Bias
;
276 uprv_memcpy((char *)&tziKey
.standardDate
, (char*)&apiTZI
.StandardDate
,
277 sizeof(apiTZI
.StandardDate
));
278 uprv_memcpy((char *)&tziKey
.daylightDate
, (char*)&apiTZI
.DaylightDate
,
279 sizeof(apiTZI
.DaylightDate
));
281 /* Convert the wchar_t* standard name to char* */
282 uprv_memset(apiStdName
, 0, sizeof(apiStdName
));
283 wcstombs(apiStdName
, apiTZI
.StandardName
, MAX_LENGTH_ID
);
287 id
= GetUserGeoID(GEOCLASS_NATION
);
288 errorCode
= GetGeoInfo(id
,GEO_ISO2
,ISOcode
,3,0);
290 bundle
= ures_openDirect(NULL
, "windowsZones", &status
);
291 ures_getByKey(bundle
, "mapTimezones", bundle
, &status
);
293 /* Note: We get the winid not from static tables but from resource bundle. */
294 while (U_SUCCESS(status
) && ures_hasNext(bundle
)) {
295 UBool idFound
= FALSE
;
297 UResourceBundle
* winTZ
= ures_getNextResource(bundle
, NULL
, &status
);
298 if (U_FAILURE(status
)) {
301 winid
= ures_getKey(winTZ
);
302 result
= getTZI(winid
, &tziReg
);
304 if (result
== ERROR_SUCCESS
) {
305 /* Windows alters the DaylightBias in some situations.
306 Using the bias and the rules suffices, so overwrite
307 these unreliable fields. */
308 tziKey
.standardBias
= tziReg
.standardBias
;
309 tziKey
.daylightBias
= tziReg
.daylightBias
;
311 if (uprv_memcmp((char *)&tziKey
, (char*)&tziReg
, sizeof(tziKey
)) == 0) {
312 const UChar
* icuTZ
= NULL
;
313 if (errorCode
!= 0) {
314 icuTZ
= ures_getStringByKey(winTZ
, ISOcode
, &len
, &status
);
316 if (errorCode
==0 || icuTZ
==NULL
) {
317 /* fallback to default "001" and reset status */
318 status
= U_ZERO_ERROR
;
319 icuTZ
= ures_getStringByKey(winTZ
, "001", &len
, &status
);
322 if (U_SUCCESS(status
)) {
323 /* Get the standard name from the registry key to compare with
324 the one from Windows API call. */
325 uprv_memset(regStdName
, 0, sizeof(regStdName
));
326 result
= getSTDName(winid
, regStdName
, sizeof(regStdName
));
327 if (result
== ERROR_SUCCESS
) {
328 if (uprv_strcmp(apiStdName
, regStdName
) == 0) {
333 /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows.
334 * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching
335 * the current time zone information)
337 if (idFound
|| tmpid
[0] == 0) {
338 /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */
340 while (! (*icuTZ
== '\0' || *icuTZ
==' ')) {
341 tmpid
[index
++]=(char)(*icuTZ
++); /* safe to assume 'char' is ASCII compatible on windows */
355 * Copy the timezone ID to icuid to be returned.
358 len
= uprv_strlen(tmpid
);
359 icuid
= (char*)uprv_calloc(len
+ 1, sizeof(char));
361 uprv_strcpy(icuid
, tmpid
);
370 #endif /* U_PLATFORM_HAS_WIN32_API */