| 489 | } |
| 490 | |
| 491 | void testTmToString( |
| 492 | const std::string& format, |
| 493 | const TimestampToStringOptions::Mode mode) { |
| 494 | uint64_t seed = 42; |
| 495 | std::default_random_engine gen(seed); |
| 496 | |
| 497 | std::uniform_int_distribution<time_t> dist( |
| 498 | std::numeric_limits<time_t>::min(), std::numeric_limits<time_t>::max()); |
| 499 | std::uniform_int_distribution<int> nanosDist(0, Timestamp::kMaxNanos); |
| 500 | |
| 501 | std::tm actual{}; |
| 502 | std::tm expected{}; |
| 503 | |
| 504 | TimestampToStringOptions options; |
| 505 | options.mode = mode; |
| 506 | |
| 507 | const std::vector<TimestampToStringOptions::Precision> precisions = { |
| 508 | TimestampToStringOptions::Precision::kMilliseconds, |
| 509 | TimestampToStringOptions::Precision::kNanoseconds}; |
| 510 | |
| 511 | for (auto precision : precisions) { |
| 512 | options.precision = precision; |
| 513 | for (int i = 0; i < 10'000; ++i) { |
| 514 | auto epoch = dist(gen); |
| 515 | auto nanos = nanosDist(gen); |
| 516 | SCOPED_TRACE(fmt::format( |
| 517 | "epoch={}, nanos={}, mode={}, precision={}", |
| 518 | epoch, |
| 519 | nanos, |
| 520 | mode, |
| 521 | precision)); |
| 522 | if (gmtime_r(&epoch, &expected)) { |
| 523 | ASSERT_TRUE(Timestamp::epochToUtc(epoch, actual)); |
| 524 | checkTm(actual, expected); |
| 525 | |
| 526 | auto actualString = Timestamp::tmToString(actual, nanos, options); |
| 527 | auto expectedString = tmToString(expected, nanos, format, options); |
| 528 | ASSERT_EQ(expectedString, actualString); |
| 529 | |
| 530 | } else { |
| 531 | ASSERT_FALSE(Timestamp::epochToUtc(epoch, actual)); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | TEST(TimestampTest, tmToStringDateOnly) { |
| 538 | // %F - equivalent to "%Y-%m-%d" (the ISO 8601 date format) |
no test coverage detected