|
| 1 | +// Copyright 2025 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "absl/time/internal/cctz/src/time_zone_name_win.h" |
| 16 | + |
| 17 | +#include "absl/base/config.h" |
| 18 | + |
| 19 | +#if !defined(NOMINMAX) |
| 20 | +#define NOMINMAX |
| 21 | +#endif // !defined(NOMINMAX) |
| 22 | +#include <windows.h> |
| 23 | + |
| 24 | +#include <algorithm> |
| 25 | +#include <atomic> |
| 26 | +#include <cstdint> |
| 27 | +#include <limits> |
| 28 | +#include <string> |
| 29 | +#include <type_traits> |
| 30 | +#include <utility> |
| 31 | + |
| 32 | +namespace absl { |
| 33 | +ABSL_NAMESPACE_BEGIN |
| 34 | +namespace time_internal { |
| 35 | +namespace cctz { |
| 36 | +namespace { |
| 37 | + |
| 38 | +// Define UChar as wchar_t here because Win32 APIs receive UTF-16 strings as |
| 39 | +// wchar_t* instead of char16_t*. Using char16_t would require additional casts. |
| 40 | +using UChar = wchar_t; |
| 41 | + |
| 42 | +enum UErrorCode : std::int32_t { |
| 43 | + U_ZERO_ERROR = 0, |
| 44 | + U_BUFFER_OVERFLOW_ERROR = 15, |
| 45 | +}; |
| 46 | + |
| 47 | +bool U_SUCCESS(UErrorCode error) { return error <= U_ZERO_ERROR; } |
| 48 | + |
| 49 | +using ucal_getTimeZoneIDForWindowsID_func = std::int32_t(__cdecl*)( |
| 50 | + const UChar* winid, std::int32_t len, const char* region, UChar* id, |
| 51 | + std::int32_t id_capacity, UErrorCode* status); |
| 52 | + |
| 53 | +std::atomic<bool> g_unavailable; |
| 54 | +std::atomic<ucal_getTimeZoneIDForWindowsID_func> |
| 55 | + g_ucal_getTimeZoneIDForWindowsID; |
| 56 | + |
| 57 | +template <typename T> |
| 58 | +static T AsProcAddress(HMODULE module, const char* name) { |
| 59 | + static_assert( |
| 60 | + std::is_pointer<T>::value && |
| 61 | + std::is_function<typename std::remove_pointer<T>::type>::value, |
| 62 | + "T must be a function pointer type"); |
| 63 | + const auto proc_address = ::GetProcAddress(module, name); |
| 64 | + return reinterpret_cast<T>(reinterpret_cast<void*>(proc_address)); |
| 65 | +} |
| 66 | + |
| 67 | +std::wstring GetSystem32Dir() { |
| 68 | + std::wstring result; |
| 69 | + std::uint32_t len = std::max<std::uint32_t>( |
| 70 | + static_cast<std::uint32_t>(std::min<size_t>( |
| 71 | + result.capacity(), std::numeric_limits<std::uint32_t>::max())), |
| 72 | + 1); |
| 73 | + do { |
| 74 | + result.resize(len); |
| 75 | + len = ::GetSystemDirectoryW(&result[0], len); |
| 76 | + } while (len > result.size()); |
| 77 | + result.resize(len); |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +ucal_getTimeZoneIDForWindowsID_func LoadIcuGetTimeZoneIDForWindowsID() { |
| 82 | + // This function is intended to be lock free to avoid potential deadlocks |
| 83 | + // with loader-lock taken inside LoadLibraryW. As LoadLibraryW and |
| 84 | + // GetProcAddress are idempotent unless the DLL is unloaded, we just need to |
| 85 | + // make sure global variables are read/written atomically, where |
| 86 | + // memory_order_relaxed is also acceptable. |
| 87 | + |
| 88 | + if (g_unavailable.load(std::memory_order_relaxed)) { |
| 89 | + return nullptr; |
| 90 | + } |
| 91 | + |
| 92 | + { |
| 93 | + const auto ucal_getTimeZoneIDForWindowsIDRef = |
| 94 | + g_ucal_getTimeZoneIDForWindowsID.load(std::memory_order_relaxed); |
| 95 | + if (ucal_getTimeZoneIDForWindowsIDRef != nullptr) { |
| 96 | + return ucal_getTimeZoneIDForWindowsIDRef; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const std::wstring system32_dir = GetSystem32Dir(); |
| 101 | + if (system32_dir.empty()) { |
| 102 | + g_unavailable.store(true, std::memory_order_relaxed); |
| 103 | + return nullptr; |
| 104 | + } |
| 105 | + |
| 106 | + // Here LoadLibraryExW(L"icu.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) does |
| 107 | + // not work if "icu.dll" is already loaded from somewhere other than the |
| 108 | + // system32 directory. Specifying the full path with LoadLibraryW is more |
| 109 | + // reliable. |
| 110 | + const std::wstring icu_dll_path = system32_dir + L"\\icu.dll"; |
| 111 | + const HMODULE icu_dll = ::LoadLibraryW(icu_dll_path.c_str()); |
| 112 | + if (icu_dll == nullptr) { |
| 113 | + g_unavailable.store(true, std::memory_order_relaxed); |
| 114 | + return nullptr; |
| 115 | + } |
| 116 | + |
| 117 | + const auto ucal_getTimeZoneIDForWindowsIDRef = |
| 118 | + AsProcAddress<ucal_getTimeZoneIDForWindowsID_func>( |
| 119 | + icu_dll, "ucal_getTimeZoneIDForWindowsID"); |
| 120 | + if (ucal_getTimeZoneIDForWindowsIDRef != nullptr) { |
| 121 | + g_unavailable.store(true, std::memory_order_relaxed); |
| 122 | + return nullptr; |
| 123 | + } |
| 124 | + |
| 125 | + g_ucal_getTimeZoneIDForWindowsID.store(ucal_getTimeZoneIDForWindowsIDRef, |
| 126 | + std::memory_order_relaxed); |
| 127 | + |
| 128 | + return ucal_getTimeZoneIDForWindowsIDRef; |
| 129 | +} |
| 130 | + |
| 131 | +// Convert wchar_t array (UTF-16) to UTF-8 string |
| 132 | +std::string Utf16ToUtf8(const wchar_t* ptr, size_t size) { |
| 133 | + if (size > std::numeric_limits<int>::max()) { |
| 134 | + return std::string(); |
| 135 | + } |
| 136 | + const int chars_len = static_cast<int>(size); |
| 137 | + std::string result; |
| 138 | + std::int32_t len = std::max<std::int32_t>( |
| 139 | + static_cast<std::int32_t>(std::min<size_t>( |
| 140 | + result.capacity(), std::numeric_limits<std::int32_t>::max())), |
| 141 | + 1); |
| 142 | + do { |
| 143 | + result.resize(len); |
| 144 | + // TODO: Switch to std::string::data() when we require C++17 or higher. |
| 145 | + len = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, ptr, chars_len, |
| 146 | + &result[0], len, nullptr, nullptr); |
| 147 | + } while (len > result.size()); |
| 148 | + result.resize(len); |
| 149 | + return result; |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace |
| 153 | + |
| 154 | +std::string GetWindowsLocalTimeZone() { |
| 155 | + const auto getTimeZoneIDForWindowsID = LoadIcuGetTimeZoneIDForWindowsID(); |
| 156 | + if (getTimeZoneIDForWindowsID == nullptr) { |
| 157 | + return std::string(); |
| 158 | + } |
| 159 | + |
| 160 | + DYNAMIC_TIME_ZONE_INFORMATION info = {}; |
| 161 | + if (::GetDynamicTimeZoneInformation(&info) == TIME_ZONE_ID_INVALID) { |
| 162 | + return std::string(); |
| 163 | + } |
| 164 | + |
| 165 | + std::wstring result; |
| 166 | + std::int32_t len = std::max<std::int32_t>( |
| 167 | + static_cast<std::int32_t>(std::min<size_t>( |
| 168 | + result.capacity(), std::numeric_limits<std::int32_t>::max())), |
| 169 | + 1); |
| 170 | + for (;;) { |
| 171 | + UErrorCode status = U_ZERO_ERROR; |
| 172 | + result.resize(len); |
| 173 | + len = getTimeZoneIDForWindowsID(info.TimeZoneKeyName, -1, nullptr, |
| 174 | + &result[0], len, &status); |
| 175 | + if (U_SUCCESS(status)) { |
| 176 | + return Utf16ToUtf8(result.data(), len); |
| 177 | + } |
| 178 | + if (status != U_BUFFER_OVERFLOW_ERROR) { |
| 179 | + return std::string(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace cctz |
| 185 | +} // namespace time_internal |
| 186 | +ABSL_NAMESPACE_END |
| 187 | +} // namespace absl |
0 commit comments