widgets: add 12h time format (#500)

This commit is contained in:
davc0n 2024-09-30 13:22:24 +02:00 committed by GitHub
parent 9ea804788c
commit d9c2a5e0b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -111,6 +111,15 @@ static std::string getTime() {
return (HRS < 10 ? "0" : "") + std::to_string(HRS) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS); 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<std::chrono::days>(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) { IWidget::SFormatResult IWidget::formatString(std::string in) {
auto uidPassword = getpwuid(getuid()); auto uidPassword = getpwuid(getuid());
@ -128,6 +137,11 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
replaceAll(in, "$USER", std::string{username ? username : ""}); replaceAll(in, "$USER", std::string{username ? username : ""});
replaceAll(in, "<br/>", std::string{"\n"}); replaceAll(in, "<br/>", 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")) { if (in.contains("$TIME")) {
replaceAll(in, "$TIME", getTime()); replaceAll(in, "$TIME", getTime());
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000; result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;