|
| 1 | +diff --git a/absl/time/internal/cctz/src/time_zone_lookup.cc b/absl/time/internal/cctz/src/time_zone_lookup.cc |
| 2 | +index 989a5640..b6efe030 100644 |
| 3 | +--- a/absl/time/internal/cctz/src/time_zone_lookup.cc |
| 4 | ++++ b/absl/time/internal/cctz/src/time_zone_lookup.cc |
| 5 | +@@ -67,7 +67,7 @@ namespace { |
| 6 | + #if defined(USE_WIN32_LOCAL_TIME_ZONE) |
| 7 | + // Calls the WinRT Calendar.GetTimeZone method to obtain the IANA ID of the |
| 8 | + // local time zone. Returns an empty vector in case of an error. |
| 9 | +-std::string win32_local_time_zone(const HMODULE combase) { |
| 10 | ++static std::string win32_local_time_zone(const HMODULE combase) { |
| 11 | + std::string result; |
| 12 | + const auto ro_activate_instance = |
| 13 | + reinterpret_cast<decltype(&RoActivateInstance)>( |
| 14 | +@@ -138,6 +138,43 @@ std::string win32_local_time_zone(const HMODULE combase) { |
| 15 | + calendar->Release(); |
| 16 | + return result; |
| 17 | + } |
| 18 | ++ |
| 19 | ++static std::string get_once_win32_local_time_zone() { |
| 20 | ++ // Use the WinRT Calendar class to get the local time zone. This feature is |
| 21 | ++ // available on Windows 10 and later. The library is dynamically linked to |
| 22 | ++ // maintain binary compatibility with Windows XP - Windows 7. On Windows 8, |
| 23 | ++ // The combase.dll API functions are available but the RoActivateInstance |
| 24 | ++ // call will fail for the Calendar class. |
| 25 | ++ std::string winrt_tz; |
| 26 | ++ const HMODULE combase = |
| 27 | ++ LoadLibraryEx(_T("combase.dll"), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 28 | ++ if (combase) { |
| 29 | ++ const auto ro_initialize = reinterpret_cast<decltype(&::RoInitialize)>( |
| 30 | ++ GetProcAddress(combase, "RoInitialize")); |
| 31 | ++ const auto ro_uninitialize = reinterpret_cast<decltype(&::RoUninitialize)>( |
| 32 | ++ GetProcAddress(combase, "RoUninitialize")); |
| 33 | ++ if (ro_initialize && ro_uninitialize) { |
| 34 | ++ const HRESULT hr = ro_initialize(RO_INIT_MULTITHREADED); |
| 35 | ++ // RPC_E_CHANGED_MODE means that a previous RoInitialize call specified |
| 36 | ++ // a different concurrency model. The WinRT runtime is initialized and |
| 37 | ++ // should work for our purpose here, but we should *not* call |
| 38 | ++ // RoUninitialize because it's a failure. |
| 39 | ++ if (SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE) { |
| 40 | ++ winrt_tz = win32_local_time_zone(combase); |
| 41 | ++ if (SUCCEEDED(hr)) { |
| 42 | ++ ro_uninitialize(); |
| 43 | ++ } |
| 44 | ++ } |
| 45 | ++ } |
| 46 | ++ FreeLibrary(combase); |
| 47 | ++ } |
| 48 | ++ return winrt_tz; |
| 49 | ++} |
| 50 | ++ |
| 51 | ++static std::string& cached_win32_local_time_zone() { |
| 52 | ++ static std::string cached_time_zone = get_once_win32_local_time_zone(); |
| 53 | ++ return cached_time_zone; |
| 54 | ++} |
| 55 | + #endif |
| 56 | + } // namespace |
| 57 | + |
| 58 | +@@ -256,34 +293,7 @@ time_zone local_time_zone() { |
| 59 | + } |
| 60 | + #endif |
| 61 | + #if defined(USE_WIN32_LOCAL_TIME_ZONE) |
| 62 | +- // Use the WinRT Calendar class to get the local time zone. This feature is |
| 63 | +- // available on Windows 10 and later. The library is dynamically linked to |
| 64 | +- // maintain binary compatibility with Windows XP - Windows 7. On Windows 8, |
| 65 | +- // The combase.dll API functions are available but the RoActivateInstance |
| 66 | +- // call will fail for the Calendar class. |
| 67 | +- std::string winrt_tz; |
| 68 | +- const HMODULE combase = |
| 69 | +- LoadLibraryEx(_T("combase.dll"), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 70 | +- if (combase) { |
| 71 | +- const auto ro_initialize = reinterpret_cast<decltype(&::RoInitialize)>( |
| 72 | +- GetProcAddress(combase, "RoInitialize")); |
| 73 | +- const auto ro_uninitialize = reinterpret_cast<decltype(&::RoUninitialize)>( |
| 74 | +- GetProcAddress(combase, "RoUninitialize")); |
| 75 | +- if (ro_initialize && ro_uninitialize) { |
| 76 | +- const HRESULT hr = ro_initialize(RO_INIT_MULTITHREADED); |
| 77 | +- // RPC_E_CHANGED_MODE means that a previous RoInitialize call specified |
| 78 | +- // a different concurrency model. The WinRT runtime is initialized and |
| 79 | +- // should work for our purpose here, but we should *not* call |
| 80 | +- // RoUninitialize because it's a failure. |
| 81 | +- if (SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE) { |
| 82 | +- winrt_tz = win32_local_time_zone(combase); |
| 83 | +- if (SUCCEEDED(hr)) { |
| 84 | +- ro_uninitialize(); |
| 85 | +- } |
| 86 | +- } |
| 87 | +- } |
| 88 | +- FreeLibrary(combase); |
| 89 | +- } |
| 90 | ++ std::string winrt_tz = cached_win32_local_time_zone(); |
| 91 | + if (!winrt_tz.empty()) { |
| 92 | + zone = winrt_tz.c_str(); |
| 93 | + } |
0 commit comments