1919
2020#include < chrono>
2121#include < cstdint>
22+ #include < locale>
23+ #include < ostream>
24+ #include < sstream>
25+ #include < string>
26+ #include < string_view>
2227
2328#include " arrow/compute/api_scalar.h"
2429#include " arrow/compute/kernels/codegen_internal.h"
2530#include " arrow/util/date_internal.h"
2631#include " arrow/util/value_parsing.h"
2732
33+ #if ARROW_USE_STD_CHRONO
34+ # include < format>
35+ # include < iterator>
36+ # include < ratio>
37+ #endif
38+
2839namespace arrow ::compute::internal {
2940
3041namespace chrono = arrow::internal::chrono;
@@ -163,15 +174,179 @@ struct ZonedLocalizer {
163174 local_days ConvertDays (sys_days d) const { return local_days (year_month_day (d)); }
164175};
165176
177+ #if ARROW_USE_STD_CHRONO
178+ namespace detail {
179+
180+ // Argument positions passed to std::vformat_to by StrftimeFormatter below.
181+ enum class FormatArgument : char {
182+ ZonedTime = ' 0' ,
183+ TimeOfDay = ' 1' ,
184+ TimeOfDayCount = ' 2' ,
185+ };
186+
187+ inline void AppendEscapedLiteral (std::string* out, char value) {
188+ out->push_back (value);
189+ if (value == ' {' || value == ' }' ) {
190+ out->push_back (value);
191+ }
192+ }
193+
194+ // These are the directives accepted by Arrow's existing strftime syntax. Treat
195+ // all others as literals to preserve compatibility.
196+ inline bool IsSupportedStrftimeSpecifier (char modifier, char specifier) {
197+ const auto contains = [specifier](std::string_view candidates) {
198+ return candidates.find (specifier) != std::string_view::npos;
199+ };
200+ if (modifier == ' \0 ' ) {
201+ return contains (" aAbBhcCxdeDFgGHIjmMprRSTuUVWwXyYzZ" );
202+ }
203+ if (modifier == ' E' ) {
204+ return contains (" cCxXyYz" );
205+ }
206+ if (modifier == ' O' ) {
207+ return contains (" deHImMSuUVwWyz" );
208+ }
209+ return false ;
210+ }
211+
212+ inline void AppendChronoField (std::string* out, FormatArgument argument, char specifier,
213+ char modifier = ' \0 ' ) {
214+ *out += {' {' , static_cast <char >(argument), ' :' , ' L' , ' %' };
215+ if (modifier != ' \0 ' ) out->push_back (modifier);
216+ *out += {specifier, ' }' };
217+ }
218+
219+ inline void AppendLocalizedField (std::string* out, FormatArgument argument) {
220+ *out += {' {' , static_cast <char >(argument), ' :' , ' L' , ' }' };
221+ }
222+
223+ inline std::string ToChronoFormat (const char * fmt, bool use_microseconds_suffix) {
224+ std::string out;
225+ while (*fmt != ' \0 ' ) {
226+ if (*fmt != ' %' ) {
227+ AppendEscapedLiteral (&out, *fmt++);
228+ continue ;
229+ }
230+
231+ ++fmt;
232+ if (*fmt == ' \0 ' ) {
233+ AppendEscapedLiteral (&out, ' %' );
234+ break ;
235+ }
236+
237+ char modifier = ' \0 ' ;
238+ if (*fmt == ' E' || *fmt == ' O' ) {
239+ modifier = *fmt++;
240+ if (*fmt == ' \0 ' ) {
241+ AppendEscapedLiteral (&out, ' %' );
242+ AppendEscapedLiteral (&out, modifier);
243+ break ;
244+ }
245+ }
246+ const char specifier = *fmt++;
247+
248+ if (modifier == ' \0 ' ) {
249+ switch (specifier) {
250+ case ' %' :
251+ AppendEscapedLiteral (&out, ' %' );
252+ continue ;
253+ case ' n' :
254+ AppendEscapedLiteral (&out, ' \n ' );
255+ continue ;
256+ case ' t' :
257+ AppendEscapedLiteral (&out, ' \t ' );
258+ continue ;
259+ case ' Q' :
260+ // Formatting a duration's %Q does not consistently apply the numeric locale.
261+ AppendLocalizedField (&out, FormatArgument::TimeOfDayCount);
262+ continue ;
263+ case ' q' :
264+ if (use_microseconds_suffix) {
265+ // Some standard libraries use "us"; Arrow uses the micro sign.
266+ out += " \xC2\xB5 s" ;
267+ } else {
268+ AppendChronoField (&out, FormatArgument::TimeOfDay, specifier);
269+ }
270+ continue ;
271+ default :
272+ break ;
273+ }
274+ }
275+
276+ # if defined(__GLIBCXX__)
277+ if (modifier == ' O' && specifier == ' V' ) {
278+ // libstdc++ does not yet accept %OV; use its equivalent base representation.
279+ AppendChronoField (&out, FormatArgument::ZonedTime, specifier);
280+ continue ;
281+ }
282+ # endif
283+
284+ if (IsSupportedStrftimeSpecifier (modifier, specifier)) {
285+ AppendChronoField (&out, FormatArgument::ZonedTime, specifier, modifier);
286+ } else {
287+ AppendEscapedLiteral (&out, ' %' );
288+ if (modifier != ' \0 ' ) AppendEscapedLiteral (&out, modifier);
289+ AppendEscapedLiteral (&out, specifier);
290+ }
291+ }
292+ return out;
293+ }
294+
295+ } // namespace detail
296+ #endif
297+
298+ // Prepare Arrow's strftime syntax once and reuse it for values with the same
299+ // precision. The timezone belongs to each value, not to this formatter.
300+ template <typename Duration>
301+ class StrftimeFormatter {
302+ public:
303+ explicit StrftimeFormatter (const std::string& format) {
304+ #if ARROW_USE_STD_CHRONO
305+ using Precision = typename chrono::zoned_time<Duration>::duration;
306+ format_ = detail::ToChronoFormat (
307+ format.c_str (), std::ratio_equal_v<typename Precision::period, std::micro>);
308+ #else
309+ format_ = format;
310+ #endif
311+ }
312+
313+ // Literal braces and unsupported directives remain literal; %Q/%q use local
314+ // time of day rather than elapsed time since the epoch.
315+ template <typename TimeZonePtr>
316+ std::ostream& Format (std::ostream& os,
317+ const chrono::zoned_time<Duration, TimeZonePtr>& value) const {
318+ #if ARROW_USE_STD_CHRONO
319+ const auto local_time = value.get_local_time ();
320+ const auto local_day = std::chrono::floor<std::chrono::days>(local_time);
321+ const auto time_of_day = local_time - local_day;
322+ const auto time_of_day_count = time_of_day.count ();
323+
324+ const std::ostream::sentry sentry (os);
325+ if (sentry) {
326+ const auto end =
327+ std::vformat_to (std::ostreambuf_iterator<char >(os), os.getloc (), format_,
328+ std::make_format_args (value, time_of_day, time_of_day_count));
329+ if (end.failed ()) os.setstate (std::ios::badbit);
330+ }
331+ return os;
332+ #else
333+ return arrow_vendored::date::to_stream (os, format_.c_str (), value);
334+ #endif
335+ }
336+
337+ private:
338+ std::string format_;
339+ };
340+
166341template <typename Duration>
167342struct TimestampFormatter {
168- const chrono::ZonedFormat <Duration> format ;
343+ const StrftimeFormatter <Duration> formatter ;
169344 const ArrowTimeZone tz;
170345 std::ostringstream bufstream;
171346
172347 explicit TimestampFormatter (const std::string& format, const ArrowTimeZone time_zone,
173348 const std::locale& locale)
174- : format (format), tz(time_zone) {
349+ : formatter (format), tz(time_zone) {
175350 bufstream.imbue (locale);
176351 // Propagate errors as C++ exceptions (to get an actual error message)
177352 bufstream.exceptions (std::ios::failbit | std::ios::badbit);
@@ -182,7 +357,7 @@ struct TimestampFormatter {
182357 const auto timepoint = sys_time<Duration>(Duration{arg});
183358 auto format_zoned_time = [&](auto && zt) {
184359 try {
185- chrono::to_stream (bufstream, format , zt);
360+ formatter. Format (bufstream, zt);
186361 return Status::OK ();
187362 } catch (const std::runtime_error& ex) {
188363 bufstream.clear ();
0 commit comments