mu-utils: update casting from int64_t -> time_t

Esp. for systems with 32-bit time_t.
This commit is contained in:
Dirk-Jan C. Binnema 2024-04-13 20:06:39 +03:00
parent d3e7b9cf05
commit a6ec43a740
2 changed files with 22 additions and 3 deletions

View File

@ -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);
}

View File

@ -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<T...> 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<int64_t>(time_t_min),
static_cast<int64_t>(time_t_max));
}
/**
* Parse a date string to the corresponding time_t
* *