Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 4 | ******************************************************************************** |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 5 | * Copyright (C) 2005-2015, International Business Machines |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 6 | * Corporation and others. All Rights Reserved. |
| 7 | ******************************************************************************** |
| 8 | * |
| 9 | * File WINTZ.CPP |
| 10 | * |
| 11 | ******************************************************************************** |
| 12 | */ |
| 13 | |
| 14 | #include "unicode/utypes.h" |
| 15 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 16 | #if U_PLATFORM_HAS_WIN32_API |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 17 | |
| 18 | #include "wintz.h" |
| 19 | #include "cmemory.h" |
| 20 | #include "cstring.h" |
| 21 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 22 | #include "unicode/ures.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 23 | #include "unicode/ustring.h" |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 24 | #include "uresimp.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 25 | |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 26 | #ifndef WIN32_LEAN_AND_MEAN |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 27 | # define WIN32_LEAN_AND_MEAN |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 28 | #endif |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 29 | # define VC_EXTRALEAN |
| 30 | # define NOUSER |
| 31 | # define NOSERVICE |
| 32 | # define NOIME |
| 33 | # define NOMCX |
| 34 | #include <windows.h> |
| 35 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 36 | U_NAMESPACE_BEGIN |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 37 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 38 | // The value of MAX_TIMEZONE_ID_LENGTH is 128, which is defined in DYNAMIC_TIME_ZONE_INFORMATION |
| 39 | #define MAX_TIMEZONE_ID_LENGTH 128 |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 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. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 46 | */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 47 | U_CFUNC const char* U_EXPORT2 |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 48 | uprv_detectWindowsTimeZone() |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 49 | { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 50 | UErrorCode status = U_ZERO_ERROR; |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 51 | char* icuid = nullptr; |
| 52 | char dynamicTZKeyName[MAX_TIMEZONE_ID_LENGTH]; |
| 53 | char tmpid[MAX_TIMEZONE_ID_LENGTH]; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 54 | int32_t len; |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 55 | int id = GEOID_NOT_AVAILABLE; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 56 | int errorCode; |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 57 | wchar_t ISOcodeW[3] = {}; /* 2 letter ISO code in UTF-16 */ |
| 58 | char ISOcode[3] = {}; /* 2 letter ISO code in UTF-8 */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 59 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 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)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 64 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 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 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 69 | |
| 70 | id = GetUserGeoID(GEOCLASS_NATION); |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 71 | errorCode = GetGeoInfoW(id, GEO_ISO2, ISOcodeW, 3, 0); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 72 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 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); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 76 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 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), UPRV_LENGTHOF(dynamicTZI.TimeZoneKeyName), &status); |
| 83 | |
| 84 | if (U_FAILURE(status)) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
| 88 | if (dynamicTZI.TimeZoneKeyName[0] != 0) { |
| 89 | UResourceBundle winTZ; |
| 90 | ures_initStackObject(&winTZ); |
| 91 | ures_getByKey(bundle.getAlias(), dynamicTZKeyName, &winTZ, &status); |
| 92 | |
| 93 | if (U_SUCCESS(status)) { |
| 94 | const UChar* icuTZ = nullptr; |
| 95 | if (errorCode != 0) { |
| 96 | icuTZ = ures_getStringByKey(&winTZ, ISOcode, &len, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 97 | } |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 98 | if (errorCode == 0 || icuTZ == nullptr) { |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 99 | /* fallback to default "001" and reset status */ |
| 100 | status = U_ZERO_ERROR; |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 101 | icuTZ = ures_getStringByKey(&winTZ, "001", &len, &status); |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 104 | if (U_SUCCESS(status)) { |
| 105 | int index = 0; |
| 106 | |
| 107 | while (!(*icuTZ == '\0' || *icuTZ == ' ')) { |
| 108 | // time zone IDs only contain ASCII invariant characters. |
| 109 | tmpid[index++] = (char)(*icuTZ++); |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 110 | } |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 111 | tmpid[index] = '\0'; |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 112 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 113 | } |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 114 | ures_close(&winTZ); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 117 | // Copy the timezone ID to icuid to be returned. |
| 118 | if (tmpid[0] != 0) { |
| 119 | icuid = uprv_strdup(tmpid); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 120 | } |
| 121 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 122 | return icuid; |
| 123 | } |
| 124 | |
Jungshik Shin | ccad447 | 2018-10-09 00:22:00 -0700 | [diff] [blame^] | 125 | U_NAMESPACE_END |
| 126 | #endif /* U_PLATFORM_HAS_WIN32_API */ |