Skip to content

Commit 6e9782e

Browse files
committed
apacheGH-51215: [C++] Avoid overflow in timestamp time-of-day formatting
1 parent 14c50d8 commit 6e9782e

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

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

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

@@ -2033,6 +2034,32 @@ TEST(TimestampFormatterTest, ReuseFormatter) {
20332034
}
20342035
}
20352036

2037+
TEST(TimestampFormatterTest, NanosecondRangeLimits) {
2038+
const arrow::internal::OffsetZone zone{std::chrono::minutes{0}};
2039+
internal::TimestampFormatter<std::chrono::nanoseconds> formatter{
2040+
"%F %T %Q %q", zone, std::locale::classic()};
2041+
for (const auto& [count, expected] :
2042+
{std::pair{std::numeric_limits<int64_t>::min() + 1000000000,
2043+
"1677-09-21 00:12:44.145224192 764145224192 ns"},
2044+
std::pair{int64_t{-86400000000000}, "1969-12-31 00:00:00.000000000 0 ns"},
2045+
std::pair{int64_t{-1}, "1969-12-31 23:59:59.999999999 86399999999999 ns"},
2046+
std::pair{int64_t{0}, "1970-01-01 00:00:00.000000000 0 ns"},
2047+
std::pair{int64_t{1}, "1970-01-01 00:00:00.000000001 1 ns"},
2048+
std::pair{std::numeric_limits<int64_t>::max(),
2049+
"2262-04-11 23:47:16.854775807 85636854775807 ns"}}) {
2050+
SCOPED_TRACE(count);
2051+
ASSERT_OK_AND_ASSIGN(auto result, formatter(count));
2052+
EXPECT_EQ(result, expected);
2053+
}
2054+
2055+
// Formatting calendar fields at the exact lower bound can overflow within
2056+
// standard-library formatters. Test the time-of-day directives independently.
2057+
internal::TimestampFormatter<std::chrono::nanoseconds> count_formatter{
2058+
"%Q %q", zone, std::locale::classic()};
2059+
ASSERT_OK_AND_ASSIGN(auto result, count_formatter(std::numeric_limits<int64_t>::min()));
2060+
EXPECT_EQ(result, "763145224192 ns");
2061+
}
2062+
20362063
TEST(TimestampFormatterTest, StreamState) {
20372064
internal::TimestampFormatter<std::chrono::seconds> formatter{
20382065
"%F %T", arrow::internal::OffsetZone{std::chrono::minutes{0}},

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,13 @@ struct TimestampFormatter {
354354
#if ARROW_USE_STD_CHRONO
355355
// %Q/%q refer to local time of day rather than elapsed time since the epoch.
356356
const auto local_time = zt.get_local_time();
357-
const auto local_day = std::chrono::floor<std::chrono::days>(local_time);
358-
const auto time_of_day = local_time - local_day;
357+
// Truncate toward zero, as in StringFormatter<TimestampType>, so midnight
358+
// remains representable even near the lower bound of nanosecond timestamps.
359+
const auto local_day =
360+
std::chrono::time_point_cast<std::chrono::days>(local_time);
361+
const auto time_of_day = local_day <= local_time
362+
? local_time - local_day
363+
: std::chrono::days{1} - (local_day - local_time);
359364
const auto time_of_day_count = time_of_day.count();
360365
const std::ostream::sentry sentry(bufstream);
361366
if (sentry) {

0 commit comments

Comments
 (0)