GH-51267: [C++] Add std::chrono opt-in with toolchain feature detection #51315
When ARROW_GANDIVA is enabled, gandiva/cast_time.cc and gdv_function_stubs.cc still call arrow_vendored::date::locate_zone/zoned_time. Those definitions come from vendored/datetime.cpp, and Gandiva links arrow_shared/arrow_static; removing this translation unit here leaves undefined symbols (or a runtime load failure). Keep the TU for Gandiva or migrate those call sites to the standard backend.
This new error path is also reached by the R package's startup configure_tzdb(), which unconditionally calls set_timezone_database(tzdb::tzdb_path("text")) when the tzdb package is installed. In a std::chrono build the OS tzdb is the intended backend, so R will report a false startup failure that timezones are unavailable; make the R initialization backend-aware or otherwise avoid passing a vendored database path for this backend.
When Arrow is added as a CMake subproject, ARROW_DEFINE_OPTIONS defaults to OFF, so ARROW_USE_STD_CHRONO is undefined and CheckStdChrono.cmake intentionally skips resolution. However, #cmakedefine01 still generates #define ARROW_USE_STD_CHRONO 0, preventing chrono_internal.h from using its existing Windows feature-detection fallback and changing those builds from the previous std::chrono backend to vendored datetime. Preserve the undefined state for an unset option (while retaining 0 for an explicit OFF) or resolve the subproject default explicitly.
🟡 Changes recommended
Unresolved Gandiva linkage, R initialization, and CMake subproject configuration issues block approval.
Get a fresh assessment by requesting another Copilot review.
<details>
<summary>Pull request overview</summary>
Adds opt-in std::chrono backend selection with CMake/Meson toolchain detection while retaining the vendored fallback.
Changes:
AUTO/enabled/disabled build options and feature probes.</details>
<details>
<summary>File summaries</summary>
| File | Summary |
|---|---|
| cpp/src/arrow/util/meson.build | Configures the selected chrono backend for Meson. |
| cpp/src/arrow/util/config.h.cmake | Generates the backend macro. Moderate (3 votes): unset CMake subproject options may change the previous Windows fallback behavior. |
| cpp/src/arrow/util/chrono_internal.h | Selects the configured chrono implementation. |
| cpp/src/arrow/public_api_test.cc | Adjusts timezone configuration coverage. |
| cpp/src/arrow/meson.build | Conditionally includes vendored datetime sources. |
| cpp/src/arrow/config.cc | Updates timezone initialization. Moderate (2 votes): R startup may falsely fail when passing a vendored database path to the standard backend. |
| cpp/src/arrow/CMakeLists.txt | Conditionally excludes vendored datetime sources. Critical (3 votes): Gandiva still references vendored symbols, risking link or runtime failures. |
| cpp/meson.options | Adds the Meson chrono feature option. |
| cpp/meson.build | Detects and resolves Meson chrono support. |
| cpp/CMakeLists.txt | Loads chrono feature detection. |
| cpp/cmake_modules/DefineOptions.cmake | Defines the CMake chrono option. |
| cpp/cmake_modules/CheckStdChrono.cmake | Resolves and validates the CMake backend selection. |
</details>
<details>
<summary>Review details</summary>
</details>
---
💡 <a href="/apache/arrow/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
Fixed at 7b99c74fe: the TU is now only removed when Gandiva is not built (if(ARROW_USE_STD_CHRONO AND NOT ARROW_GANDIVA)). Gandiva's cast_time.cc / gdv_function_stubs.cc keep their locate_zone/zoned_time definitions, so ARROW_USE_STD_CHRONO=ON + ARROW_GANDIVA=ON links; migrating those call sites to the standard backend can come later under GH-51267.
Fixed at 7b99c74fe on the R side: runtime_info() now reports using_os_timezone_db (third element, "true"/"false"), and configure_tzdb() skips set_timezone_database(tzdb::tzdb_path("text")) entirely when the runtime reads the OS timezone database — exactly the std::chrono case here. So R no longer surfaces the false "timezones will not be available" startup warning on this backend; the tzdb package path is only used when Arrow actually consumes a vendored, non-OS database (preserving the GH-49866 MinGW behavior). arrow_info() exposes the flag as runtime_info$using_os_timezone_db too.
Fixed at 7b99c74fe: config.h.cmake now emits @ARROW_USE_STD_CHRONO_DEFINITION@, which the build system resolves as — #define ARROW_USE_STD_CHRONO 1 / 0 when the option is defined (explicit ON/OFF, AUTO-resolved), and / #undef ARROW_USE_STD_CHRONO / when it is unset (subproject with ARROW_DEFINE_OPTIONS=OFF). The undefined state restores chrono_internal.h's platform fallback for those builds, while an explicit OFF keeps 0 so the fallback cannot re-enable std::chrono against the user's choice. The Meson side sets the same definition key from its resolved option (Meson options always resolve, so there is no unset case there).
This probe only checks the feature-test macro and links an empty main, but the selected branch unconditionally includes <format>, calls std::chrono::locate_zone, and uses std::vformat. A standard library can advertise __cpp_lib_chrono >= 201907L while one of those headers/APIs is unavailable, so ARROW_USE_STD_CHRONO=ON can pass configure and then fail to build. Make the CMake and Meson probes compile/link the APIs actually used by chrono_internal.h.
ARROW_USE_STD_CHRONO does not mean every datetime consumer uses the standard backend: the build deliberately keeps vendored/datetime.cpp when Gandiva is enabled, and Gandiva still calls arrow_vendored::date::locate_zone/zoned_time. Rejecting timezone_db_path here prevents configuring the vendored database that Gandiva needs, so std-chrono + Gandiva can fail at runtime. Keep the vendored configuration path for that combination (using a generated Gandiva/backend flag) instead of treating the whole library as OS-tzdb-only.
In a CMake subproject with ARROW_DEFINE_OPTIONS=OFF, the generated config intentionally leaves ARROW_USE_STD_CHRONO undefined, so chrono_internal.h still selects std::chrono on Windows via its platform fallback. config.cc does not include that header, so this preprocessor branch is skipped and such a binary reports using_os_timezone_db=false (and continues exposing the vendored timezone configuration) even though its chrono backend uses the OS database. Please use a shared/resolved backend predicate here so RuntimeInfo and Initialize match the header-level fallback as well.
Because this check is nested under requireNamespace(), a std::chrono build with no tzdb package still falls into the existing else and emits “Timezones will not be available”, even though using_os_timezone_db is true. Move the backend check before package detection (or guard the else) so the OS-backend path is silent regardless of whether tzdb is installed.
🟡 Changes recommended
Unresolved probe, backend-consistency, Gandiva timezone, and R initialization findings remain.
Get a fresh assessment by requesting another Copilot review.
<details>
<summary>Review details</summary>
</details>
CI triage on the failing runs — one real bug (fixed), the rest infrastructure:
Fixed at f96c81bcf — the AMD64 Ubuntu 24.04 R 4.6 / Windows 2022 Python 3.13 / other from-source pyarrow failures. Root cause: the ARROW_USE_STD_CHRONO description in DefineOptions.cmake used an escaped backslash (\\) before the newline instead of a line-continuation backslash, so the newline stayed inside the quoted description and leaked into the generated ArrowOptions.cmake as a bare statement line (AUTO keeps the current platform default (GH-51267)). Every consumer that includes the installed file via find_package(Arrow) then failed with Parse error. Expected "(", got identifier with text "keeps" — that's the ERROR: Failed building wheel for pyarrow inside the R docker build and the Windows Python build. Reproduced locally with the exact generated file, fixed, and verified: the regenerated ArrowOptions.cmake parses clean via find_package (parsed OK).
Not from this PR:
macOS 15-intel C++, Windows MinGW CLANG64/MINGW64 C++ (and likely the remaining MinIO-dependent jobs): dl.min.io returns 410: Gone while downloading the MinIO release archive — upstream infrastructure.Windows 2022 MATLAB: workflow template error — hashFiles('cpp/, matlab/') failed to hash files on the runner; workflow-side.AMD64 Conda C++ AVX2: arrow-filesystem-test fails with "Attempt to initialize S3 after it has been finalized" — pre-existing flake, this same job was the only failure on my merged #51203 as well.Because this check is nested under requireNamespace("tzdb"), a std::chrono build with no R tzdb package still reaches the else branch below and emits the false “Timezones will not be available” startup message. Check using_os_timezone_db before testing for the optional package so OS-backed builds return without requiring or warning about tzdb.
🟡 Changes recommended
Unresolved backend detection, Gandiva configuration, runtime selection, and R/Python compatibility issues remain.
Get a fresh assessment by requesting another Copilot review.
<details>
<summary>Review details</summary>
Suppressed comments (6)
cpp/cmake_modules/CheckStdChrono.cmake:49
This probe only includes <chrono> and checks __cpp_lib_chrono, but selecting this backend makes chrono_internal.h unconditionally include <format> and use std::vformat. A standard library can advertise chrono timezone support without implementing the separate format facility, so ARROW_USE_STD_CHRONO=ON can pass configure and then fail compiling Arrow. Probe <format>/__cpp_lib_format and the required formatting expression here as part of the opt-in check.#include <chrono>
#if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L
# error \"C++20 chrono timezone support (__cpp_lib_chrono >= 201907L) is unavailable\"
#endif
cpp/meson.build:125
The Meson probe has the same gap as the CMake probe: it accepts a toolchain based only on __cpp_lib_chrono, while the selected backend includes <format> and calls std::vformat. This can make use_std_chrono=enabled configure successfully and then fail during the Arrow build when the separate C++20 format facility is unavailable. Check <format>/__cpp_lib_format and exercise the formatting expression here too.
#include <chrono>
#if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L
#error "C++20 chrono timezone support (__cpp_lib_chrono >= 201907L) is unavailable"
#endif
int main() { return 0; }
cpp/src/arrow/CMakeLists.txt:574
This intentionally retains
vendored/datetime.cpp for Gandiva, creating a mixed-backend build. On Windows that TU uses USE_OS_TZDB=0, but config.cc now treats every ARROW_USE_STD_CHRONO=1 build as OS-backed and rejects timezone_db_path, leaving Gandiva without a way to load its required timezone database. Either reject this combination at configure time or preserve the vendored database configuration for Gandiva.if(ARROW_USE_STD_CHRONO AND NOT ARROW_GANDIVA)
# GH-51267: standard-backend binaries use C++20 std::chrono and do not bundle
# the vendored datetime implementation. The remaining direct users of
# arrow/vendored/datetime.h (formatting, parsing, pretty printing) only rely
# on its header-only calendar types — except Gandiva, whose cast_time.cc and
cpp/src/arrow/config.cc:109
When
ARROW_GANDIVA=ON, the source-list change intentionally retains vendored/datetime.cpp because Gandiva still calls arrow_vendored::date::locate_zone directly. On Windows that vendored implementation is not OS-backed and needs set_install/reload_tzdb to point at its database; this unconditional std::chrono rejection prevents Initialize() from configuring it, so std+Gandiva timezone calls fail unless the default download location is already populated. Keep a separate vendored-database configuration path for Gandiva or migrate those callers before rejecting the path.#if defined(ARROW_USE_STD_CHRONO) && ARROW_USE_STD_CHRONO
return Status::Invalid(
"Arrow was built with C++20 std::chrono and uses the OS timezone database, "
"so a downloaded database cannot be provided at runtime.");
cpp/src/arrow/config.cc:90
When
ARROW_DEFINE_OPTIONS=OFF, the generated header intentionally leaves ARROW_USE_STD_CHRONO undefined, but chrono_internal.h still derives it as 1 on a supported Windows toolchain. config.cc does not use that same fallback selector, so this branch reports the build as vendored and the following initialization logic can configure an unused vendored database while compute uses std::chrono. Please centralize or reuse the backend selection so runtime info and timezone configuration match in CMake subprojects.#if defined(ARROW_USE_STD_CHRONO) && ARROW_USE_STD_CHRONO
// GH-51267: std::chrono builds always use the OS timezone database.
info.using_os_timezone_db = true;
info.timezone_db_path = std::optional<std::string>();
#else
cpp/src/arrow/config.cc:109
On non-Windows std::chrono opt-in builds,
pa.set_timezone_db_path() now raises the new C++20-specific message, while python/pyarrow/tests/test_misc.py:140-149 still matches only Arrow was set to use OS timezone database at compile time. The existing Python test will fail for this supported configuration; update it to accept the new message (or preserve the previous wording).#if defined(ARROW_USE_STD_CHRONO) && ARROW_USE_STD_CHRONO
return Status::Invalid(
"Arrow was built with C++20 std::chrono and uses the OS timezone database, "
"so a downloaded database cannot be provided at runtime.");
</details>
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Found and fixed a real one from the latest run's CI triage: the Windows 2022 AVX2 C++ failure (arrow-substrait-substrait-test with Cannot locate or parse timezone 'UTC': Timezone database not found at "C:\Users\runneradmin\Downloads\tzdata") was caused by this PR's configure probe. It passed /std:c++20 through CMAKE_REQUIRED_FLAGS on top of the project-wide CMAKE_CXX_STANDARD=20 that try_compile already applies, and under CMake 4.1 + MSVC 19.44 the probe failed spuriously — so AUTO silently downgraded the build to the vendored backend, whose tzdb lookups then fail at runtime on Windows (no downloaded database). Fixed at c1c045ad5: the probe now compiles with the project standard alone and prints its compiler output on failure, so any future probe regression is diagnosable from CI directly. Verified the new mechanics locally (probe compiles with a single -std=c++20, failure output surfaced, explicit ON on an unsupported toolchain still fails the configure with the intended message).
The remaining failures in that run are all the MinIO 410: Gone outage (Install MinIO step — macOS/ARM64/MinGW/R jobs) and the pre-existing Conda S3 after finalized flake; Windows R release is MinIO too, and Windows 2022 MATLAB is the workflow-template hashFiles error.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Ready for another look — pushed fixes for the 4 open review findings and rebased onto latest main (b274238).
Fixes (commit 3f41597):
1. Probe gap: CMake + Meson probes now compile <format> (vformat_to/make_format_args) and std::chrono::locate_zone, not just __cpp_lib_chrono (catches e.g. GCC 12, which advertises the macro but has no <format>).
2. R startup: configure_tzdb() checks using_os_timezone_db first and skips entirely — no more false "Timezones will not be available" when the tzdb package is missing on OS-db builds.
3. Gandiva mix: new ARROW_HAVE_VENDORED_DATETIME (CMake + Meson) records whether datetime.cpp is linked; Initialize() honors timezone_db_path for std+Gandiva builds (which still carry the vendored copy) and keeps rejecting it for pure-std builds.
4. Subproject: config.cc now includes chrono_internal.h, so ARROW_DEFINE_OPTIONS=OFF builds use the same backend predicate as the header (no more using_os_timezone_db=false on Windows-std); the SetTimezoneConfig skip follows it too.
Also fixed the cmake-format lint failure on CheckStdChrono.cmake (try_compile layout).
Reds triage on the previous head (c1c045a) — all CI-side except one to watch:
Install/Download MinIO failures: macOS 15-intel C++, ARM64 macOS 14 C++, MinGW MINGW64 + CLANG64 C++, macOS Python, Windows R release — MinIO infra, unrelated to this PR.Post Cache ccache — infra flake.Execute Docker Build — looks infra-related; will confirm on the new head.Test step — please flag if still red on the new head and I'll dig into the log.pre-commit: only cmake-format on this PR's own file, fixed in this push.New CI is running on this head; will re-triage anything still red.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Fixed and verified against this head: the probe now compiles and links the APIs actually used — <format>, std::chrono::locate_zone, std::vformat_to with make_format_args — and additionally requires __cpp_lib_format (cpp/cmake_modules/CheckStdChrono.cmake:42-71). The same source is mirrored in cpp/meson.build:120-153, so Meson no longer trusts the feature-test macro alone either.
Addressed: Initialize() no longer keys on ARROW_USE_STD_CHRONO alone but on ARROW_HAVE_VENDORED_DATETIME (cpp/src/arrow/config.cc:112-133). std::chrono plus Gandiva still bundles the vendored timezone code and takes the set_install/reload_tzdb branch; only pure std::chrono builds, which carry no vendored code, reject timezone_db_path.
Addressed: cpp/src/arrow/config.cc now includes arrow/util/chrono_internal.h (lines 22-26), so the CMake-subproject case (ARROW_DEFINE_OPTIONS=OFF, macro undefined) resolves through the same header-level backend predicate that RuntimeInfo() and Initialize() use, instead of drifting to the vendored reading.
Addressed: configure_tzdb() returns before touching the optional package when runtime_info() reports the OS database (r/R/arrow-package.R:181-188), so an OS-backed build without tzdb installed no longer reaches the warning branch. The check now precedes requireNamespace as requested.
Same fix as the earlier thread on this file: the backend check at r/R/arrow-package.R:181-188 now precedes requireNamespace("tzdb"), so OS-backed builds return early and never emit that startup message. Resolving as a duplicate of that thread.
CI triage for this head, so the two non-green checks aren't read as coming from this diff: AMD64 Windows 2022 MATLAB fails with .github/workflows/matlab.yml (Line: 168, Col: 16): hashFiles('cpp/, matlab/') failed. Fail to hash files under directory 'D:\a\arrow\arrow'. That line is unchanged on main, and this PR touches zero .github/ files. AMD64 macOS 15-intel C++ was cancelled after hitting the 1 h 15 m job cap. Everything else on the head is green (41 success, 16 skipped), and all 8 review threads are now answered and resolved.
:warning: GitHub issue #51267 has been automatically assigned in GitHub to PR creator.
Adarsh-Me
Rationale for this change
GH-51267 tracks removing the vendored datetime fallback once all supported toolchains provide working C++20 chrono timezone support. The backend selection is currently hardcoded in
chrono_internal.h(Windows-only) with no build-level switch, and the vendored tz implementation is compiled into every binary — including standard-backend ones.What changes are included in this PR?
ARROW_USE_STD_CHRONOCMake option (AUTO/ON/OFF, defaultAUTO) resolved by a toolchain feature probe (__cpp_lib_chrono >= 201907L) incpp/cmake_modules/CheckStdChrono.cmake; new matchinguse_std_chronoMeson feature option with the same semantics.AUTOpreserves the current platform default (std::chrono on Windows toolchains with timezone support, vendored fallback elsewhere) until the minimum-toolchain prerequisites in GH-51267 are met. Explicit opt-in fails loudly at configure time on unsupported toolchains.arrow/util/config.hand honored bychrono_internal.h; standard-backend binaries no longer buildvendored/datetime.cpp, andconfig.cc/ theSetTimezoneConfigtest honor the selected backend (OS tzdb, no vendoredset_install/reload_tzdb).Are these changes tested?
AUTO→OFFon Linux/GCC 11, passesOFFthrough, fails configure onONwithout toolchain support and on invalid values; verifiedconfig.h.cmakerendersARROW_USE_STD_CHRONO 0/1via realconfigure_file.auto/disabledkeep the fallback and render0, andenablederrors out on an unsupported toolchain.vendored/datetime.cppplus a functional test against the modifiedchrono_internal.hfallback path — calendar round-trip, floor/ceil/trunc,format,make_time,OffsetZone, and a livelocate_zone("America/New_York")lookup against system zoneinfo all pass; also confirmed the header selects the std branch whenARROW_USE_STD_CHRONO=1.ctestmatrix (notably Windows/MSVC and opt-in toolchains) left to CI: this environment has no CMake project build deps and GCC 11 lacks<format>/chrono-tz.Are there any user-facing changes?
New opt-in build options only; default builds behave exactly as before.
Closes #51267
Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by:
Summary of AI usage: both the diff and this description were produced by an AI
coding agent working on a human contributor's machine under that contributor's
direction. The agent wrote the CMake option plumbing, the Meson equivalent, the
config.h/chrono_internal.hwiring and the description above, and it alsoexecuted every verification reported under "Are these changes tested?" (CMake
3.31.6
configure_filerenders, Meson 1.7.2 resolution, g++ 11 compile and runof
vendored/datetime.cppagainst the modified header). No human re-ran thosecommands before submission, and no copyrighted material was copied into the
diff. Full toolchain inventory of that host is in the "Are these changes
tested?" section, which is why the ctest matrix is delegated to CI.