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.
This commit is contained in:
Dirk-Jan C. Binnema 2023-08-05 10:51:13 +03:00
parent bdbf47c80f
commit 27c07280b1
2 changed files with 14 additions and 58 deletions

View File

@ -334,33 +334,7 @@ Mu::vformat(const char* frm, va_list args)
return str; return str;
} }
std::string static Option<::time_t>
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<int64_t>
delta_ymwdhMs(const std::string& expr) delta_ymwdhMs(const std::string& expr)
{ {
char* endptr; char* endptr;
@ -390,7 +364,7 @@ delta_ymwdhMs(const std::string& expr)
then = then =
g_date_time_add_full(now, -years, -months, -days, -hours, -minutes, -seconds); g_date_time_add_full(now, -years, -months, -days, -hours, -minutes, -seconds);
auto t = std::max<int64_t>(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(then);
g_date_time_unref(now); g_date_time_unref(now);
@ -398,7 +372,7 @@ delta_ymwdhMs(const std::string& expr)
return t; return t;
} }
static Option<int64_t> static Option<::time_t>
special_date_time(const std::string& d, bool is_first) special_date_time(const std::string& d, bool is_first)
{ {
if (d == "now") if (d == "now")
@ -467,12 +441,12 @@ fixup_month(struct tm* tbuf)
} }
Option<int64_t> Option<::time_t>
Mu::parse_date_time(const std::string& dstr, bool is_first) Mu::parse_date_time(const std::string& dstr, bool is_first)
{ {
struct tm tbuf{}; struct tm tbuf{};
GDateTime *dtime{}; GDateTime *dtime{};
int64_t t; ::time_t t;
/* one-sided dates */ /* one-sided dates */
if (dstr.empty()) 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); t = g_date_time_to_unix(dtime);
g_date_time_unref(dtime); g_date_time_unref(dtime);
return std::max<int64_t>(t, 0); return std::max<::time_t>(t, 0);
} }

View File

@ -43,6 +43,7 @@
#endif /*FMT_HEADER_ONLY*/ #endif /*FMT_HEADER_ONLY*/
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <fmt/chrono.h>
namespace Mu { namespace Mu {
@ -133,6 +134,11 @@ auto mu_join(Range&& range, std::string_view sepa) {
return fmt::join(std::forward<Range>(range), sepa); return fmt::join(std::forward<Range>(range), sepa);
} }
template <typename T>
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<std::string>; using StringVec = std::vector<std::string>;
@ -145,8 +151,7 @@ using StringVec = std::vector<std::string>;
*/ */
std::string utf8_flatten(const char* str); std::string utf8_flatten(const char* str);
inline std::string inline std::string
utf8_flatten(const std::string& s) utf8_flatten(const std::string& s) {
{
return utf8_flatten(s.c_str()); return utf8_flatten(s.c_str());
} }
@ -240,30 +245,7 @@ static inline bool mu_print_encoded(fmt::format_string<T...> frm, T&&... args) n
* *
* @return the corresponding time_t or Nothing if parsing failed. * @return the corresponding time_t or Nothing if parsing failed.
*/ */
Option<int64_t> parse_date_time(const std::string& date, bool first); Option<::time_t> 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;
/** /**
* Crudely convert HTML to plain text. This attempts to scrape the * Crudely convert HTML to plain text. This attempts to scrape the