mu-utils: use const char* from for time_to_string

To avoid lifetime problems with some(?) compilers.
This commit is contained in:
Dirk-Jan C. Binnema 2022-12-10 19:22:00 +02:00
parent 3b1d1edd46
commit 0fd9020726
2 changed files with 6 additions and 8 deletions

View File

@ -346,12 +346,9 @@ Mu::vformat(const char* frm, va_list args)
} }
std::string std::string
Mu::time_to_string(const std::string& frm_, time_t t, bool utc) Mu::time_to_string(const char *frm, time_t t, bool utc)
{ {
/* Temporary hack... https://github.com/djcb/mu/issues/2230 */ g_return_val_if_fail(frm, "");
const auto frm =
g_utf8_validate(frm_.c_str(), frm_.length(), {}) ?
frm_ : "%c";
GDateTime* dt = std::invoke([&] { GDateTime* dt = std::invoke([&] {
if (utc) if (utc)
@ -366,10 +363,11 @@ Mu::time_to_string(const std::string& frm_, time_t t, bool utc)
return {}; return {};
} }
auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm.c_str()))}; frm = frm ? frm : "%c";
auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm))};
g_date_time_unref(dt); g_date_time_unref(dt);
if (!datestr) if (!datestr)
g_warning("failed to format time with format '%s'", frm.c_str()); g_warning("failed to format time with format '%s'", frm);
return datestr.value_or(""); return datestr.value_or("");
} }

View File

@ -153,7 +153,7 @@ std::string date_to_time_t_string(int64_t t);
* @return a string representation of the time in UTF8-format, or empty in case * @return a string representation of the time in UTF8-format, or empty in case
* of error. * of error.
*/ */
std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST; std::string time_to_string(const char *frm, time_t t, bool utc = false) G_GNUC_CONST;
/** /**