Skip to content

Commit ed71575

Browse files
wgtmacdongjoon-hyun
authored andcommitted
ORC-1663: [C++] Enable TestTimezone.testMissingTZDB on Windows
### What changes were proposed in this pull request? Enable TestTimezone.testMissingTZDB unit test to run on Windows. ### Why are the changes needed? When /usr/share/zoneinfo is unavailable and TZDIR env is unset, creating C++ ORC reader will crash on Windows. We need to better deal with this case. See context from the Apache Arrow community: apache/arrow#36026 and apache/arrow#40633 ### How was this patch tested? Make sure the test passes on Windows. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #1856 from wgtmac/win_tz_test. Authored-by: Gang Wu <ustcwg@gmail.com> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
1 parent 4eaf306 commit ed71575

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

c++/test/TestTimezone.cc

+23-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "wrap/gmock.h"
2222
#include "wrap/gtest-wrapper.h"
2323

24+
#include <cstdlib>
2425
#include <iostream>
2526
#include <vector>
2627

@@ -403,20 +404,37 @@ namespace orc {
403404
EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000));
404405
}
405406

406-
#ifndef _MSC_VER
407+
bool setEnv(const char* name, const char* value) {
408+
#ifdef _MSC_VER
409+
return _putenv_s(name, value) == 0;
410+
#else
411+
return setenv(name, value, 1) == 0;
412+
#endif
413+
}
414+
415+
bool delEnv(const char* name) {
416+
#ifdef _MSC_VER
417+
return _putenv_s(name, "") == 0;
418+
#else
419+
return unsetenv(name) == 0;
420+
#endif
421+
}
422+
407423
TEST(TestTimezone, testMissingTZDB) {
408424
const char* tzDirBackup = std::getenv("TZDIR");
409-
setenv("TZDIR", "/path/to/wrong/tzdb", 1);
425+
if (tzDirBackup != nullptr) {
426+
ASSERT_TRUE(delEnv("TZDIR"));
427+
}
428+
ASSERT_TRUE(setEnv("TZDIR", "/path/to/wrong/tzdb"));
410429
EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); },
411430
testing::ThrowsMessage<TimezoneError>(testing::HasSubstr(
412431
"Time zone file /path/to/wrong/tzdb/America/Los_Angeles does not exist."
413432
" Please install IANA time zone database and set TZDIR env.")));
414433
if (tzDirBackup != nullptr) {
415-
setenv("TZDIR", tzDirBackup, 1);
434+
ASSERT_TRUE(setEnv("TZDIR", tzDirBackup));
416435
} else {
417-
unsetenv("TZDIR");
436+
ASSERT_TRUE(delEnv("TZDIR"));
418437
}
419438
}
420-
#endif
421439

422440
} // namespace orc

0 commit comments

Comments
 (0)