From 4b1be6f88f9043f6d7829c397bcba88639b8491f Mon Sep 17 00:00:00 2001 From: Marvin W Date: Tue, 11 Mar 2025 13:53:58 +0100 Subject: Improve 24h time format detection --- main/src/ui/util/helper.vala | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'main/src/ui') diff --git a/main/src/ui/util/helper.vala b/main/src/ui/util/helper.vala index 578e313a..60e0e81c 100644 --- a/main/src/ui/util/helper.vala +++ b/main/src/ui/util/helper.vala @@ -134,12 +134,16 @@ public static bool is_dark_theme(Gtk.Widget widget) { return (bg.red > 0.5 && bg.green > 0.5 && bg.blue > 0.5); } -private static uint8 is24h = 0; +private static int8 is24h = 0; public static bool is_24h_format() { if (is24h == 0) { - string p_format = " "; // Leaving room to be filled by strftime - Time.local(0).strftime((char[]) p_format.data, "%p"); - is24h = p_format.strip() == "" ? 1 : -1; + Regex has_ampm = /(^|[^%])%[pP]/; + Regex has_t_fmt_ampm = /(^|[^%])%r/; + unowned string t_fmt = Posix.nl_langinfo(Posix.NLItem.T_FMT); + unowned string t_fmt_ampm = Posix.nl_langinfo(Posix.NLItem.T_FMT_AMPM); + bool has_am_str = Posix.nl_langinfo(Posix.NLItem.AM_STR).strip() != ""; + bool has_pm_str = Posix.nl_langinfo(Posix.NLItem.PM_STR).strip() != ""; + is24h = ((has_ampm.match(t_fmt) || has_t_fmt_ampm.match(t_fmt) && has_ampm.match(t_fmt_ampm)) && (has_am_str || has_pm_str)) ? -1 : 1; } return is24h == 1; } -- cgit v1.3