Skip to content

Commit 7556de6

Browse files
committed
apacheGH-51215: [C++] Address chrono shim review feedback
1 parent 77d3787 commit 7556de6

7 files changed

Lines changed: 174 additions & 100 deletions

File tree

‎cpp/src/arrow/compute/kernels/temporal_internal.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct ZonedLocalizer {
165165

166166
template <typename Duration>
167167
struct TimestampFormatter {
168-
const std::string format;
168+
const chrono::ZonedFormat<Duration> format;
169169
const ArrowTimeZone tz;
170170
std::ostringstream bufstream;
171171

@@ -182,7 +182,7 @@ struct TimestampFormatter {
182182
const auto timepoint = sys_time<Duration>(Duration{arg});
183183
auto format_zoned_time = [&](auto&& zt) {
184184
try {
185-
chrono::to_stream(bufstream, format.c_str(), zt);
185+
chrono::to_stream(bufstream, format, zt);
186186
return Status::OK();
187187
} catch (const std::runtime_error& ex) {
188188
bufstream.clear();

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,21 @@
1919

2020
#include <chrono>
2121

22-
// Share backend selection with the vendored implementation without including its
23-
// headers. datetime.h undefines macros needed when compiling the implementation.
22+
// Prefer std::chrono when the standard library provides the C++20 timezone APIs.
23+
// Builds may explicitly set ARROW_USE_STD_CHRONO to 0 (vendored date.h) or 1
24+
// (std::chrono) to override automatic selection.
2425
//
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.
26+
// Share this selection between chrono_internal.h and the vendored timezone sources
27+
// datetime/tz.cpp and datetime/ios.mm. Do not include arrow/vendored/datetime.h here:
28+
// it undefines NOEXCEPT, which is needed to compile datetime/tz.cpp.
2829
//
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.
30+
// On Windows, MSVC's standard library uses the system timezone database, while
31+
// libstdc++ reads tzdata files (using TZDIR).
3132
//
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
33+
// Select the vendored date backend when the C++20 timezone APIs are unavailable.
34+
// On non-Windows, also select it for older libstdc++ versions because of
3435
// 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.
3636
// The datestamp distinguishes 16.2 (2026-08-07) from 16.1 and early snapshots.
37-
// Keep the existing Windows backend selection unchanged.
3837
#ifndef ARROW_USE_STD_CHRONO
3938
# define ARROW_USE_STD_CHRONO 1
4039
# if !defined(__cpp_lib_chrono) || __cpp_lib_chrono < 201907L

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

Lines changed: 86 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
#if ARROW_USE_STD_CHRONO
3939
// Use C++20 standard library chrono
4040
# include <format>
41+
# include <iterator>
4142
# include <locale>
4243
# include <ostream>
4344
# include <ratio>
44-
# include <type_traits>
4545
#else
4646
// Use vendored Howard Hinnant date library
4747
# include "arrow/vendored/datetime.h"
@@ -129,107 +129,93 @@ inline const time_zone* current_zone() { return std::chrono::current_zone(); }
129129

130130
namespace detail {
131131

132-
// Argument positions passed to std::vformat by to_stream below.
132+
// Argument positions passed to std::vformat_to by to_stream below.
133133
enum class FormatArgument : char {
134134
ZonedTime = '0',
135135
TimeOfDay = '1',
136136
TimeOfDayCount = '2',
137137
};
138138

139-
template <typename CharT>
140-
void AppendEscapedLiteral(std::basic_string<CharT>* out, CharT value) {
139+
inline void AppendEscapedLiteral(std::string* out, char value) {
141140
out->push_back(value);
142-
if (value == CharT{'{'} || value == CharT{'}'}) {
141+
if (value == '{' || value == '}') {
143142
out->push_back(value);
144143
}
145144
}
146145

147146
// These are the directives accepted by Arrow's existing strftime syntax. Treat
148147
// all others as literals to preserve compatibility.
149-
template <typename CharT>
150-
bool IsSupportedStrftimeSpecifier(CharT modifier, CharT specifier) {
151-
const auto contains = [specifier](const char* candidates) {
152-
for (; *candidates != '\0'; ++candidates) {
153-
if (specifier == static_cast<CharT>(*candidates)) return true;
154-
}
155-
return false;
148+
inline bool IsSupportedStrftimeSpecifier(char modifier, char specifier) {
149+
const auto contains = [specifier](std::string_view candidates) {
150+
return candidates.find(specifier) != std::string_view::npos;
156151
};
157-
if (modifier == CharT{}) {
152+
if (modifier == '\0') {
158153
return contains("aAbBhcCxdeDFgGHIjmMprRSTuUVWwXyYzZ");
159154
}
160-
if (modifier == CharT{'E'}) {
155+
if (modifier == 'E') {
161156
return contains("cCxXyYz");
162157
}
163-
if (modifier == CharT{'O'}) {
158+
if (modifier == 'O') {
164159
return contains("deHImMSuUVwWyz");
165160
}
166161
return false;
167162
}
168163

169-
template <typename CharT>
170-
void AppendChronoField(std::basic_string<CharT>* out, FormatArgument argument,
171-
CharT specifier, CharT modifier = CharT{}) {
172-
*out += {CharT{'{'}, static_cast<CharT>(argument), CharT{':'}, CharT{'L'}, CharT{'%'}};
173-
if (modifier != CharT{}) out->push_back(modifier);
174-
*out += {specifier, CharT{'}'}};
164+
inline void AppendChronoField(std::string* out, FormatArgument argument, char specifier,
165+
char modifier = '\0') {
166+
*out += {'{', static_cast<char>(argument), ':', 'L', '%'};
167+
if (modifier != '\0') out->push_back(modifier);
168+
*out += {specifier, '}'};
175169
}
176170

177-
template <typename CharT>
178-
void AppendLocalizedField(std::basic_string<CharT>* out, FormatArgument argument) {
179-
*out += {CharT{'{'}, static_cast<CharT>(argument), CharT{':'}, CharT{'L'}, CharT{'}'}};
171+
inline void AppendLocalizedField(std::string* out, FormatArgument argument) {
172+
*out += {'{', static_cast<char>(argument), ':', 'L', '}'};
180173
}
181174

182-
template <typename CharT>
183-
std::basic_string<CharT> ToChronoFormat(const CharT* fmt, bool use_microseconds_suffix) {
184-
std::basic_string<CharT> out;
185-
while (*fmt != CharT{}) {
186-
if (*fmt != CharT{'%'}) {
175+
inline std::string ToChronoFormat(const char* fmt, bool use_microseconds_suffix) {
176+
std::string out;
177+
while (*fmt != '\0') {
178+
if (*fmt != '%') {
187179
AppendEscapedLiteral(&out, *fmt++);
188180
continue;
189181
}
190182

191183
++fmt;
192-
if (*fmt == CharT{}) {
193-
AppendEscapedLiteral(&out, CharT{'%'});
184+
if (*fmt == '\0') {
185+
AppendEscapedLiteral(&out, '%');
194186
break;
195187
}
196188

197-
CharT modifier{};
198-
if (*fmt == CharT{'E'} || *fmt == CharT{'O'}) {
189+
char modifier = '\0';
190+
if (*fmt == 'E' || *fmt == 'O') {
199191
modifier = *fmt++;
200-
if (*fmt == CharT{}) {
201-
AppendEscapedLiteral(&out, CharT{'%'});
192+
if (*fmt == '\0') {
193+
AppendEscapedLiteral(&out, '%');
202194
AppendEscapedLiteral(&out, modifier);
203195
break;
204196
}
205197
}
206-
const CharT specifier = *fmt++;
198+
const char specifier = *fmt++;
207199

208-
if (modifier == CharT{}) {
200+
if (modifier == '\0') {
209201
switch (specifier) {
210-
case CharT{'%'}:
211-
AppendEscapedLiteral(&out, CharT{'%'});
202+
case '%':
203+
AppendEscapedLiteral(&out, '%');
212204
continue;
213-
case CharT{'n'}:
214-
AppendEscapedLiteral(&out, CharT{'\n'});
205+
case 'n':
206+
AppendEscapedLiteral(&out, '\n');
215207
continue;
216-
case CharT{'t'}:
217-
AppendEscapedLiteral(&out, CharT{'\t'});
208+
case 't':
209+
AppendEscapedLiteral(&out, '\t');
218210
continue;
219-
case CharT{'Q'}:
211+
case 'Q':
220212
// Formatting a duration's %Q does not consistently apply the numeric locale.
221213
AppendLocalizedField(&out, FormatArgument::TimeOfDayCount);
222214
continue;
223-
case CharT{'q'}:
215+
case 'q':
224216
if (use_microseconds_suffix) {
225217
// Some standard libraries use "us"; Arrow uses the micro sign.
226-
if constexpr (std::is_same_v<CharT, char>) {
227-
AppendEscapedLiteral(&out, CharT{'\xC2'});
228-
AppendEscapedLiteral(&out, CharT{'\xB5'});
229-
} else {
230-
AppendEscapedLiteral(&out, static_cast<CharT>(0xB5));
231-
}
232-
AppendEscapedLiteral(&out, CharT{'s'});
218+
out += "\xC2\xB5s";
233219
} else {
234220
AppendChronoField(&out, FormatArgument::TimeOfDay, specifier);
235221
}
@@ -240,7 +226,7 @@ std::basic_string<CharT> ToChronoFormat(const CharT* fmt, bool use_microseconds_
240226
}
241227

242228
# if defined(__GLIBCXX__)
243-
if (modifier == CharT{'O'} && specifier == CharT{'V'}) {
229+
if (modifier == 'O' && specifier == 'V') {
244230
// libstdc++ does not yet accept %OV; use its equivalent base representation.
245231
AppendChronoField(&out, FormatArgument::ZonedTime, specifier);
246232
continue;
@@ -250,8 +236,8 @@ std::basic_string<CharT> ToChronoFormat(const CharT* fmt, bool use_microseconds_
250236
if (IsSupportedStrftimeSpecifier(modifier, specifier)) {
251237
AppendChronoField(&out, FormatArgument::ZonedTime, specifier, modifier);
252238
} else {
253-
AppendEscapedLiteral(&out, CharT{'%'});
254-
if (modifier != CharT{}) AppendEscapedLiteral(&out, modifier);
239+
AppendEscapedLiteral(&out, '%');
240+
if (modifier != '\0') AppendEscapedLiteral(&out, modifier);
255241
AppendEscapedLiteral(&out, specifier);
256242
}
257243
}
@@ -260,35 +246,45 @@ std::basic_string<CharT> ToChronoFormat(const CharT* fmt, bool use_microseconds_
260246

261247
} // namespace detail
262248

263-
// Convert Arrow's strftime syntax to C++20 replacement fields. Literal braces and
264-
// unsupported directives remain literal, and %Q/%q use local time of day.
265-
template <typename CharT, typename Traits, typename Duration, typename TimeZonePtr>
266-
std::basic_ostream<CharT, Traits>& to_stream(
267-
std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
268-
const std::chrono::zoned_time<Duration, TimeZonePtr>& zt) {
269-
static_assert(std::is_same_v<CharT, char> || std::is_same_v<CharT, wchar_t>);
270-
using Precision = typename std::chrono::zoned_time<Duration, TimeZonePtr>::duration;
271-
const auto standard_format = detail::ToChronoFormat(
272-
fmt, std::ratio_equal_v<typename Precision::period, std::micro>);
249+
// Prepare once and reuse for a sequence of zoned times with the same precision.
250+
template <typename Duration>
251+
class ZonedFormat {
252+
public:
253+
explicit ZonedFormat(const std::string& format)
254+
: value_(detail::ToChronoFormat(
255+
format.c_str(), std::ratio_equal_v<typename Precision::period, std::micro>)) {
256+
}
257+
258+
const std::string& value() const { return value_; }
259+
260+
private:
261+
using Precision = typename zoned_time<Duration>::duration;
262+
std::string value_;
263+
};
264+
265+
// Literal braces and unsupported directives remain literal; %Q/%q use local time
266+
// of day rather than elapsed time since the epoch.
267+
template <typename Duration, typename TimeZonePtr>
268+
std::ostream& to_stream(std::ostream& os, const ZonedFormat<Duration>& format,
269+
const std::chrono::zoned_time<Duration, TimeZonePtr>& zt) {
273270
const auto local_time = zt.get_local_time();
274271
const auto local_day = std::chrono::floor<std::chrono::days>(local_time);
275272
const auto time_of_day = local_time - local_day;
276273
const auto time_of_day_count = time_of_day.count();
277274

278-
std::basic_string<CharT> formatted;
279-
if constexpr (std::is_same_v<CharT, char>) {
280-
formatted = std::vformat(os.getloc(), standard_format,
281-
std::make_format_args(zt, time_of_day, time_of_day_count));
282-
} else {
283-
formatted = std::vformat(os.getloc(), standard_format,
284-
std::make_wformat_args(zt, time_of_day, time_of_day_count));
275+
const std::ostream::sentry sentry(os);
276+
if (sentry) {
277+
const auto end =
278+
std::vformat_to(std::ostreambuf_iterator<char>(os), os.getloc(), format.value(),
279+
std::make_format_args(zt, time_of_day, time_of_day_count));
280+
if (end.failed()) os.setstate(std::ios::badbit);
285281
}
286-
os.write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
287282
return os;
288283
}
289284

290-
// Format a duration or time point using strftime-like format specifiers.
291-
// Converts "%H%M" style to C++20's "{:L%H%M}" style and uses std::vformat.
285+
// Format durations and unzoned time points using internal format strings shared
286+
// by both backends. User-supplied strftime syntax uses ZonedFormat and to_stream.
287+
// Keep each format in one replacement field to avoid repeating duration signs.
292288
template <typename Temporal>
293289
std::string format(const char* fmt, const Temporal& value) {
294290
return std::vformat(std::locale{}, std::string("{:L") + fmt + "}",
@@ -382,11 +378,21 @@ using vendored::set_install;
382378
// Formatting support
383379
using vendored::format;
384380

385-
template <typename CharT, typename Traits, typename Duration, typename TimeZonePtr>
386-
std::basic_ostream<CharT, Traits>& to_stream(
387-
std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
388-
const vendored::zoned_time<Duration, TimeZonePtr>& zt) {
389-
return vendored::to_stream(os, fmt, zt);
381+
template <typename Duration>
382+
class ZonedFormat {
383+
public:
384+
explicit ZonedFormat(const std::string& format) : value_(format) {}
385+
386+
const std::string& value() const { return value_; }
387+
388+
private:
389+
std::string value_;
390+
};
391+
392+
template <typename Duration, typename TimeZonePtr>
393+
std::ostream& to_stream(std::ostream& os, const ZonedFormat<Duration>& format,
394+
const vendored::zoned_time<Duration, TimeZonePtr>& zt) {
395+
return vendored::to_stream(os, format.value().c_str(), zt);
390396
}
391397

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

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
#include "arrow/testing/gtest_util.h"
2424
#include "arrow/util/logger.h"
2525

26-
// Emit log via the default logger. Token-paste here to prevent Windows' ERROR
27-
// macro from expanding before the logger macro is selected.
26+
// Emit log via the default logger. In unity builds, io_util_test.cc can include
27+
// Windows headers before this file. Paste LEVEL here so ERROR is not expanded to
28+
// 0 before selecting ARROW_LOGGER_ERROR.
2829
#define DO_LOG(LEVEL, ...) ARROW_LOGGER_##LEVEL("", __VA_ARGS__)
2930

3031
namespace arrow {
@@ -59,6 +60,16 @@ struct OstreamableTracer {
5960

6061
} // namespace
6162

63+
// Reproduce the Windows macro collision on every platform, without leaking the
64+
// test macro into other sources in a unity build.
65+
#pragma push_macro("ERROR")
66+
#undef ERROR
67+
#define ERROR 0
68+
TEST(LoggerTest, ErrorMacroCollision) {
69+
DO_LOG(ERROR, "Logging with the Windows ERROR macro defined");
70+
}
71+
#pragma pop_macro("ERROR")
72+
6273
TEST(LoggerTest, Basics) {
6374
// Basic tests using the default logger
6475
DO_LOG(ERROR);

0 commit comments

Comments
 (0)