mirror of https://github.com/djcb/mu.git
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:
parent
bdbf47c80f
commit
27c07280b1
|
@ -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<int64_t>
|
||||
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<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(now);
|
||||
|
@ -398,7 +372,7 @@ delta_ymwdhMs(const std::string& expr)
|
|||
return t;
|
||||
}
|
||||
|
||||
static Option<int64_t>
|
||||
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<int64_t>
|
||||
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<int64_t>(t, 0);
|
||||
return std::max<::time_t>(t, 0);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
#endif /*FMT_HEADER_ONLY*/
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/chrono.h>
|
||||
|
||||
namespace Mu {
|
||||
|
||||
|
@ -133,6 +134,11 @@ auto mu_join(Range&& range, std::string_view 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>;
|
||||
|
||||
|
@ -145,8 +151,7 @@ using StringVec = std::vector<std::string>;
|
|||
*/
|
||||
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<T...> frm, T&&... args) n
|
|||
*
|
||||
* @return the corresponding time_t or Nothing if parsing failed.
|
||||
*/
|
||||
Option<int64_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;
|
||||
Option<::time_t> parse_date_time(const std::string& date, bool first);
|
||||
|
||||
/**
|
||||
* Crudely convert HTML to plain text. This attempts to scrape the
|
||||
|
|
Loading…
Reference in New Issue