Skip to content

Commit 6998bb4

Browse files
committed
apacheGH-51215: [C++] Migrate datetime consumers to the chrono shim
Route datetime consumers in Arrow and Gandiva through arrow::internal::chrono. Prefer std::chrono when the standard library has reliable C++20 timezone support, while retaining the vendored fallback and allowing an explicit backend override. Compile the vendored timezone sources, including the Apple helper, only when the fallback backend is selected.
1 parent 382ffd9 commit 6998bb4

23 files changed

Lines changed: 227 additions & 143 deletions

‎cpp/src/arrow/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ set(ARROW_VENDORED_SRCS
538538
vendored/uriparser/UriResolve.c
539539
vendored/uriparser/UriShorten.c)
540540
if(APPLE)
541-
list(APPEND ARROW_VENDORED_SRCS vendored/datetime/ios.mm)
541+
# This wrapper excludes the iOS timezone implementation from standard-backend builds.
542+
list(APPEND ARROW_VENDORED_SRCS vendored/datetime_ios.mm)
542543
endif()
543544
set_source_files_properties(vendored/datetime.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION
544545
ON)

‎cpp/src/arrow/array/diff.cc‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,19 @@
4343
#include "arrow/type_traits.h"
4444
#include "arrow/util/bit_util.h"
4545
#include "arrow/util/checked_cast.h"
46+
#include "arrow/util/chrono_internal.h"
4647
#include "arrow/util/float16.h"
4748
#include "arrow/util/logging_internal.h"
4849
#include "arrow/util/range.h"
4950
#include "arrow/util/ree_util.h"
5051
#include "arrow/util/string.h"
5152
#include "arrow/util/unreachable.h"
52-
#include "arrow/vendored/datetime.h"
5353
#include "arrow/visit_type_inline.h"
5454

