Skip to content

Commit 29491ca

Browse files
committed
apacheGH-51215: [C++] Move strftime formatting out of the chrono shim
1 parent 7556de6 commit 29491ca

4 files changed

Lines changed: 217 additions & 186 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "arrow/compute/api_scalar.h"
2424
#include "arrow/compute/kernels/codegen_internal.h"
2525
#include "arrow/util/date_internal.h"
26+
#include "arrow/util/strftime_internal.h"
2627
#include "arrow/util/value_parsing.h"
2728

2829
namespace arrow::compute::internal {
@@ -165,13 +166,13 @@ struct ZonedLocalizer {
165166

166167
template <typename Duration>
167168
struct TimestampFormatter {
168-
const chrono::ZonedFormat<Duration> format;
169+
const arrow::internal::StrftimeFormatter<Duration> formatter;
169170
const ArrowTimeZone tz;
170171
std::ostringstream bufstream;
171172

172173
explicit TimestampFormatter(const std::string& format, const ArrowTimeZone time_zone,
173174
const std::locale& locale)
174-
: format(format), tz(time_zone) {
175+
: formatter(format), tz(time_zone) {
175176
bufstream.imbue(locale);
176177
// Propagate errors as C++ exceptions (to get an actual error message)
177178
bufstream.exceptions(std::ios::failbit | std::ios::badbit);
@@ -182,7 +183,7 @@ struct TimestampFormatter {
182183
const auto timepoint = sys_time<Duration>(Duration{arg});
183184
auto format_zoned_time = [&](auto&& zt) {
184185
try {
185-
chrono::to_stream(bufstream, format, zt);
186+
formatter.Format(bufstream, zt);
186187
return Status::OK();
187188
} catch (const std::runtime_error& ex) {
188189
bufstream.clear();

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

Lines changed: 2 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838
#if ARROW_USE_STD_CHRONO
3939
// Use C++20 standard library chrono
4040
# include <format>
41-
# include <iterator>
4241
# include <locale>
43-
# include <ostream>
44-
# include <ratio>
4542
#else
4643
// Use vendored Howard Hinnant date library
4744
# include "arrow/vendored/datetime.h"
@@ -127,163 +124,9 @@ inline const time_zone* locate_zone(std::string_view tz_name) {
127124

128125
inline const time_zone* current_zone() { return std::chrono::current_zone(); }
129126

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-
285127
// 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.
287130
// Keep each format in one replacement field to avoid repeating duration signs.
288131
template <typename Temporal>
289132
std::string format(const char* fmt, const Temporal& value) {
@@ -378,23 +221,6 @@ using vendored::set_install;
378221
// Formatting support
379222
using vendored::format;
380223

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-
398224
inline constexpr vendored::month jan = vendored::jan;
399225
inline constexpr vendored::month dec = vendored::dec;
400226

0 commit comments

Comments
 (0)