diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index d3fea3f6..6d36dc1a 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -489,7 +489,7 @@ Mu::parse_date_time(const std::string& dstr, bool is_first, bool utc) { struct tm tbuf{}; GDateTime *dtime{}; - ::time_t t; + gint64 t; /* one-sided dates */ if (dstr.empty()) @@ -531,7 +531,7 @@ Mu::parse_date_time(const std::string& dstr, bool is_first, bool utc) t = g_date_time_to_unix(dtime); g_date_time_unref(dtime); - return std::max<::time_t>(t, 0); + return to_time_t(t); } diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index cef84b4f..783351f8 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -36,7 +36,6 @@ #include "mu-option.hh" - #ifndef FMT_HEADER_ONLY #define FMT_HEADER_ONLY #endif /*FMT_HEADER_ONLY*/ @@ -268,6 +267,26 @@ static inline bool mu_print_encoded(fmt::format_string frm, T&&... args) n stdout); } +/** + * Convert an int64_t to a time_t, clamping it within the range. + * + * This is only doing anything when using a 32-bit time_t value. This doesn't + * solve the 3038 problem, but at least allows for clearly marking where we + * convert + * + * @param t some 64-bit value that encodes a Unix time. + * + * @return a time_t value + */ +constexpr ::time_t time_t_min = 0; +constexpr ::time_t time_t_max = std::numeric_limits<::time_t>::max(); +constexpr ::time_t to_time_t(int64_t t) { + return std::clamp(t, + static_cast(time_t_min), + static_cast(time_t_max)); +} + + /** * Parse a date string to the corresponding time_t * *