1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 2005-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 ********************************************************************************
14 #include "unicode/utypes.h"
16 #if U_PLATFORM_USES_ONLY_WIN32_API
22 #include "unicode/ures.h"
23 #include "unicode/ustring.h"
26 #ifndef WIN32_LEAN_AND_MEAN
27 # define WIN32_LEAN_AND_MEAN
38 // The max size of TimeZoneKeyName is 128, defined in DYNAMIC_TIME_ZONE_INFORMATION
39 #define MAX_TIMEZONE_ID_LENGTH 128
42 * Main Windows time zone detection function.
43 * Returns the Windows time zone converted to an ICU time zone as a heap-allocated buffer, or nullptr upon failure.
44 * Note: We use the Win32 API GetDynamicTimeZoneInformation to get the current time zone info.
45 * This API returns a non-localized time zone name, which we can then map to an ICU time zone name.
47 U_INTERNAL
const char* U_EXPORT2
48 uprv_detectWindowsTimeZone()
50 UErrorCode status
= U_ZERO_ERROR
;
51 char* icuid
= nullptr;
52 char dynamicTZKeyName
[MAX_TIMEZONE_ID_LENGTH
];
53 char tmpid
[MAX_TIMEZONE_ID_LENGTH
];
55 int id
= GEOID_NOT_AVAILABLE
;
57 wchar_t ISOcodeW
[3] = {}; /* 2 letter ISO code in UTF-16 */
58 char ISOcode
[3] = {}; /* 2 letter ISO code in UTF-8 */
60 DYNAMIC_TIME_ZONE_INFORMATION dynamicTZI
;
61 uprv_memset(&dynamicTZI
, 0, sizeof(dynamicTZI
));
62 uprv_memset(dynamicTZKeyName
, 0, sizeof(dynamicTZKeyName
));
63 uprv_memset(tmpid
, 0, sizeof(tmpid
));
65 /* Obtain TIME_ZONE_INFORMATION from the API and get the non-localized time zone name. */
66 if (TIME_ZONE_ID_INVALID
== GetDynamicTimeZoneInformation(&dynamicTZI
)) {
70 id
= GetUserGeoID(GEOCLASS_NATION
);
71 errorCode
= GetGeoInfoW(id
, GEO_ISO2
, ISOcodeW
, 3, 0);
73 // convert from wchar_t* (UTF-16 on Windows) to char* (UTF-8).
74 u_strToUTF8(ISOcode
, UPRV_LENGTHOF(ISOcode
), nullptr,
75 reinterpret_cast<const UChar
*>(ISOcodeW
), UPRV_LENGTHOF(ISOcodeW
), &status
);
77 LocalUResourceBundlePointer
bundle(ures_openDirect(nullptr, "windowsZones", &status
));
78 ures_getByKey(bundle
.getAlias(), "mapTimezones", bundle
.getAlias(), &status
);
80 // convert from wchar_t* (UTF-16 on Windows) to char* (UTF-8).
81 u_strToUTF8(dynamicTZKeyName
, UPRV_LENGTHOF(dynamicTZKeyName
), nullptr,
82 reinterpret_cast<const UChar
*>(dynamicTZI
.TimeZoneKeyName
), -1, &status
);
84 if (U_FAILURE(status
)) {
88 if (dynamicTZI
.TimeZoneKeyName
[0] != 0) {
89 StackUResourceBundle winTZ
;
90 ures_getByKey(bundle
.getAlias(), dynamicTZKeyName
, winTZ
.getAlias(), &status
);
92 if (U_SUCCESS(status
)) {
93 const UChar
* icuTZ
= nullptr;
95 icuTZ
= ures_getStringByKey(winTZ
.getAlias(), ISOcode
, &len
, &status
);
97 if (errorCode
== 0 || icuTZ
== nullptr) {
98 /* fallback to default "001" and reset status */
99 status
= U_ZERO_ERROR
;
100 icuTZ
= ures_getStringByKey(winTZ
.getAlias(), "001", &len
, &status
);
103 if (U_SUCCESS(status
)) {
106 while (!(*icuTZ
== '\0' || *icuTZ
== ' ')) {
107 // time zone IDs only contain ASCII invariant characters.
108 tmpid
[index
++] = (char)(*icuTZ
++);
115 // Copy the timezone ID to icuid to be returned.
117 icuid
= uprv_strdup(tmpid
);
124 #endif /* U_PLATFORM_USES_ONLY_WIN32_API */