Skip to content

Commit 5b4bee2

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

4 files changed

Lines changed: 229 additions & 228 deletions

File tree

‎cpp/src/arrow/compute/kernels/scalar_temporal_test.cc‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#include <sstream>
1819
#include <tuple>
1920

2021
#include <gtest/gtest.h>
2122

2223
#include "arrow/compute/api_scalar.h"
2324
#include "arrow/compute/cast.h"
25+
#include "arrow/compute/kernels/temporal_internal.h"
2426
#include "arrow/compute/kernels/test_util_internal.h"
2527
#include "arrow/testing/gtest_util.h"
2628
#include "arrow/testing/matchers.h"
@@ -2003,6 +2005,52 @@ TEST_F(ScalarTemporalTest, TestAssumeTimezoneNonexistent) {
20032005
&options_earliest);
20042006
}
20052007

2008+
TEST(StrftimeFormatterTest, ReuseFormatter) {
2009+
namespace chrono = arrow::internal::chrono;
2010+
using std::chrono::microseconds;
2011+
const arrow::internal::OffsetZone zone{std::chrono::minutes{60}};
2012+
const internal::StrftimeFormatter<microseconds> formatter{"{%F %T} %Q %q %J"};
2013+
for (const auto& [count, expected] :
2014+
{std::pair{1, "{1970-01-01 01:00:00.000001} 3600000001 \xC2\xB5s %J"},
2015+
std::pair{-1, "{1970-01-01 00:59:59.999999} 3599999999 \xC2\xB5s %J"}}) {
2016+
std::ostringstream out;
2017+
out.imbue(std::locale::classic());
2018+
const chrono::zoned_time<microseconds, arrow::internal::OffsetZone> value{
2019+
zone, chrono::sys_time<microseconds>{microseconds{count}}};
2020+
formatter.Format(out, value);
2021+
EXPECT_EQ(out.str(), expected);
2022+
}
2023+
}
2024+
2025+
TEST(StrftimeFormatterTest, StreamState) {
2026+
namespace chrono = arrow::internal::chrono;
2027+
using std::chrono::seconds;
2028+
const internal::StrftimeFormatter<seconds> formatter{"%F %T"};
2029+
const chrono::zoned_time<seconds, arrow::internal::OffsetZone> value{
2030+
arrow::internal::OffsetZone{std::chrono::minutes{0}},
2031+
chrono::sys_time<seconds>{seconds{0}}};
2032+
std::ostringstream out;
2033+
out.imbue(std::locale::classic());
2034+
out << std::hex << std::showbase;
2035+
out.precision(3);
2036+
out.width(30);
2037+
out.fill('*');
2038+
const auto flags = out.flags();
2039+
EXPECT_EQ(&formatter.Format(out, value), &out);
2040+
EXPECT_EQ(out.str(), "1970-01-01 00:00:00");
2041+
EXPECT_EQ(out.flags(), flags);
2042+
EXPECT_EQ(out.precision(), 3);
2043+
EXPECT_EQ(out.width(), 30);
2044+
EXPECT_EQ(out.fill(), '*');
2045+
2046+
// A streambuf with no put area rejects every write.
2047+
class FailingBuffer : public std::streambuf {
2048+
} buffer;
2049+
std::ostream failing(&buffer);
2050+
failing.exceptions(std::ios::badbit | std::ios::failbit);
2051+
EXPECT_THROW(formatter.Format(failing, value), std::ios_base::failure);
2052+
}
2053+
20062054
TEST_F(ScalarTemporalTest, StrftimeFormatSyntax) {
20072055
const auto type = timestamp(TimeUnit::MILLI, "UTC");
20082056
const char* input = R"(["1970-01-01T00:00:00.123", null])";

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

Lines changed: 178 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@
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+
2839
namespace arrow::compute::internal {
2940

3041
namespace 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\xB5s";
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+
166341
template <typename Duration>
167342
struct 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

Comments
 (0)