Skip to content

Commit d22fc96

Browse files
committed
apacheGH-51215: [C++] Migrate remaining datetime consumers to the chrono shim
Route core calendar, parsing, formatting, and Gandiva callers through arrow::internal::chrono. Preserve backend selection and the vendored fallback, with a shared configuration header guarding the timezone implementation.
1 parent f95b001 commit d22fc96

22 files changed

Lines changed: 287 additions & 148 deletions

‎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: 6 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
#
@@ -52,6 +57,7 @@ add_arrow_test(utility-test
5257
byte_stream_split_test.cc
5358
cache_test.cc
5459
checked_cast_test.cc
60+
chrono_test.cc
5561
compression_test.cc
5662
decimal_test.cc
5763
float16_test.cc
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// Keep the non-Windows fallback for supported libstdc++ versions without the
30+
// complete fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110.
31+
#if defined(_WIN32) && defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
32+
# define ARROW_USE_STD_CHRONO 1
33+
#else
34+
# define ARROW_USE_STD_CHRONO 0
35+
#endif
36+
37+
// Only the vendored Windows text database supports setting its path via Arrow.
38+
// The non-Windows vendored backend uses USE_OS_TZDB (see datetime/visibility.h).
39+
#if ARROW_USE_STD_CHRONO || !defined(_WIN32)
40+
# define ARROW_CHRONO_USE_OS_TZDB 1
41+
#else
42+
# define ARROW_CHRONO_USE_OS_TZDB 0
43+
#endif

‎cpp/src/arrow/util/chrono_internal.h‎

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,18 @@
2121
/// \brief Abstraction layer for C++20 chrono calendar/timezone APIs
2222
///
2323
/// This header provides a unified interface for chrono calendar and timezone
24-
/// functionality. On compilers with full C++20 chrono support, it uses
25-
/// std::chrono. On other compilers, it falls back to the vendored Howard Hinnant
26-
/// date library.
24+
/// functionality. It uses std::chrono on supported Windows standard libraries
25+
/// and otherwise falls back to the vendored Howard Hinnant date library.
26+
/// See chrono_config_internal.h for backend selection.
2727
///
28-
/// The main benefit is on Windows where std::chrono uses the system timezone
29-
/// database, eliminating the need for users to install IANA tzdata separately.
28+
/// On Windows with MSVC, std::chrono uses the system timezone database,
29+
/// eliminating the need for users to install IANA tzdata separately.
3030

3131
#include <chrono>
3232
#include <string>
3333
#include <string_view>
3434

35-
// Feature detection for C++20 chrono timezone support
36-
// https://en.cppreference.com/w/cpp/compiler_support/20.html#cpp_lib_chrono_201907L
37-
//
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-
// Set TZDIR=/usr/share/zoneinfo to use the system tzdata.
43-
//
44-
// On non-Windows: GCC libstdc++ has a bug where DST state is incorrectly reset when
45-
// a timezone transitions between rule sets (e.g., Australia/Broken_Hill around
46-
// 2000-02-29). Until this is fixed, we use the vendored date.h library.
47-
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110
48-
49-
// Use std::chrono on Windows when C++20 chrono timezone support is available.
50-
// The __cpp_lib_chrono >= 201907L feature test macro indicates full support:
51-
// - MSVC: Uses Windows' internal timezone database (no IANA tzdata needed)
52-
// - GCC/libstdc++: Requires TZDIR environment variable to locate tzdata
53-
// - Clang/libc++: Does not define 201907L (no timezone support), so falls back
54-
//
55-
// On non-Windows, we use the vendored date library due to a GCC libstdc++ bug
56-
// where DST state is incorrectly reset during timezone rule transitions.
57-
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110
58-
#if defined(_WIN32) && defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
59-
# define ARROW_USE_STD_CHRONO 1
60-
#else
61-
# define ARROW_USE_STD_CHRONO 0
62-
#endif
35+
#include "arrow/util/chrono_config_internal.h"
6336

6437
#if ARROW_USE_STD_CHRONO
6538
// Use C++20 standard library chrono
@@ -162,11 +135,11 @@ std::basic_ostream<CharT, Traits>& to_stream(
162135
return os;
163136
}
164137

165-
// Format a duration using strftime-like format specifiers
166-
// Converts "%H%M" style to C++20's "{:%H%M}" style and uses std::vformat
167-
template <typename Duration>
168-
std::string format(const char* fmt, const Duration& d) {
169-
return std::vformat(std::string("{:") + fmt + "}", std::make_format_args(d));
138+
// Format a duration or time point using strftime-like format specifiers.
139+
// Converts "%H%M" style to C++20's "{:%H%M}" style and uses std::vformat.
140+
template <typename Temporal>
141+
std::string format(const char* fmt, const Temporal& value) {
142+
return std::vformat(std::string("{:") + fmt + "}", std::make_format_args(value));
170143
}
171144

172145
inline constexpr std::chrono::month jan = std::chrono::January;
@@ -248,6 +221,11 @@ inline const time_zone* locate_zone(std::string_view tz_name) {
248221

249222
inline const time_zone* current_zone() { return vendored::current_zone(); }
250223

224+
# if !ARROW_CHRONO_USE_OS_TZDB
225+
using vendored::reload_tzdb;
226+
using vendored::set_install;
227+
# endif
228+
251229
// Formatting support
252230
using vendored::format;
253231

@@ -270,4 +248,11 @@ inline constexpr vendored::last_spec last = vendored::last;
270248

271249
#endif // ARROW_USE_STD_CHRONO
272250

251+
// date::make_time has no std::chrono equivalent, but hh_mm_ss is shared by both
252+
// backends. Keep duration deduction here for callers formatting time of day.
253+
template <typename Rep, typename Period>
254+
constexpr auto make_time(std::chrono::duration<Rep, Period> value) {
255+
return hh_mm_ss<std::chrono::duration<Rep, Period>>{value};
256+
}
257+
273258
} // namespace arrow::internal::chrono

0 commit comments

Comments
 (0)