From 0fd90207268ee37fbf1bbbc3a7851b287712fd29 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 10 Dec 2022 19:22:00 +0200 Subject: [PATCH] mu-utils: use const char* from for time_to_string To avoid lifetime problems with some(?) compilers. --- lib/utils/mu-utils.cc | 12 +++++------- lib/utils/mu-utils.hh | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index f32eed72..6a88788c 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -346,12 +346,9 @@ Mu::vformat(const char* frm, va_list args) } 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 */ - const auto frm = - g_utf8_validate(frm_.c_str(), frm_.length(), {}) ? - frm_ : "%c"; + g_return_val_if_fail(frm, ""); GDateTime* dt = std::invoke([&] { if (utc) @@ -366,10 +363,11 @@ Mu::time_to_string(const std::string& frm_, time_t t, bool utc) 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); 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(""); } diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index dd38416c..4a9f70d6 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -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 * 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; /**