|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +#include <limits> |
18 | 19 | #include <sstream> |
19 | 20 | #include <tuple> |
20 | 21 |
|
@@ -2033,6 +2034,32 @@ TEST(TimestampFormatterTest, ReuseFormatter) { |
2033 | 2034 | } |
2034 | 2035 | } |
2035 | 2036 |
|
| 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 | + |
2036 | 2063 | TEST(TimestampFormatterTest, StreamState) { |
2037 | 2064 | internal::TimestampFormatter<std::chrono::seconds> formatter{ |
2038 | 2065 | "%F %T", arrow::internal::OffsetZone{std::chrono::minutes{0}}, |
|
0 commit comments