|
38 | 38 | #if ARROW_USE_STD_CHRONO |
39 | 39 | // Use C++20 standard library chrono |
40 | 40 | # include <format> |
41 | | -# include <iterator> |
42 | 41 | # include <locale> |
43 | | -# include <ostream> |
44 | | -# include <ratio> |
45 | 42 | #else |
46 | 43 | // Use vendored Howard Hinnant date library |
47 | 44 | # include "arrow/vendored/datetime.h" |
@@ -127,163 +124,9 @@ inline const time_zone* locate_zone(std::string_view tz_name) { |
127 | 124 |
|
128 | 125 | inline const time_zone* current_zone() { return std::chrono::current_zone(); } |
129 | 126 |
|
130 | | -namespace detail { |
131 | | - |
132 | | -// Argument positions passed to std::vformat_to by to_stream below. |
133 | | -enum class FormatArgument : char { |
134 | | - ZonedTime = '0', |
135 | | - TimeOfDay = '1', |
136 | | - TimeOfDayCount = '2', |
137 | | -}; |
138 | | - |
139 | | -inline void AppendEscapedLiteral(std::string* out, char value) { |
140 | | - out->push_back(value); |
141 | | - if (value == '{' || value == '}') { |
142 | | - out->push_back(value); |
143 | | - } |
144 | | -} |
145 | | - |
146 | | -// These are the directives accepted by Arrow's existing strftime syntax. Treat |
147 | | -// all others as literals to preserve compatibility. |
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; |
151 | | - }; |
152 | | - if (modifier == '\0') { |
153 | | - return contains("aAbBhcCxdeDFgGHIjmMprRSTuUVWwXyYzZ"); |
154 | | - } |
155 | | - if (modifier == 'E') { |
156 | | - return contains("cCxXyYz"); |
157 | | - } |
158 | | - if (modifier == 'O') { |
159 | | - return contains("deHImMSuUVwWyz"); |
160 | | - } |
161 | | - return false; |
162 | | -} |
163 | | - |
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, '}'}; |
169 | | -} |
170 | | - |
171 | | -inline void AppendLocalizedField(std::string* out, FormatArgument argument) { |
172 | | - *out += {'{', static_cast<char>(argument), ':', 'L', '}'}; |
173 | | -} |
174 | | - |
175 | | -inline std::string ToChronoFormat(const char* fmt, bool use_microseconds_suffix) { |
176 | | - std::string out; |
177 | | - while (*fmt != '\0') { |
178 | | - if (*fmt != '%') { |
179 | | - AppendEscapedLiteral(&out, *fmt++); |
180 | | - continue; |
181 | | - } |
182 | | - |
183 | | - ++fmt; |
184 | | - if (*fmt == '\0') { |
185 | | - AppendEscapedLiteral(&out, '%'); |
186 | | - break; |
187 | | - } |
188 | | - |
189 | | - char modifier = '\0'; |
190 | | - if (*fmt == 'E' || *fmt == 'O') { |
191 | | - modifier = *fmt++; |
192 | | - if (*fmt == '\0') { |
193 | | - AppendEscapedLiteral(&out, '%'); |
194 | | - AppendEscapedLiteral(&out, modifier); |
195 | | - break; |
196 | | - } |
197 | | - } |
198 | | - const char specifier = *fmt++; |
199 | | - |
200 | | - if (modifier == '\0') { |
201 | | - switch (specifier) { |
202 | | - case '%': |
203 | | - AppendEscapedLiteral(&out, '%'); |
204 | | - continue; |
205 | | - case 'n': |
206 | | - AppendEscapedLiteral(&out, '\n'); |
207 | | - continue; |
208 | | - case 't': |
209 | | - AppendEscapedLiteral(&out, '\t'); |
210 | | - continue; |
211 | | - case 'Q': |
212 | | - // Formatting a duration's %Q does not consistently apply the numeric locale. |
213 | | - AppendLocalizedField(&out, FormatArgument::TimeOfDayCount); |
214 | | - continue; |
215 | | - case 'q': |
216 | | - if (use_microseconds_suffix) { |
217 | | - // Some standard libraries use "us"; Arrow uses the micro sign. |
218 | | - out += "\xC2\xB5s"; |
219 | | - } else { |
220 | | - AppendChronoField(&out, FormatArgument::TimeOfDay, specifier); |
221 | | - } |
222 | | - continue; |
223 | | - default: |
224 | | - break; |
225 | | - } |
226 | | - } |
227 | | - |
228 | | -# if defined(__GLIBCXX__) |
229 | | - if (modifier == 'O' && specifier == 'V') { |
230 | | - // libstdc++ does not yet accept %OV; use its equivalent base representation. |
231 | | - AppendChronoField(&out, FormatArgument::ZonedTime, specifier); |
232 | | - continue; |
233 | | - } |
234 | | -# endif |
235 | | - |
236 | | - if (IsSupportedStrftimeSpecifier(modifier, specifier)) { |
237 | | - AppendChronoField(&out, FormatArgument::ZonedTime, specifier, modifier); |
238 | | - } else { |
239 | | - AppendEscapedLiteral(&out, '%'); |
240 | | - if (modifier != '\0') AppendEscapedLiteral(&out, modifier); |
241 | | - AppendEscapedLiteral(&out, specifier); |
242 | | - } |
243 | | - } |
244 | | - return out; |
245 | | -} |
246 | | - |
247 | | -} // namespace detail |
248 | | - |
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) { |
270 | | - const auto local_time = zt.get_local_time(); |
271 | | - const auto local_day = std::chrono::floor<std::chrono::days>(local_time); |
272 | | - const auto time_of_day = local_time - local_day; |
273 | | - const auto time_of_day_count = time_of_day.count(); |
274 | | - |
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); |
281 | | - } |
282 | | - return os; |
283 | | -} |
284 | | - |
285 | 127 | // Format durations and unzoned time points using internal format strings shared |
286 | | -// by both backends. User-supplied strftime syntax uses ZonedFormat and to_stream. |
| 128 | +// by both backends. User-supplied strftime syntax uses StrftimeFormatter in |
| 129 | +// arrow/util/strftime_internal.h. |
287 | 130 | // Keep each format in one replacement field to avoid repeating duration signs. |
288 | 131 | template <typename Temporal> |
289 | 132 | std::string format(const char* fmt, const Temporal& value) { |
@@ -378,23 +221,6 @@ using vendored::set_install; |
378 | 221 | // Formatting support |
379 | 222 | using vendored::format; |
380 | 223 |
|
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); |
396 | | -} |
397 | | - |
398 | 224 | inline constexpr vendored::month jan = vendored::jan; |
399 | 225 | inline constexpr vendored::month dec = vendored::dec; |
400 | 226 |
|
|
0 commit comments