5555
namespace arrow {
5656

57+
namespace chrono = internal::chrono;
58+
5759
using internal::checked_cast;
5860
using internal::checked_pointer_cast;
5961
using internal::MakeLazyRange;
@@ -631,14 +633,13 @@ class MakeFormatterImpl {
631633
template <typename T>
632634
enable_if_date<T, Status> Visit(const T&) {
633635
using unit = typename std::conditional<std::is_same<T, Date32Type>::value,
634-
arrow_vendored::date::days,
635-
std::chrono::milliseconds>::type;
636+
chrono::days, std::chrono::milliseconds>::type;
636637

637-
static arrow_vendored::date::sys_days epoch{arrow_vendored::date::jan / 1 / 1970};
638+
static chrono::sys_days epoch{chrono::jan / 1 / 1970};
638639

639640
impl_ = [](const Array& array, int64_t index, std::ostream* os) {
640641
unit value(checked_cast<const NumericArray<T>&>(array).Value(index));
641-
*os << arrow_vendored::date::format("%F", value + epoch);
642+
*os << chrono::format("%F", value + epoch);
642643
};
643644
return Status::OK();
644645
}
@@ -854,42 +855,41 @@ class MakeFormatterImpl {
854855
auto value = checked_cast<const NumericArray<T>&>(array).Value(index);
855856
// Using unqualified `format` directly would produce ambiguous
856857
// lookup because of `std::format` (ARROW-15520).
857-
namespace avd = arrow_vendored::date;
858858
using std::chrono::nanoseconds;
859859
using std::chrono::microseconds;
860860
using std::chrono::milliseconds;
861861
using std::chrono::seconds;
862862
if (AddEpoch) {
863-
static avd::sys_days epoch{avd::jan / 1 / 1970};
863+
static chrono::sys_days epoch{chrono::jan / 1 / 1970};
864864

865865
switch (unit) {
866866
case TimeUnit::NANO:
867-
*os << avd::format(fmt, static_cast<nanoseconds>(value) + epoch);
867+
*os << chrono::format(fmt, static_cast<nanoseconds>(value) + epoch);
868868
break;
869869
case TimeUnit::MICRO:
870-
*os << avd::format(fmt, static_cast<microseconds>(value) + epoch);
870+
*os << chrono::format(fmt, static_cast<microseconds>(value) + epoch);
871871
break;
872872
case TimeUnit::MILLI:
873-
*os << avd::format(fmt, static_cast<milliseconds>(value) + epoch);
873+
*os << chrono::format(fmt, static_cast<milliseconds>(value) + epoch);
874874
break;
875875
case TimeUnit::SECOND:
876-
*os << avd::format(fmt, static_cast<seconds>(value) + epoch);
876+
*os << chrono::format(fmt, static_cast<seconds>(value) + epoch);
877877
break;
878878
}
879879
return;
880880
}
881881
switch (unit) {
882882
case TimeUnit::NANO:
883-
*os << avd::format(fmt, static_cast<nanoseconds>(value));
883+
*os << chrono::format(fmt, static_cast<nanoseconds>(value));
884884
break;
885885
case TimeUnit::MICRO:
886-
*os << avd::format(fmt, static_cast<microseconds>(value));
886+
*os << chrono::format(fmt, static_cast<microseconds>(value));
887887
break;
888888
case TimeUnit::MILLI:
889-
*os << avd::format(fmt, static_cast<milliseconds>(value));
889+
*os << chrono::format(fmt, static_cast<milliseconds>(value));
890890
break;
891891
case TimeUnit::SECOND:
892-
*os << avd::format(fmt, static_cast<seconds>(value));
892+
*os << chrono::format(fmt, static_cast<seconds>(value));
893893
break;
894894
}
895895
};

‎cpp/src/arrow/compute/kernels/scalar_cast_temporal.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ struct ParseDate {
462462
using value_type = typename DateType::c_type;
463463

464464
using duration_type =
465-
typename std::conditional<std::is_same<DateType, Date32Type>::value,
466-
arrow_vendored::date::days,
465+
typename std::conditional<std::is_same<DateType, Date32Type>::value, chrono::days,
467466
std::chrono::milliseconds>::type;
468467

469468
template <typename OutValue, typename Arg0Value>

‎cpp/src/arrow/config.cc‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
#include <cstdint>
2121

22+
#include "arrow/util/chrono_internal.h"
2223
#include "arrow/util/config.h"
2324
#include "arrow/util/config_internal.h"
2425
#include "arrow/util/cpu_info.h"
25-
#include "arrow/vendored/datetime.h"
2626

2727
namespace arrow {
2828

2929
using internal::CpuInfo;
30+
namespace chrono = internal::chrono;
3031

3132
namespace {
3233

@@ -77,8 +78,8 @@ RuntimeInfo GetRuntimeInfo() {
7778
MakeSimdLevelString([&](int64_t flags) { return cpu_info->IsSupported(flags); });
7879
info.detected_simd_level =
7980
MakeSimdLevelString([&](int64_t flags) { return cpu_info->IsDetected(flags); });
80-
info.using_os_timezone_db = USE_OS_TZDB;
81-
#if !USE_OS_TZDB
81+
info.using_os_timezone_db = ARROW_CHRONO_USE_OS_TZDB;
82+
#if !ARROW_CHRONO_USE_OS_TZDB
8283
info.timezone_db_path = timezone_db_path;
8384
#else
8485
info.timezone_db_path = std::optional<std::string>();
@@ -91,10 +92,10 @@ RuntimeInfo GetRuntimeInfo() {
9192
Status Initialize(const GlobalOptions& options) noexcept {
9293
ARROW_SUPPRESS_DEPRECATION_WARNING
9394
if (options.timezone_db_path.has_value()) {
94-
#if !USE_OS_TZDB
95+
#if !ARROW_CHRONO_USE_OS_TZDB
9596
try {
96-
arrow_vendored::date::set_install(options.timezone_db_path.value());
97-
arrow_vendored::date::reload_tzdb();
97+
chrono::set_install(options.timezone_db_path.value());
98+
chrono::reload_tzdb();
9899
} catch (const std::runtime_error& e) {
99100
return Status::IOError(e.what());
100101
}
@@ -103,7 +104,7 @@ Status Initialize(const GlobalOptions& options) noexcept {
103104
return Status::Invalid(
104105
"Arrow was set to use OS timezone database at compile time, "
105106
"so a downloaded database cannot be provided at runtime.");
106-
#endif // !USE_OS_TZDB
107+
#endif // !ARROW_CHRONO_USE_OS_TZDB
107108
}
108109
ARROW_UNSUPPRESS_DEPRECATION_WARNING
109110
return Status::OK();

‎cpp/src/arrow/config.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ struct RuntimeInfo {
6666
/// The SIMD level available on the OS and CPU
6767
std::string detected_simd_level;
6868

69-
/// Whether using the OS-based timezone database
70-
/// This is set at compile-time.
69+
/// Whether the timezone database is managed by the OS or standard library,
70+
/// rather than Arrow's configurable text database. This is set at compile-time.
7171
bool using_os_timezone_db;
7272

7373
/// The path to the timezone database; by default None.

‎cpp/src/arrow/pretty_print.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "arrow/util/int_util_overflow.h"
4343
#include "arrow/util/key_value_metadata.h"
4444
#include "arrow/util/string.h"
45-
#include "arrow/vendored/datetime.h"
4645
#include "arrow/visit_array_inline.h"
4746

4847
namespace arrow {

‎cpp/src/arrow/public_api_test.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ TEST(Misc, BuildInfo) {
125125
// TODO(GH-48593): Remove when libc++ supports std::chrono timezones.
126126
ARROW_SUPPRESS_DEPRECATION_WARNING
127127
TEST(Misc, SetTimezoneConfig) {
128+
ASSERT_OK(Initialize(GlobalOptions{}));
129+
if (GetRuntimeInfo().using_os_timezone_db) {
130+
ASSERT_FALSE(GetRuntimeInfo().timezone_db_path.has_value());
131+
// Standard-library backends must reject even an existing path rather than
132+
// silently configuring an unused vendored database.
133+
GlobalOptions options;
134+
options.timezone_db_path = ".";
135+
ASSERT_RAISES(Invalid, Initialize(options));
136+
ASSERT_FALSE(GetRuntimeInfo().timezone_db_path.has_value());
137+
EnvVarGuard tzdata("ARROW_TIMEZONE_DATABASE", ".");
138+
ASSERT_OK(InitTestTimezoneDatabase());
139+
return;
140+
}
128141
#ifndef _WIN32
129142
GTEST_SKIP() << "Can only set the Timezone database on Windows";
130143
#elif !defined(ARROW_FILESYSTEM)
@@ -164,6 +177,7 @@ TEST(Misc, SetTimezoneConfig) {
164177

165178
// Validate that tzdb is working
166179
ASSERT_OK(arrow::Initialize(options));
180+
ASSERT_EQ(GetRuntimeInfo().timezone_db_path, options.timezone_db_path);
167181
#endif
168182
}
169183
ARROW_UNSUPPRESS_DEPRECATION_WARNING

‎cpp/src/arrow/testing/util.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ std::optional<std::string> GetTestTimezoneDatabaseRoot() {
141141
// TODO(GH-48593): Remove when libc++ supports std::chrono timezones.
142142
ARROW_SUPPRESS_DEPRECATION_WARNING
143143
Status InitTestTimezoneDatabase() {
144+
if (GetRuntimeInfo().using_os_timezone_db) return Status::OK();
145+
144146
auto maybe_tzdata = GetTestTimezoneDatabaseRoot();
145147
// If missing, timezone database will default to %USERPROFILE%\Downloads\tzdata
146148
if (!maybe_tzdata.has_value()) return Status::OK();

‎cpp/src/arrow/util/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
# Headers: top level
2323
arrow_install_all_headers("arrow/util")
2424

25+
# The automatic rule excludes internal headers, but these are dependencies of
26+
# the installed formatting.h and value_parsing.h headers.
27+
install(FILES chrono_config_internal.h chrono_internal.h
28+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/arrow/util")
29+
2530
#
2631
# arrow_test_main
2732
#
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include <chrono>
21+
22+
// Share backend selection with the vendored implementation without including its
23+
// headers. datetime.h undefines macros needed when compiling the implementation.
24+
//
25+
// On Windows, MSVC's standard library uses the system timezone database, while
26+
// libstdc++ reads tzdata files (using TZDIR). Libraries without the C++20 timezone
27+
// APIs, including older libc++, still require the vendored date library.
28+
//
29+
// Use the standard backend by default. Builds may explicitly define
30+
// ARROW_USE_STD_CHRONO to 0 or 1 when they need to select a backend.
31+
//
32+
// Automatically disable the default for libraries without the C++20 timezone APIs.
33+
// On non-Windows, older libstdc++ versions also need the fallback because of
34+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110 (fully fixed in GCC 16.2).
35+
// Check library macros, not __GNUC__, so Clang using libstdc++ agrees with GCC.
36+
// The datestamp distinguishes 16.2 (2026-08-07) from 16.1 and early snapshots.
37+
// Keep the existing Windows backend selection unchanged.
38+
#ifndef ARROW_USE_STD_CHRONO
39+
# define ARROW_USE_STD_CHRONO 1
40+
# if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L
41+
# undef ARROW_USE_STD_CHRONO
42+
# define ARROW_USE_STD_CHRONO 0
43+
# elif !defined(_WIN32) && defined(__GLIBCXX__) && \
44+
(!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE < 16 || __GLIBCXX__ < 20260807)
45+
# undef ARROW_USE_STD_CHRONO
46+
# define ARROW_USE_STD_CHRONO 0
47+
# endif
48+
#endif
49+
50+
// Only the vendored Windows text database supports setting its path via Arrow.
51+
// The non-Windows vendored backend uses USE_OS_TZDB (see datetime/visibility.h).
52+
#if ARROW_USE_STD_CHRONO || !defined(_WIN32)
53+
# define ARROW_CHRONO_USE_OS_TZDB 1
54+
#else
55+
# define ARROW_CHRONO_USE_OS_TZDB 0
56+
#endif

0 commit comments

Comments
 (0)