Skip to content

Commit d09a3fd

Browse files
committed
Fix inverted null check in time_zone_name_win.cc
This commit follows up the previous commit [1], which aimed to improve the code but ended up introducing an inverted null check in time_zone_name_win.cc. As a result, LoadIcuGetTimeZoneIDForWindowsID() currently returns nullptr when it should be returning a valid time zone ID. A unit test is also added, as the fallback chain in local_time_zone() makes it difficult to verify the behavior of LoadIcuGetTimeZoneIDForWindowsID() in isolation. The test ensures that the function correctly returns a valid time zone ID on Windows. This is also a preparation for implementing TimeZoneIf with Windows time APIs (#328). [1] 27ca173
1 parent 00fc77b commit d09a3fd

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ cc_test(
125125
],
126126
)
127127

128+
cc_test(
129+
name = "time_zone_name_win_test",
130+
size = "small",
131+
srcs = ["src/time_zone_name_win_test.cc"],
132+
target_compatible_with = ["@platforms//os:windows"],
133+
deps = [
134+
":time_zone",
135+
"@googletest//:gtest",
136+
"@googletest//:gtest_main",
137+
],
138+
)
139+
128140
cc_test(
129141
name = "time_zone_fuzz_test",
130142
srcs = [

CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ if (BUILD_TESTING)
127127
${CMAKE_THREAD_LIBS_INIT}
128128
)
129129
add_test(civil_time_test civil_time_test)
130+
list(APPEND CCTZ_TESTS civil_time_test)
130131

131132
add_executable(time_zone_lookup_test src/time_zone_lookup_test.cc)
132133
cctz_target_set_cxx_standard(time_zone_lookup_test)
@@ -138,6 +139,7 @@ if (BUILD_TESTING)
138139
${CMAKE_THREAD_LIBS_INIT}
139140
)
140141
add_test(time_zone_lookup_test time_zone_lookup_test)
142+
list(APPEND CCTZ_TESTS time_zone_lookup_test)
141143

142144
add_executable(time_zone_format_test src/time_zone_format_test.cc)
143145
cctz_target_set_cxx_standard(time_zone_format_test)
@@ -147,13 +149,25 @@ if (BUILD_TESTING)
147149
GMock::Main
148150
)
149151
add_test(time_zone_format_test time_zone_format_test)
152+
list(APPEND CCTZ_TESTS time_zone_format_test)
153+
154+
if(WIN32)
155+
add_executable(time_zone_name_win_test src/time_zone_name_win_test.cc)
156+
cctz_target_set_cxx_standard(time_zone_name_win_test)
157+
target_include_directories(time_zone_name_win_test PRIVATE ${GTEST_INCLUDE_DIRS})
158+
target_link_libraries(time_zone_name_win_test
159+
cctz::cctz
160+
${GTEST_BOTH_LIBRARIES}
161+
${CMAKE_THREAD_LIBS_INIT}
162+
)
163+
add_test(time_zone_name_win_test time_zone_name_win_test)
164+
list(APPEND CCTZ_TESTS time_zone_name_win_test)
165+
endif()
150166

151167
# tests runs on testdata
152168
set_property(
153169
TEST
154-
civil_time_test
155-
time_zone_format_test
156-
time_zone_lookup_test
170+
${CCTZ_TESTS}
157171
PROPERTY
158172
ENVIRONMENT "TZDIR=${CMAKE_CURRENT_SOURCE_DIR}/testdata/zoneinfo"
159173
)

src/time_zone_name_win.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ucal_getTimeZoneIDForWindowsID_func LoadIcuGetTimeZoneIDForWindowsID() {
112112
const auto ucal_getTimeZoneIDForWindowsIDRef =
113113
AsProcAddress<ucal_getTimeZoneIDForWindowsID_func>(
114114
icu_dll, "ucal_getTimeZoneIDForWindowsID");
115-
if (ucal_getTimeZoneIDForWindowsIDRef != nullptr) {
115+
if (ucal_getTimeZoneIDForWindowsIDRef == nullptr) {
116116
g_unavailable.store(true, std::memory_order_relaxed);
117117
return nullptr;
118118
}

src/time_zone_name_win_test.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2026 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 "time_zone_name_win.h"
16+
17+
#include <string>
18+
19+
#include <windows.h>
20+
21+
#include "cctz/time_zone.h"
22+
#include "gtest/gtest.h"
23+
24+
namespace cctz {
25+
26+
TEST(TimeZoneNameWin, GetWindowsLocalTimeZone) {
27+
// On Windows 10 1809+ (where icu.dll is available in System32),
28+
// GetWindowsLocalTimeZone() should return a valid IANA time zone name.
29+
// Note that LOAD_LIBRARY_SEARCH_SYSTEM32 is not sufficient to reliably load
30+
// "icu.dll" in the production code, but it should be OK for testing purposes.
31+
HMODULE icu_dll =
32+
::LoadLibraryExW(L"icu.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
33+
const std::string tz = GetWindowsLocalTimeZone();
34+
if (icu_dll != nullptr) {
35+
EXPECT_FALSE(tz.empty());
36+
::FreeLibrary(icu_dll);
37+
} else {
38+
EXPECT_TRUE(tz.empty());
39+
}
40+
}
41+
42+
} // namespace cctz

0 commit comments

Comments
 (0)