38#include <gtest/gtest.h>
50std::tm checked_localtime(std::time_t value)
52 std::tm *tm_ptr = std::localtime(&value);
53 if (tm_ptr ==
nullptr)
54 throw std::runtime_error(
"localtime failed during test");
60TEST(AhDateTest, LeapYearDetection)
68TEST(AhDateTest, DayValidationHandlesMonthLengths)
77TEST(AhDateTest, ToTimeTTripleRoundTrip)
80 const std::tm tm = checked_localtime(ts);
82 EXPECT_EQ(tm.tm_mday, 25);
83 EXPECT_EQ(tm.tm_mon + 1, 12);
84 EXPECT_EQ(tm.tm_year + 1900, 2022);
87TEST(AhDateTest, ToTimeTTripleRejectsInvalidDates)
93TEST(AhDateTest, ToTimeTFromStringValidatesFormat)
96 const std::tm tm = checked_localtime(ts);
98 EXPECT_EQ(tm.tm_year + 1900, 2024);
99 EXPECT_EQ(tm.tm_mon + 1, 5);
100 EXPECT_EQ(tm.tm_mday, 10);
101 EXPECT_EQ(tm.tm_hour, 12);
102 EXPECT_EQ(tm.tm_min, 34);
103 EXPECT_EQ(tm.tm_sec, 56);
109TEST(AhDateTest, ToDaysRequiresNonNegativeTimestamps)
111 constexpr std::time_t horizon =
static_cast<std::time_t
>(5 * 24 * 60 * 60);
113 EXPECT_THROW(
Aleph::to_days(
static_cast<std::time_t
>(-1)), std::domain_error);
116TEST(AhDateTest, ToStringFormatsAndDetectsOverflow)
121 const std::string oversized_format(256,
'X');
Lightweight helpers for validating and formatting civil dates.
size_t to_days(const time_t t)
Return the number of whole days represented by t.
bool is_leap_year(const size_t yy) noexcept
Return true if the provided year is a leap year.
bool valid_day(const size_t yy, const size_t mm, const size_t dd) noexcept
Check whether a given day exists for the supplied month/year.
time_t to_time_t(const size_t dd, const size_t mm, const size_t yy)
Convert a (day, month, year) triple to a POSIX time_t value.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.