Skip to content

Commit 29a37f3

Browse files
committed
apacheGH-51215: [C++] Migrate datetime consumers to the chrono shim
Route calendar, parsing, formatting, timezone configuration, and Gandiva callers 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. Avoid compiling the vendored timezone implementation for standard-backend builds. Preserve the existing strftime-compatible syntax and locale behavior across both backends, including empty formats, literal braces, duration directives, timezone offsets, and abbreviations.
1 parent 397b3d0 commit 29a37f3

25 files changed

Lines changed: 240 additions & 157 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/api_scalar.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ class ARROW_EXPORT StrftimeOptions : public FunctionOptions {
360360

361361
static constexpr const char* kDefaultFormat = "%Y-%m-%dT%H:%M:%S";
362362

363-
/// The desired format string.
363+
/// The desired format string. An empty format produces the default local date and
364+
/// time representation.
364365
std::string format;
365366
/// The desired output locale string.
366367
std::string locale;

‎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/compute/kernels/scalar_temporal_test.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,20 @@ TEST_F(ScalarTemporalTest, TestAssumeTimezoneNonexistent) {
20032003
&options_earliest);
20042004
}
20052005

2006+
TEST_F(ScalarTemporalTest, StrftimeFormatSyntax) {
2007+
const auto type = timestamp(TimeUnit::MILLI, "UTC");
2008+
const char* input = R"(["1970-01-01T00:00:00.123", null])";
2009+
for (const auto& [format, expected] :
2010+
{std::pair{"", R"(["1970-01-01 00:00:00.123", null])"},
2011+
std::pair{"literal {%Y}", R"(["literal {1970}", null])"},
2012+
std::pair{"%Y}", R"(["1970}", null])"},
2013+
std::pair{"%Q %q %J %z %Z", R"(["123 ms %J +0000 UTC", null])"}}) {
2014+
SCOPED_TRACE(format);
2015+
const auto options = StrftimeOptions(format);
2016+
CheckScalarUnary("strftime", type, input, utf8(), expected, &options);
2017+
}
2018+
}
2019+
20062020
TEST_F(ScalarTemporalTest, StrftimeOffsetTimezone) {
20072021
auto options_ymdhms = StrftimeOptions("%Y-%m-%dT%H:%M:%S");
20082022

‎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
#

0 commit comments

Comments
 (0)