From 27c07280b15d0e972026771457b8db4580b09ee1 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 5 Aug 2023 10:51:13 +0300 Subject: [PATCH] utils: replace time_to_string with fmt-based formatting It's faster; make "mu find" ~5-10% faster, and removes some code we no longer need. --- lib/utils/mu-utils.cc | 38 ++++++-------------------------------- lib/utils/mu-utils.hh | 34 ++++++++-------------------------- 2 files changed, 14 insertions(+), 58 deletions(-) diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index 242ab020..99c9a971 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -334,33 +334,7 @@ Mu::vformat(const char* frm, va_list args) return str; } -std::string -Mu::time_to_string(const char *frm, time_t t, bool utc) -{ - g_return_val_if_fail(frm, ""); - - GDateTime* dt = std::invoke([&] { - if (utc) - return g_date_time_new_from_unix_utc(t); - else - return g_date_time_new_from_unix_local(t); - }); - - if (!dt) { - mu_warning("time_t out of range: <{}>", t); - return {}; - } - - frm = frm ? frm : "%c"; - auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm))}; - g_date_time_unref(dt); - if (!datestr) - mu_warning("failed to format time with format '{}'", frm); - - return datestr.value_or(""); -} - -static Option +static Option<::time_t> delta_ymwdhMs(const std::string& expr) { char* endptr; @@ -390,7 +364,7 @@ delta_ymwdhMs(const std::string& expr) then = g_date_time_add_full(now, -years, -months, -days, -hours, -minutes, -seconds); - auto t = std::max(0, g_date_time_to_unix(then)); + auto t = std::max<::time_t>(0, g_date_time_to_unix(then)); g_date_time_unref(then); g_date_time_unref(now); @@ -398,7 +372,7 @@ delta_ymwdhMs(const std::string& expr) return t; } -static Option +static Option<::time_t> special_date_time(const std::string& d, bool is_first) { if (d == "now") @@ -467,12 +441,12 @@ fixup_month(struct tm* tbuf) } -Option +Option<::time_t> Mu::parse_date_time(const std::string& dstr, bool is_first) { struct tm tbuf{}; GDateTime *dtime{}; - int64_t t; + ::time_t t; /* one-sided dates */ if (dstr.empty()) @@ -506,7 +480,7 @@ Mu::parse_date_time(const std::string& dstr, bool is_first) t = g_date_time_to_unix(dtime); g_date_time_unref(dtime); - return std::max(t, 0); + return std::max<::time_t>(t, 0); } diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 5247c74b..0b7543fd 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -43,6 +43,7 @@ #endif /*FMT_HEADER_ONLY*/ #include #include +#include namespace Mu { @@ -133,6 +134,11 @@ auto mu_join(Range&& range, std::string_view sepa) { return fmt::join(std::forward(range), sepa); } +template +auto mu_time(T t, bool use_utc=false) { + ::time_t tt{static_cast<::time_t>(t)}; + return use_utc ? fmt::gmtime(tt) : fmt::localtime(tt); +} using StringVec = std::vector; @@ -145,8 +151,7 @@ using StringVec = std::vector; */ std::string utf8_flatten(const char* str); inline std::string -utf8_flatten(const std::string& s) -{ +utf8_flatten(const std::string& s) { return utf8_flatten(s.c_str()); } @@ -240,30 +245,7 @@ static inline bool mu_print_encoded(fmt::format_string frm, T&&... args) n * * @return the corresponding time_t or Nothing if parsing failed. */ -Option parse_date_time(const std::string& date, bool first); - -/** - * 64-bit incarnation of time_t expressed as a 10-digit string. Uses 64-bit for the time-value, - * regardless of the size of time_t. - * - * @param t some time value - * - * @return - */ -std::string date_to_time_t_string(int64_t t); - -/** - * Get a string for a given time_t and format - * memory that must be freed after use. - * - * @param frm the format of the string (in strftime(3) format) - * @param t the time as time_t - * @param utc whether to display as UTC(if true) or local time - * - * @return a string representation of the time in UTF8-format, or empty in case - * of error. - */ -std::string time_to_string(const char *frm, time_t t, bool utc = false) G_GNUC_CONST; +Option<::time_t> parse_date_time(const std::string& date, bool first); /** * Crudely convert HTML to plain text. This attempts to scrape the