Skip to content

Commit c9a2149

Browse files
committed
Fix R's tzdb
1 parent adadc8f commit c9a2149

File tree

10 files changed

+79
-5
lines changed

10 files changed

+79
-5
lines changed

c_glib/test/test-assume-timezone-options.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_nonexistent_property
4545
end
4646

4747
def test_assume_timezone_function
48+
omit("std::chrono not available on Windows MinGW") if Gem.win_platform?
4849
args = [
4950
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
5051
]

c_glib/test/test-day-of-week-options.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_week_start_property
3939
end
4040

4141
def test_day_of_week_function_with_count_from_zero_false
42+
omit("std::chrono not available on Windows MinGW") if Gem.win_platform?
4243
args = [
4344
# 2017-09-09T10:33:10Z (Saturday)
4445
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),
@@ -50,6 +51,7 @@ def test_day_of_week_function_with_count_from_zero_false
5051
end
5152

5253
def test_day_of_week_function_with_week_start
54+
omit("std::chrono not available on Windows MinGW") if Gem.win_platform?
5355
args = [
5456
# 2017-09-09T10:33:10Z (Saturday)
5557
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190000])),

c_glib/test/test-strftime-options.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_locale_property
3535
end
3636

3737
def test_strftime_function
38+
omit("std::chrono not available on Windows MinGW") if Gem.win_platform?
3839
args = [
3940
Arrow::ArrayDatum.new(build_timestamp_array(:milli, [1504953190854])),
4041
]

cpp/src/arrow/config.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,18 @@ RuntimeInfo GetRuntimeInfo() {
7878
return info;
7979
}
8080

81+
Status Initialize(const GlobalOptions& options) noexcept {
82+
if (options.timezone_db_path.has_value()) {
83+
#if !USE_OS_TZDB
84+
try {
85+
arrow_vendored::date::set_install(options.timezone_db_path.value());
86+
arrow_vendored::date::reload_tzdb();
87+
} catch (const std::runtime_error& e) {
88+
return Status::IOError(e.what());
89+
}
90+
#endif
91+
}
92+
return Status::OK();
93+
}
94+
8195
} // namespace arrow

cpp/src/arrow/config.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ const BuildInfo& GetBuildInfo();
7979
ARROW_EXPORT
8080
RuntimeInfo GetRuntimeInfo();
8181

82+
struct GlobalOptions {
83+
/// Path to text timezone database. This is only used on Windows MinGW
84+
/// builds where std::chrono timezone support is not available.
85+
std::optional<std::string> timezone_db_path;
86+
};
87+
88+
ARROW_EXPORT
89+
Status Initialize(const GlobalOptions& options) noexcept;
90+
8291
} // namespace arrow

cpp/src/arrow/util/chrono_internal.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@
3333
#include <string_view>
3434

3535
// Feature detection for C++20 chrono timezone support
36-
// We only enable for compilers with FULL support (not partial)
3736
// https://en.cppreference.com/w/cpp/compiler_support/20.html#cpp_lib_chrono_201907L
3837
//
38+
// On Windows with MSVC: std::chrono uses Windows' internal timezone database,
39+
// eliminating the need for users to install IANA tzdata separately.
40+
//
41+
// On Windows with MinGW/GCC: libstdc++ reads tzdata files via TZDIR env var.
42+
// The tzdata files must be provided (e.g., via the tzdb R package).
43+
//
3944
// On non-Windows: GCC libstdc++ has a bug where DST state is incorrectly reset when
4045
// a timezone transitions between rule sets (e.g., Australia/Broken_Hill around
4146
// 2000-02-29). Until this is fixed, we use the vendored date.h library.
4247
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110
43-
//
44-
// On Windows: Use std::chrono which accesses Windows' internal timezone database,
45-
// eliminating the need for users to install IANA tzdata separately. We tolerate
46-
// the GCC bug here since Windows users are less likely to be using GCC.
4748

4849
#if defined(_WIN32) && defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
4950
# define ARROW_USE_STD_CHRONO 1

r/R/arrow-package.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ s3_finalizer <- new.env(parent = emptyenv())
152152
# Disable multithreading on Windows
153153
# See https://issues.apache.org/jira/browse/ARROW-8379
154154
options(arrow.use_threads = FALSE)
155+
156+
# Try to set timezone database for MinGW builds
157+
configure_tzdb()
155158
}
156159

157160
# Set interrupt handlers
@@ -168,6 +171,22 @@ s3_finalizer <- new.env(parent = emptyenv())
168171
invisible()
169172
}
170173

174+
configure_tzdb <- function() {
175+
# This is needed on Windows MinGW builds where std::chrono timezone support
176+
# is not available (older GCC versions). The tzdb R package provides the
177+
# IANA timezone database.
178+
if (requireNamespace("tzdb", quietly = TRUE)) {
179+
tzdb::tzdb_initialize()
180+
set_timezone_database(tzdb::tzdb_path("text"))
181+
} else {
182+
msg <- paste(
183+
"The tzdb package is not installed.",
184+
"Timezones will not be available to Arrow compute functions."
185+
)
186+
packageStartupMessage(msg)
187+
}
188+
}
189+
171190
.onAttach <- function(libname, pkgname) {
172191
# Just to be extra safe, let's wrap this in a try();
173192
# we don't want a failed startup message to prevent the package from loading

r/R/arrowExports.R

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/src/arrowExports.cpp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/src/config.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "./arrow_types.h"
1919

20+
#include <optional>
21+
2022
#include <arrow/config.h>
2123

2224
// [[arrow::export]]
@@ -31,3 +33,15 @@ std::vector<std::string> runtime_info() {
3133
auto info = arrow::GetRuntimeInfo();
3234
return {info.simd_level, info.detected_simd_level};
3335
}
36+
37+
// [[arrow::export]]
38+
void set_timezone_database(cpp11::strings path) {
39+
auto paths = cpp11::as_cpp<std::vector<std::string>>(path);
40+
if (path.size() != 1) {
41+
cpp11::stop("Must provide a single path to the timezone database.");
42+
}
43+
44+
arrow::GlobalOptions options;
45+
options.timezone_db_path = std::make_optional(paths[0]);
46+
arrow::StopIfNotOk(arrow::Initialize(options));
47+
}

0 commit comments

Comments
 (0)