2 ********************************************************************************
3 * Copyright (C) 2005-2014, 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"
21 #include "unicode/ustring.h"
23 # define WIN32_LEAN_AND_MEAN
31 #define MAX_LENGTH_ID 40
33 /* The layout of the Tzi value in the registry */
39 SYSTEMTIME standardDate
;
40 SYSTEMTIME daylightDate
;
44 * Various registry keys and key fragments.
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";
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.
58 static const char* const WIN_TYPE_PROBE_REGKEY
[] = {
60 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones",
63 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\GMT"
65 /* otherwise: WIN_2K_XP_TYPE */
69 * The time zone root subkeys (under HKLM) for different flavors of
72 static const char* const TZ_REGKEY
[] = {
74 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\",
76 /* WIN_NT_TYPE | WIN_2K_XP_TYPE */
77 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\"
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
91 static int32_t gWinType
= 0;
93 static int32_t detectWindowsType()
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
104 for (winType
= 0; winType
< 2; winType
++) {
105 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
106 WIN_TYPE_PROBE_REGKEY
[winType
],
112 if (result
== ERROR_SUCCESS
) {
117 return winType
+1; /* +1 to bring it inline with the enum */
120 static LONG
openTZRegKey(HKEY
*hkey
, const char *winid
)
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];
129 /* This isn't thread safe, but it's good enough because the result should be constant per system. */
131 gWinType
= detectWindowsType();
134 uprv_strcpy(subKeyName
, TZ_REGKEY
[(gWinType
!= WIN_9X_ME_TYPE
)]);
135 name
= &subKeyName
[strlen(subKeyName
)];
136 uprv_strcat(subKeyName
, winid
);
138 if (gWinType
== WIN_9X_ME_TYPE
) {
139 /* Remove " Standard Time" */
140 char *pStd
= uprv_strstr(subKeyName
, STANDARD_TIME_REGKEY
);
146 result
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
154 static LONG
getTZI(const char *winid
, TZI
*tzi
)
156 DWORD cbData
= sizeof(TZI
);
160 result
= openTZRegKey(&hkey
, winid
);
162 if (result
== ERROR_SUCCESS
) {
163 result
= RegQueryValueExA(hkey
,
177 static LONG
getSTDName(const char *winid
, char *regStdName
, int32_t length
) {
178 DWORD cbData
= length
;
182 result
= openTZRegKey(&hkey
, winid
);
184 if (result
== ERROR_SUCCESS
) {
185 result
= RegQueryValueExA(hkey
,
199 static LONG
getTZKeyName(char* tzKeyName
, int32_t length
) {
202 DWORD cbData
= length
;
204 if(ERROR_SUCCESS
== RegOpenKeyExA(
211 result
= RegQueryValueExA(
224 This code attempts to detect the Windows time zone, as set in the
225 Windows Date and Time control panel. It attempts to work on
226 multiple flavors of Windows (9x, Me, NT, 2000, XP) and on localized
227 installs. It works by directly interrogating the registry and
228 comparing the data there with the data returned by the
229 GetTimeZoneInformation API, along with some other strategies. The
230 registry contains time zone data under one of two keys (depending on
231 the flavor of Windows):
233 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones\
234 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
236 Under this key are several subkeys, one for each time zone. These
237 subkeys are named "Pacific" on Win9x/Me and "Pacific Standard Time"
238 on WinNT/2k/XP. There are some other wrinkles; see the code for
239 details. The subkey name is NOT LOCALIZED, allowing us to support
242 Under the subkey are data values. We care about:
244 Std Standard time display name, localized
245 TZI Binary block of data
247 The TZI data is of particular interest. It contains the offset, two
248 more offsets for standard and daylight time, and the start and end
249 rules. This is the same data returned by the GetTimeZoneInformation
250 API. The API may modify the data on the way out, so we have to be
251 careful, but essentially we do a binary comparison against the TZI
252 blocks of various registry keys. When we find a match, we know what
253 time zone Windows is set to. Since the registry key is not
254 localized, we can then translate the key through a simple table
255 lookup into the corresponding ICU time zone.
257 This strategy doesn't always work because there are zones which
258 share an offset and rules, so more than one TZI block will match.
259 For example, both Tokyo and Seoul are at GMT+9 with no DST rules;
260 their TZI blocks are identical. For these cases, we fall back to a
261 name lookup. We attempt to match the display name as stored in the
262 registry for the current zone to the display name stored in the
263 registry for various Windows zones. By comparing the registry data
264 directly we avoid conversion complications.
268 Based on original code by Carl Brown <cbrown@xnetinc.com>
272 * Main Windows time zone detection function. Returns the Windows
273 * time zone, translated to an ICU time zone, or NULL upon failure.
275 U_CFUNC
const char* U_EXPORT2
276 uprv_detectWindowsTimeZone() {
277 UErrorCode status
= U_ZERO_ERROR
;
278 UResourceBundle
* bundle
= NULL
;
280 char apiStdName
[MAX_LENGTH_ID
];
281 char regStdName
[MAX_LENGTH_ID
];
282 char tmpid
[MAX_LENGTH_ID
];
286 UChar ISOcodeW
[3]; /* 2 letter iso code in UTF-16*/
287 char ISOcodeA
[3]; /* 2 letter iso code in ansi */
292 TIME_ZONE_INFORMATION apiTZI
;
294 BOOL isVistaOrHigher
;
295 BOOL tryPreVistaFallback
;
296 OSVERSIONINFO osVerInfo
;
298 /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it
299 to TZI. We could also interrogate the registry directly; we do
300 this below if needed. */
301 uprv_memset(&apiTZI
, 0, sizeof(apiTZI
));
302 uprv_memset(&tziKey
, 0, sizeof(tziKey
));
303 uprv_memset(&tziReg
, 0, sizeof(tziReg
));
304 GetTimeZoneInformation(&apiTZI
);
305 tziKey
.bias
= apiTZI
.Bias
;
306 uprv_memcpy((char *)&tziKey
.standardDate
, (char*)&apiTZI
.StandardDate
,
307 sizeof(apiTZI
.StandardDate
));
308 uprv_memcpy((char *)&tziKey
.daylightDate
, (char*)&apiTZI
.DaylightDate
,
309 sizeof(apiTZI
.DaylightDate
));
311 /* Convert the wchar_t* standard name to char* */
312 uprv_memset(apiStdName
, 0, sizeof(apiStdName
));
313 wcstombs(apiStdName
, apiTZI
.StandardName
, MAX_LENGTH_ID
);
317 id
= GetUserGeoID(GEOCLASS_NATION
);
318 errorCode
= GetGeoInfoW(id
,GEO_ISO2
,ISOcodeW
,3,0);
319 u_strToUTF8(ISOcodeA
, 3, NULL
, ISOcodeW
, 3, &status
);
321 bundle
= ures_openDirect(NULL
, "windowsZones", &status
);
322 ures_getByKey(bundle
, "mapTimezones", bundle
, &status
);
325 Windows Vista+ provides us with a "TimeZoneKeyName" that is not localized
326 and can be used to directly map a name in our bundle. Try to use that first
327 if we're on Vista or higher
329 uprv_memset(&osVerInfo
, 0, sizeof(osVerInfo
));
330 osVerInfo
.dwOSVersionInfoSize
= sizeof(osVerInfo
);
331 GetVersionEx(&osVerInfo
);
332 isVistaOrHigher
= osVerInfo
.dwMajorVersion
>= 6; /* actually includes Windows Server 2008 as well, but don't worry about it */
333 tryPreVistaFallback
= TRUE
;
334 if(isVistaOrHigher
) {
335 result
= getTZKeyName(regStdName
, sizeof(regStdName
));
336 if(ERROR_SUCCESS
== result
) {
337 UResourceBundle
* winTZ
= ures_getByKey(bundle
, regStdName
, NULL
, &status
);
338 if(U_SUCCESS(status
)) {
339 const UChar
* icuTZ
= NULL
;
340 if (errorCode
!= 0) {
341 icuTZ
= ures_getStringByKey(winTZ
, ISOcodeA
, &len
, &status
);
343 if (errorCode
==0 || icuTZ
==NULL
) {
344 /* fallback to default "001" and reset status */
345 status
= U_ZERO_ERROR
;
346 icuTZ
= ures_getStringByKey(winTZ
, "001", &len
, &status
);
349 if(U_SUCCESS(status
)) {
351 while (! (*icuTZ
== '\0' || *icuTZ
==' ')) {
352 tmpid
[index
++]=(char)(*icuTZ
++); /* safe to assume 'char' is ASCII compatible on windows */
355 tryPreVistaFallback
= FALSE
;
361 if(tryPreVistaFallback
) {
363 /* Note: We get the winid not from static tables but from resource bundle. */
364 while (U_SUCCESS(status
) && ures_hasNext(bundle
)) {
365 UBool idFound
= FALSE
;
367 UResourceBundle
* winTZ
= ures_getNextResource(bundle
, NULL
, &status
);
368 if (U_FAILURE(status
)) {
371 winid
= ures_getKey(winTZ
);
372 result
= getTZI(winid
, &tziReg
);
374 if (result
== ERROR_SUCCESS
) {
375 /* Windows alters the DaylightBias in some situations.
376 Using the bias and the rules suffices, so overwrite
377 these unreliable fields. */
378 tziKey
.standardBias
= tziReg
.standardBias
;
379 tziKey
.daylightBias
= tziReg
.daylightBias
;
381 if (uprv_memcmp((char *)&tziKey
, (char*)&tziReg
, sizeof(tziKey
)) == 0) {
382 const UChar
* icuTZ
= NULL
;
383 if (errorCode
!= 0) {
384 icuTZ
= ures_getStringByKey(winTZ
, ISOcodeA
, &len
, &status
);
386 if (errorCode
==0 || icuTZ
==NULL
) {
387 /* fallback to default "001" and reset status */
388 status
= U_ZERO_ERROR
;
389 icuTZ
= ures_getStringByKey(winTZ
, "001", &len
, &status
);
392 if (U_SUCCESS(status
)) {
393 /* Get the standard name from the registry key to compare with
394 the one from Windows API call. */
395 uprv_memset(regStdName
, 0, sizeof(regStdName
));
396 result
= getSTDName(winid
, regStdName
, sizeof(regStdName
));
397 if (result
== ERROR_SUCCESS
) {
398 if (uprv_strcmp(apiStdName
, regStdName
) == 0) {
403 /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows.
404 * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching
405 * the current time zone information)
407 if (idFound
|| tmpid
[0] == 0) {
408 /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */
410 while (! (*icuTZ
== '\0' || *icuTZ
==' ')) {
411 tmpid
[index
++]=(char)(*icuTZ
++); /* safe to assume 'char' is ASCII compatible on windows */
426 * Copy the timezone ID to icuid to be returned.
429 len
= uprv_strlen(tmpid
);
430 icuid
= (char*)uprv_calloc(len
+ 1, sizeof(char));
432 uprv_strcpy(icuid
, tmpid
);
441 #endif /* U_PLATFORM_HAS_WIN32_API */