Skip to content

Commit 499b3f9

Browse files
committed
apacheGH-51215: [C++] Use standard chrono on supported non-Windows libraries
Remove the Windows-only restriction while retaining the non-Windows fallback for libstdc++ versions without the complete GCC PR116110 fix.
1 parent d22fc96 commit 499b3f9

14 files changed

Lines changed: 87 additions & 146 deletions

File tree

‎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_temporal_test.cc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,19 @@ TEST_F(ScalarTemporalTest, TestAssumeTimezoneNonexistent) {
20132013
&options_earliest);
20142014
}
20152015

2016+
TEST_F(ScalarTemporalTest, StrftimeFormatSyntax) {
2017+
const auto type = timestamp(TimeUnit::SECOND, "UTC");
2018+
const char* input = R"(["1970-01-01T00:00:00", null])";
2019+
for (const auto& [format, expected] :
2020+
{std::pair{"", R"(["1970-01-01 00:00:00", null])"},
2021+
std::pair{"literal {%Y} %Q %q %J",
2022+
R"(["literal {1970} 0 s %J", null])"}}) {
2023+
SCOPED_TRACE(format);
2024+
const auto options = StrftimeOptions(format);
2025+
CheckScalarUnary("strftime", type, input, utf8(), expected, &options);
2026+
}
2027+
}
2028+
20162029
TEST_F(ScalarTemporalTest, StrftimeOffsetTimezone) {
20172030
auto options_ymdhms = StrftimeOptions("%Y-%m-%dT%H:%M:%S");
20182031

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ add_arrow_test(utility-test
5757
byte_stream_split_test.cc
5858
cache_test.cc
5959
checked_cast_test.cc
60-
chrono_test.cc
6160
compression_test.cc
6261
decimal_test.cc
6362
float16_test.cc

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@
2626
// libstdc++ reads tzdata files (using TZDIR). Libraries without the C++20 timezone
2727
// APIs, including older libc++, still require the vendored date library.
2828
//
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
29+
// Use the standard backend wherever the C++20 timezone APIs are available.
30+
// On non-Windows, older libstdc++ versions still need the fallback because of
31+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116110 (fully fixed in GCC 16.2).
32+
// Check library macros, not __GNUC__, so Clang using libstdc++ agrees with GCC.
33+
// The datestamp distinguishes 16.2 (2026-08-07) from 16.1 and early snapshots.
34+
// Keep the existing Windows backend selection unchanged.
35+
#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L && \
36+
(defined(_WIN32) || !defined(__GLIBCXX__) || \
37+
(defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 16 && __GLIBCXX__ >= 20260807))
3238
# define ARROW_USE_STD_CHRONO 1
3339
#else
3440
# define ARROW_USE_STD_CHRONO 0

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
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. It uses std::chrono on supported Windows standard libraries
25-
/// and otherwise falls back to the vendored Howard Hinnant date library.
24+
/// functionality. It uses std::chrono with supported C++20 timezone
25+
/// implementations, otherwise falling back to the vendored Howard Hinnant
26+
/// date library.
2627
/// See chrono_config_internal.h for backend selection.
2728
///
2829
/// On Windows with MSVC, std::chrono uses the system timezone database,
@@ -35,10 +36,10 @@
3536
#include "arrow/util/chrono_config_internal.h"
3637

3738
#if ARROW_USE_STD_CHRONO
38-
// Use C++20 standard library chrono
39+
// Use C++20 standard library chrono and the vendored strftime-compatible formatter.
3940
# include <format>
40-
# include <iterator>
4141
# include <ostream>
42+
# include "arrow/vendored/datetime/date.h"
4243
#else
4344
// Use vendored Howard Hinnant date library
4445
# include "arrow/vendored/datetime.h"
@@ -130,9 +131,14 @@ template <typename CharT, typename Traits, typename Duration, typename TimeZoneP
130131
std::basic_ostream<CharT, Traits>& to_stream(
131132
std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
132133
const std::chrono::zoned_time<Duration, TimeZonePtr>& zt) {
133-
std::vformat_to(std::ostreambuf_iterator<CharT>(os), std::string("{:") + fmt + "}",
134-
std::make_format_args(zt));
135-
return os;
134+
using Precision = typename std::chrono::zoned_time<Duration, TimeZonePtr>::duration;
135+
using LocalTime = arrow_vendored::date::local_time<Precision>;
136+
constexpr CharT default_format[] = {'%', 'F', ' ', '%', 'T', CharT{}};
137+
const auto st = zt.get_sys_time();
138+
const auto info = zt.get_time_zone()->get_info(st);
139+
const LocalTime local{(st + info.offset).time_since_epoch()};
140+
return arrow_vendored::date::to_stream(os, *fmt == CharT{} ? default_format : fmt,
141+
local, &info.abbrev, &info.offset);
136142
}
137143

138144
// Format a duration or time point using strftime-like format specifiers.
@@ -233,7 +239,8 @@ template <typename CharT, typename Traits, typename Duration, typename TimeZoneP
233239
std::basic_ostream<CharT, Traits>& to_stream(
234240
std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
235241
const vendored::zoned_time<Duration, TimeZonePtr>& zt) {
236-
return vendored::to_stream(os, fmt, zt);
242+
constexpr CharT default_format[] = {'%', 'F', ' ', '%', 'T', CharT{}};
243+
return vendored::to_stream(os, *fmt == CharT{} ? default_format : fmt, zt);
237244
}
238245

239246
inline constexpr vendored::month jan = vendored::jan;

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

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎cpp/src/arrow/util/meson.build‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ utility_test_srcs = [
191191
'byte_stream_split_test.cc',
192192
'cache_test.cc',
193193
'checked_cast_test.cc',
194-
'chrono_test.cc',
195194
'compression_test.cc',
196195
'decimal_test.cc',
197196
'float16_test.cc',

‎cpp/src/gandiva/cast_time.cc‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ arrow::Status ExportedTimeFunctions::AddMappings(Engine* engine) const {
4848
} // namespace gandiva
4949
#endif // !GANDIVA_UNIT_TEST
5050

51+
namespace chrono = arrow::internal::chrono;
52+
5153
extern "C" {
5254

5355
// TODO : Do input validation or make sure the callers do that ?
5456
int gdv_fn_time_with_zone(int* time_fields, const char* zone, int zone_len,
5557
int64_t* ret_time) {
56-
using arrow::internal::chrono::day;
57-
using arrow::internal::chrono::local_days;
58-
using arrow::internal::chrono::locate_zone;
59-
using arrow::internal::chrono::month;
60-
using arrow::internal::chrono::time_zone;
61-
using arrow::internal::chrono::year;
58+
using chrono::day;
59+
using chrono::local_days;
60+
using chrono::locate_zone;
61+
using chrono::month;
62+
using chrono::time_zone;
63+
using chrono::year;
6264
using std::chrono::hours;
6365
using std::chrono::milliseconds;
6466
using std::chrono::minutes;

‎cpp/src/gandiva/gdv_function_stubs.cc‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "gandiva/random_generator_holder.h"
4040
#include "gandiva/to_date_holder.h"
4141

42+
namespace chrono = arrow::internal::chrono;
43+
4244
/// Stub functions that can be accessed from LLVM or the pre-compiled library.
4345

4446
extern "C" {
@@ -836,8 +838,8 @@ int32_t gdv_fn_cast_intervalyear_utf8_int32(int64_t context_ptr, int64_t holder_
836838
GANDIVA_EXPORT
837839
gdv_timestamp to_utc_timezone_timestamp(int64_t context, gdv_timestamp time_milliseconds,
838840
const char* timezone, gdv_int32 length) {
839-
using arrow::internal::chrono::locate_zone;
840-
using arrow::internal::chrono::sys_time;
841+
using chrono::locate_zone;
842+
using chrono::sys_time;
841843
using std::chrono::milliseconds;
842844

843845
sys_time<milliseconds> tp{milliseconds{time_milliseconds}};
@@ -856,8 +858,8 @@ GANDIVA_EXPORT
856858
gdv_timestamp from_utc_timezone_timestamp(gdv_int64 context,
857859
gdv_timestamp time_milliseconds,
858860
const char* timezone, gdv_int32 length) {
859-
using arrow::internal::chrono::sys_time;
860-
using arrow::internal::chrono::zoned_time;
861+
using chrono::sys_time;
862+
using chrono::zoned_time;
861863
using std::chrono::milliseconds;
862864

863865
const sys_time<milliseconds> tp{milliseconds{time_milliseconds}};

‎cpp/src/gandiva/precompiled/epoch_time_point.h‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
#include "arrow/util/chrono_internal.h"
2121

22+
namespace chrono = arrow::internal::chrono;
23+
2224
bool is_leap_year(int yy);
23-
bool did_days_overflow(arrow::internal::chrono::year_month_day ymd);
25+
bool did_days_overflow(chrono::year_month_day ymd);
2426
int last_possible_day_in_month(int month, int year);
2527

2628
// A point of time measured in millis since epoch.
@@ -37,19 +39,16 @@ class EpochTimePoint {
3739
int TmMon() const { return static_cast<unsigned int>(YearMonthDay().month()) - 1; }
3840

3941
int TmYday() const {
40-
auto to_days = arrow::internal::chrono::floor<arrow::internal::chrono::days>(tp_);
41-
auto first_day_in_year = arrow::internal::chrono::sys_days{
42-
YearMonthDay().year() / arrow::internal::chrono::jan / 1};
42+
auto to_days = chrono::floor<chrono::days>(tp_);
43+
auto first_day_in_year = chrono::sys_days{YearMonthDay().year() / chrono::jan / 1};
4344
return (to_days - first_day_in_year).count();
4445
}
4546

4647
int TmMday() const { return static_cast<unsigned int>(YearMonthDay().day()); }
4748

4849
int TmWday() const {
49-
auto to_days = arrow::internal::chrono::floor<arrow::internal::chrono::days>(tp_);
50-
return (arrow::internal::chrono::weekday{to_days} - // NOLINT
51-
arrow::internal::chrono::Sunday)
52-
.count();
50+
auto to_days = chrono::floor<chrono::days>(tp_);
51+
return (chrono::weekday{to_days} - chrono::Sunday).count();
5352
}
5453

5554
int TmHour() const { return static_cast<int>(TimeOfDay().hours().count()); }
@@ -62,19 +61,18 @@ class EpochTimePoint {
6261
}
6362

6463
EpochTimePoint AddYears(int num_years) const {
65-
auto ymd = YearMonthDay() + arrow::internal::chrono::years(num_years);
66-
return EpochTimePoint((arrow::internal::chrono::sys_days{ymd} + // NOLINT
64+
auto ymd = YearMonthDay() + chrono::years(num_years);
65+
return EpochTimePoint((chrono::sys_days{ymd} + // NOLINT
6766
TimeOfDay().to_duration())
6867
.time_since_epoch());
6968
}
7069

7170
EpochTimePoint AddMonths(int num_months) const {
72-
auto ymd = YearMonthDay() + arrow::internal::chrono::months(num_months);
71+
auto ymd = YearMonthDay() + chrono::months(num_months);
7372

74-
EpochTimePoint tp =
75-
EpochTimePoint((arrow::internal::chrono::sys_days{ymd} + // NOLINT
76-
TimeOfDay().to_duration())
77-
.time_since_epoch());
73+
EpochTimePoint tp = EpochTimePoint((chrono::sys_days{ymd} + // NOLINT
74+
TimeOfDay().to_duration())
75+
.time_since_epoch());
7876

7977
if (did_days_overflow(ymd)) {
8078
int days_to_offset =
@@ -87,8 +85,8 @@ class EpochTimePoint {
8785
}
8886

8987
EpochTimePoint AddDays(int num_days) const {
90-
auto days_since_epoch = arrow::internal::chrono::sys_days{YearMonthDay()} + // NOLINT
91-
arrow::internal::chrono::days(num_days);
88+
auto days_since_epoch = chrono::sys_days{YearMonthDay()} + // NOLINT
89+
chrono::days(num_days);
9290
return EpochTimePoint(
9391
(days_since_epoch + TimeOfDay().to_duration()).time_since_epoch());
9492
}
@@ -101,16 +99,14 @@ class EpochTimePoint {
10199

102100
int64_t MillisSinceEpoch() const { return tp_.time_since_epoch().count(); }
103101

104-
arrow::internal::chrono::hh_mm_ss<std::chrono::milliseconds> TimeOfDay() const {
105-
auto millis_since_midnight =
106-
tp_ - arrow::internal::chrono::floor<arrow::internal::chrono::days>(tp_);
107-
return arrow::internal::chrono::make_time(millis_since_midnight);
102+
chrono::hh_mm_ss<std::chrono::milliseconds> TimeOfDay() const {
103+
auto millis_since_midnight = tp_ - chrono::floor<chrono::days>(tp_);
104+
return chrono::make_time(millis_since_midnight);
108105
}
109106

110107
private:
111-
arrow::internal::chrono::year_month_day YearMonthDay() const {
112-
return arrow::internal::chrono::year_month_day{
113-
arrow::internal::chrono::floor<arrow::internal::chrono::days>(tp_)}; // NOLINT
108+
chrono::year_month_day YearMonthDay() const {
109+
return chrono::year_month_day{chrono::floor<chrono::days>(tp_)}; // NOLINT
114110
}
115111

116112
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp_;

0 commit comments

Comments
 (0)