diff --git a/BUILD b/BUILD index 14d6b97..4f1f58f 100644 --- a/BUILD +++ b/BUILD @@ -125,6 +125,18 @@ cc_test( ], ) +cc_test( + name = "time_zone_name_win_test", + size = "small", + srcs = ["src/time_zone_name_win_test.cc"], + target_compatible_with = ["@platforms//os:windows"], + deps = [ + ":time_zone", + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) + cc_test( name = "time_zone_fuzz_test", srcs = [ diff --git a/CMakeLists.txt b/CMakeLists.txt index f8bfbdd..0ab18df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ if (BUILD_TESTING) ${CMAKE_THREAD_LIBS_INIT} ) add_test(civil_time_test civil_time_test) + list(APPEND CCTZ_TESTS civil_time_test) add_executable(time_zone_lookup_test src/time_zone_lookup_test.cc) cctz_target_set_cxx_standard(time_zone_lookup_test) @@ -138,6 +139,7 @@ if (BUILD_TESTING) ${CMAKE_THREAD_LIBS_INIT} ) add_test(time_zone_lookup_test time_zone_lookup_test) + list(APPEND CCTZ_TESTS time_zone_lookup_test) add_executable(time_zone_format_test src/time_zone_format_test.cc) cctz_target_set_cxx_standard(time_zone_format_test) @@ -147,13 +149,25 @@ if (BUILD_TESTING) GMock::Main ) add_test(time_zone_format_test time_zone_format_test) + list(APPEND CCTZ_TESTS time_zone_format_test) + + if(WIN32) + add_executable(time_zone_name_win_test src/time_zone_name_win_test.cc) + cctz_target_set_cxx_standard(time_zone_name_win_test) + target_include_directories(time_zone_name_win_test PRIVATE ${GTEST_INCLUDE_DIRS}) + target_link_libraries(time_zone_name_win_test + cctz::cctz + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ) + add_test(time_zone_name_win_test time_zone_name_win_test) + list(APPEND CCTZ_TESTS time_zone_name_win_test) + endif() # tests runs on testdata set_property( TEST - civil_time_test - time_zone_format_test - time_zone_lookup_test + ${CCTZ_TESTS} PROPERTY ENVIRONMENT "TZDIR=${CMAKE_CURRENT_SOURCE_DIR}/testdata/zoneinfo" ) diff --git a/src/time_zone_name_win.cc b/src/time_zone_name_win.cc index daa9a56..d3d9566 100644 --- a/src/time_zone_name_win.cc +++ b/src/time_zone_name_win.cc @@ -112,7 +112,7 @@ ucal_getTimeZoneIDForWindowsID_func LoadIcuGetTimeZoneIDForWindowsID() { const auto ucal_getTimeZoneIDForWindowsIDRef = AsProcAddress( icu_dll, "ucal_getTimeZoneIDForWindowsID"); - if (ucal_getTimeZoneIDForWindowsIDRef != nullptr) { + if (ucal_getTimeZoneIDForWindowsIDRef == nullptr) { g_unavailable.store(true, std::memory_order_relaxed); return nullptr; } diff --git a/src/time_zone_name_win_test.cc b/src/time_zone_name_win_test.cc new file mode 100644 index 0000000..e7e9009 --- /dev/null +++ b/src/time_zone_name_win_test.cc @@ -0,0 +1,42 @@ +// Copyright 2026 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "time_zone_name_win.h" + +#include + +#include + +#include "cctz/time_zone.h" +#include "gtest/gtest.h" + +namespace cctz { + +TEST(TimeZoneNameWin, GetWindowsLocalTimeZone) { + // On Windows 10 1809+ (where icu.dll is available in System32), + // GetWindowsLocalTimeZone() should return a valid IANA time zone name. + // Note that LOAD_LIBRARY_SEARCH_SYSTEM32 is not sufficient to reliably load + // "icu.dll" in the production code, but it should be OK for testing purposes. + HMODULE icu_dll = + ::LoadLibraryExW(L"icu.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + const std::string tz = GetWindowsLocalTimeZone(); + if (icu_dll != nullptr) { + EXPECT_FALSE(tz.empty()); + ::FreeLibrary(icu_dll); + } else { + EXPECT_TRUE(tz.empty()); + } +} + +} // namespace cctz