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
130130namespace 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.
133133enum 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\xB5 s" ;
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.
292288template <typename Temporal>
293289std::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
383379using 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
392398inline constexpr vendored::month jan = vendored::jan;
0 commit comments