1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html |
3 | /* |
4 | ******************************************************************************** |
5 | * Copyright (C) 2005-2015, International Business Machines |
6 | * Corporation and others. All Rights Reserved. |
7 | ******************************************************************************** |
8 | * |
9 | * File WINTZ.CPP |
10 | * |
11 | ******************************************************************************** |
12 | */ |
13 | |
14 | #include "unicode/utypes.h" |
15 | |
16 | #if U_PLATFORM_USES_ONLY_WIN32_API |
17 | |
18 | #include "wintz.h" |
19 | #include "cmemory.h" |
20 | #include "cstring.h" |
21 | |
22 | #include "unicode/ures.h" |
23 | #include "unicode/ustring.h" |
24 | #include "uresimp.h" |
25 | |
26 | #ifndef WIN32_LEAN_AND_MEAN |
27 | # define WIN32_LEAN_AND_MEAN |
28 | #endif |
29 | # define VC_EXTRALEAN |
30 | # define NOUSER |
31 | # define NOSERVICE |
32 | # define NOIME |
33 | # define NOMCX |
34 | #include <windows.h> |
35 | |
36 | U_NAMESPACE_BEGIN |
37 | |
38 | // The max size of TimeZoneKeyName is 128, defined in DYNAMIC_TIME_ZONE_INFORMATION |
39 | #define MAX_TIMEZONE_ID_LENGTH 128 |
40 | |
41 | /** |
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. |
46 | */ |
47 | U_INTERNAL const char* U_EXPORT2 |
48 | uprv_detectWindowsTimeZone() |
49 | { |
50 | UErrorCode status = U_ZERO_ERROR; |
51 | char* icuid = nullptr; |
52 | char dynamicTZKeyName[MAX_TIMEZONE_ID_LENGTH]; |
53 | char tmpid[MAX_TIMEZONE_ID_LENGTH]; |
54 | int32_t len; |
55 | int id = GEOID_NOT_AVAILABLE; |
56 | int errorCode; |
57 | wchar_t ISOcodeW[3] = {}; /* 2 letter ISO code in UTF-16 */ |
58 | char ISOcode[3] = {}; /* 2 letter ISO code in UTF-8 */ |
59 | |
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)); |
64 | |
65 | /* Obtain TIME_ZONE_INFORMATION from the API and get the non-localized time zone name. */ |
66 | if (TIME_ZONE_ID_INVALID == GetDynamicTimeZoneInformation(&dynamicTZI)) { |
67 | return nullptr; |
68 | } |
69 | |
70 | id = GetUserGeoID(GEOCLASS_NATION); |
71 | errorCode = GetGeoInfoW(id, GEO_ISO2, ISOcodeW, 3, 0); |
72 | |
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); |
76 | |
77 | LocalUResourceBundlePointer bundle(ures_openDirect(nullptr, "windowsZones" , &status)); |
78 | ures_getByKey(bundle.getAlias(), "mapTimezones" , bundle.getAlias(), &status); |
79 | |
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); |
83 | |
84 | if (U_FAILURE(status)) { |
85 | return nullptr; |
86 | } |
87 | |
88 | if (dynamicTZI.TimeZoneKeyName[0] != 0) { |
89 | StackUResourceBundle winTZ; |
90 | ures_getByKey(bundle.getAlias(), dynamicTZKeyName, winTZ.getAlias(), &status); |
91 | |
92 | if (U_SUCCESS(status)) { |
93 | const UChar* icuTZ = nullptr; |
94 | if (errorCode != 0) { |
95 | icuTZ = ures_getStringByKey(winTZ.getAlias(), ISOcode, &len, &status); |
96 | } |
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); |
101 | } |
102 | |
103 | if (U_SUCCESS(status)) { |
104 | int index = 0; |
105 | |
106 | while (!(*icuTZ == '\0' || *icuTZ == ' ')) { |
107 | // time zone IDs only contain ASCII invariant characters. |
108 | tmpid[index++] = (char)(*icuTZ++); |
109 | } |
110 | tmpid[index] = '\0'; |
111 | } |
112 | } |
113 | } |
114 | |
115 | // Copy the timezone ID to icuid to be returned. |
116 | if (tmpid[0] != 0) { |
117 | icuid = uprv_strdup(tmpid); |
118 | } |
119 | |
120 | return icuid; |
121 | } |
122 | |
123 | U_NAMESPACE_END |
124 | #endif /* U_PLATFORM_USES_ONLY_WIN32_API */ |
125 | |