From d9c2a5e0b790bfd1bdc7ab39ce188bcc5519b1b4 Mon Sep 17 00:00:00 2001 From: davc0n Date: Mon, 30 Sep 2024 13:22:24 +0200 Subject: [PATCH] widgets: add 12h time format (#500) --- src/renderer/widgets/IWidget.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/renderer/widgets/IWidget.cpp b/src/renderer/widgets/IWidget.cpp index 776fb1c..0cb8f93 100644 --- a/src/renderer/widgets/IWidget.cpp +++ b/src/renderer/widgets/IWidget.cpp @@ -111,6 +111,15 @@ static std::string getTime() { return (HRS < 10 ? "0" : "") + std::to_string(HRS) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS); } +static std::string getTime12h() { + const auto current_zone = std::chrono::current_zone(); + const auto HHMMSS = std::chrono::hh_mm_ss{current_zone->to_local(std::chrono::system_clock::now()) - + std::chrono::floor(current_zone->to_local(std::chrono::system_clock::now()))}; + const auto HRS = HHMMSS.hours().count(); + const auto MINS = HHMMSS.minutes().count(); + return (HRS % 12 < 10 ? "0" : "") + std::to_string(HRS % 12) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS) + (HRS < 12 ? " AM" : " PM"); +} + IWidget::SFormatResult IWidget::formatString(std::string in) { auto uidPassword = getpwuid(getuid()); @@ -128,6 +137,11 @@ IWidget::SFormatResult IWidget::formatString(std::string in) { replaceAll(in, "$USER", std::string{username ? username : ""}); replaceAll(in, "
", std::string{"\n"}); + if (in.contains("$TIME12")) { + replaceAll(in, "$TIME12", getTime12h()); + result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000; + } + if (in.contains("$TIME")) { replaceAll(in, "$TIME", getTime()); result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;