Skip to content

Commit 7dbf042

Browse files
committed
apacheGH-49351: [C++] Check TZDIR environment variable in vendored date library
The vendored Howard Hinnant date library hardcodes /usr/share/zoneinfo as the timezone database path. This adds a TZDIR check in discover_tz_dir() before falling back to platform-specific defaults, consistent with POSIX conventions. This fixes timezone operations on non-FHS Linux distributions (e.g. NixOS) where zoneinfo lives under a non-standard path.
1 parent b7ed8df commit 7dbf042

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎cpp/src/arrow/public_api_test.cc‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#include <filesystem>
1819
#include <sstream>
1920
#include <string>
2021

@@ -122,6 +123,34 @@ TEST(Misc, BuildInfo) {
122123
ASSERT_THAT(info.full_so_version, ::testing::HasSubstr(info.so_version));
123124
}
124125

126+
#ifndef _WIN32
127+
TEST(Misc, TZDIREnvironmentVariable) {
128+
// Find a valid zoneinfo directory
129+
std::string tz_dir;
130+
const char* env_tzdir = std::getenv("TZDIR");
131+
if (env_tzdir != nullptr &&
132+
std::filesystem::is_directory(env_tzdir)) {
133+
tz_dir = env_tzdir;
134+
} else if (std::filesystem::is_directory(
135+
"/usr/share/zoneinfo")) {
136+
tz_dir = "/usr/share/zoneinfo";
137+
} else {
138+
GTEST_SKIP() << "No system zoneinfo directory found";
139+
}
140+
141+
// Set TZDIR and verify timezone resolution works
142+
EnvVarGuard guard("TZDIR", tz_dir);
143+
auto arr = ArrayFromJSON(
144+
timestamp(TimeUnit::SECOND, "UTC"), "[0]");
145+
ASSERT_OK_AND_ASSIGN(
146+
auto result,
147+
compute::Cast(
148+
arr,
149+
timestamp(TimeUnit::SECOND, "America/New_York")));
150+
ASSERT_NE(result.make_array(), nullptr);
151+
}
152+
#endif
153+
125154
TEST(Misc, SetTimezoneConfig) {
126155
#ifndef _WIN32
127156
GTEST_SKIP() << "Can only set the Timezone database on Windows";

‎cpp/src/arrow/vendored/datetime/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ The following changes are made:
2525
- enclose the `date` namespace inside the `arrow_vendored` namespace
2626
- fix 4 declarations like `CONSTCD11 date::day operator "" _d(unsigned long long d) NOEXCEPT;`
2727
to not have offending whitespace for modern clang:
28-
`CONSTCD11 date::day operator ""_d(unsigned long long d) NOEXCEPT;`
28+
`CONSTCD11 date::day operator ""_d(unsigned long long d) NOEXCEPT;`
29+
- check the TZDIR environment variable in `discover_tz_dir()` (tz.cpp) before
30+
falling back to hardcoded paths, for compatibility with non-FHS Linux
31+
distributions (e.g. NixOS)
2932

3033
## How to update
3134

‎cpp/src/arrow/vendored/datetime/tz.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ discover_tz_dir()
486486
{
487487
struct stat sb;
488488
using namespace std;
489+
// Check TZDIR environment variable first (POSIX standard)
490+
const char* tz_dir_env = std::getenv("TZDIR");
491+
if (tz_dir_env != nullptr
492+
&& stat(tz_dir_env, &sb) == 0
493+
&& S_ISDIR(sb.st_mode))
494+
return tz_dir_env;
489495
# if defined(ANDROID) || defined(__ANDROID__)
490496
CONSTDATA auto tz_dir_default = "/apex/com.android.tzdata/etc/tz";
491497
CONSTDATA auto tz_dir_fallback = "/system/usr/share/zoneinfo";

‎python/pyarrow/conftest.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@
111111
if sys.platform == "win32":
112112
defaults['timezone_data'] = windows_has_tzdata()
113113
elif sys.platform == "emscripten":
114-
defaults['timezone_data'] = os.path.exists("/usr/share/zoneinfo")
114+
tz_dir = os.environ.get("TZDIR", "/usr/share/zoneinfo")
115+
defaults['timezone_data'] = os.path.exists(tz_dir)
115116

116117
try:
117118
import cython # noqa

0 commit comments

Comments
 (0)