Skip to content

Commit ef7d5fe

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. Select using library feature/version macros so GCC and Clang agree. Other libraries with complete timezone APIs can use the standard backend as well. Preserve stream locales and normalize zoned_time formatting to its stored precision, including date-sized inputs. Add coverage to the existing utility test target; leave all CI workflows and job paths unchanged. Validation: feature-only selection builds with GCC 14, but the existing six zoned temporal/cast cases fail. With the known-broken-library fallback retained, all four tested Arrow targets pass. Seven Arrow/Gandiva targets also passed with GCC 13; 32 synthetic backend-selection cases, clang-format, cpplint and whitespace checks passed. Actual fixed GCC 16.2 execution remains for existing CI.
1 parent d22fc96 commit ef7d5fe

3 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎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: 11 additions & 4 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,
@@ -130,8 +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));
134+
// zoned_time stores at least seconds, even for a coarser input duration. Format
135+
// that precision explicitly: older libstdc++ formatters reject zoned_time<days>.
136+
using Precision = typename std::chrono::zoned_time<Duration, TimeZonePtr>::duration;
137+
const std::chrono::zoned_time<Precision, TimeZonePtr> printable{zt.get_time_zone(),
138+
zt.get_sys_time()};
139+
// Like date::to_stream, honor the stream locale (chrono formatting requires L).
140+
std::vformat_to(std::ostreambuf_iterator<CharT>(os), os.getloc(),
141+
std::string("{:L") + fmt + "}", std::make_format_args(printable));
135142
return os;
136143
}
137144

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <gtest/gtest.h>
2121

2222
#include <chrono>
23+
#include <locale>
24+
#include <sstream>
2325
#include <type_traits>
2426

2527
namespace arrow::internal {
@@ -62,6 +64,28 @@ TEST(Chrono, Format) {
6264
"1969-12-31 23:59:59.999999999");
6365
}
6466

67+
TEST(Chrono, StreamDate) {
68+
const chrono::zoned_time<chrono::days> epoch{"UTC", chrono::sys_days{}};
69+
std::ostringstream stream;
70+
chrono::to_stream(stream, "%F %T", epoch);
71+
EXPECT_EQ(stream.str(), "1970-01-01 00:00:00");
72+
}
73+
74+
TEST(Chrono, StreamLocale) {
75+
// Avoid depending on installed system locales.
76+
class DecimalCommaFacet : public std::numpunct<char> {
77+
protected:
78+
char do_decimal_point() const override { return ','; }
79+
};
80+
81+
std::ostringstream stream;
82+
stream.imbue(std::locale(std::locale::classic(), new DecimalCommaFacet));
83+
const chrono::zoned_time<std::chrono::milliseconds> time{
84+
"UTC", chrono::sys_time<std::chrono::milliseconds>{123ms}};
85+
chrono::to_stream(stream, "%S", time);
86+
EXPECT_EQ(stream.str(), "00,123");
87+
}
88+
6589
TEST(Chrono, TimezoneTransitions) {
6690
const auto* zone = chrono::locate_zone("Europe/Brussels");
6791
const auto spring = chrono::sys_days{chrono::year{2015} / 3 / 29} + 1h;

0 commit comments

Comments
 (0)