59 static_cast<size_t>(std::numeric_limits<int>::max()) +
tm_epoch_year;
61 static_cast<time_t
>(24 * 60 * 60);
67 return (yy % 4 == 0
and yy % 100 != 0) or (yy % 400 == 0);
71 [[nodiscard]]
inline bool valid_month(
const size_t mm)
noexcept
73 return mm >= 1
and mm <= 12;
77 [[nodiscard]]
inline bool valid_day(
const size_t yy,
const size_t mm,
const size_t dd)
noexcept
82 static constexpr std::array<size_t, 12> month_lengths =
83 {31u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};
85 const size_t base = month_lengths[mm - 1];
88 return dd <= base + extra;
92 inline time_t
to_time_t(
const size_t dd,
const size_t mm,
const size_t yy)
96 <<
"Year " << yy <<
" is outside the supported range ["
100 <<
"Invalid date components"
101 <<
" (dd=" << dd <<
", mm=" << mm <<
", yy=" << yy <<
')';
104 tm.tm_mday =
static_cast<int>(dd);
105 tm.tm_mon =
static_cast<int>(mm) - 1;
106 tm.tm_year =
static_cast<int>(yy) - 1900;
109 return std::mktime(&tm);
113 inline time_t
to_time_t(
const std::string & s,
const std::string & format)
116 const char *res = ::strptime(s.c_str(), format.c_str(), &tm);
118 <<
"Input string '" << s <<
"' does not match format '" << format <<
'\'';
121 return std::mktime(&tm);
127 return to_time_t(s,
"%Y-%m-%d %H:%M:%S");
134 <<
"to_days() expects non-negative timestamps";
140 inline std::string
to_string(
const time_t t,
const std::string & format)
142 const std::tm *tm_ptr = std::localtime(&t);
145 std::tm tm = *tm_ptr;
147 const size_t written = std::strftime(buff,
sizeof(buff), format.c_str(), &tm);
149 <<
"Unable to format time using pattern '" << format <<
"'";
151 return std::string(buff, written);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
constexpr size_t tm_epoch_year
constexpr time_t seconds_per_day
constexpr size_t max_supported_year
Main namespace for Aleph-w library functions.
size_t to_days(const time_t t)
Return the number of whole days represented by t.
bool valid_month(const size_t mm) noexcept
Check whether the month number is within the [1, 12] range.
bool is_leap_year(const size_t yy) noexcept
Return true if the provided year is a leap year.
and
Check uniqueness with explicit hash + equality functors.
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.