Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets: add TZ env var support for $TIME #627

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions src/renderer/widgets/IWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "../../helpers/Log.hpp"
#include "../../core/hyprlock.hpp"
#include "../../auth/Auth.hpp"
#include "../../auth/Fingerprint.hpp"
#include <chrono>
#include <unistd.h>
#include <pwd.h>
Expand Down Expand Up @@ -120,43 +119,46 @@ static void replaceAllLayout(std::string& str) {
}

static bool logMissingTzOnce = true;
static std::string getTime() {
const auto PCURRENTTZ = std::chrono::current_zone();
const auto TPNOW = std::chrono::system_clock::now();
static std::chrono::hh_mm_ss<std::chrono::system_clock::duration> getTime() {
const std::chrono::time_zone* pCurrentTz = nullptr;
try {
auto name = std::getenv("TZ");
if (name)
pCurrentTz = std::chrono::locate_zone(name);
} catch (std::runtime_error&) {
Debug::log(WARN, "Invalid TZ value. Falling back to current timezone!");
}

if (!pCurrentTz)
pCurrentTz = std::chrono::current_zone();

const auto TPNOW = std::chrono::system_clock::now();

//
std::chrono::hh_mm_ss<std::chrono::system_clock::duration> hhmmss;
if (!PCURRENTTZ) {
if (!pCurrentTz) {
if (logMissingTzOnce) {
Debug::log(WARN, "Current timezone unknown for $TIME. Falling back to UTC!");
Debug::log(WARN, "Current timezone unknown. Falling back to UTC!");
logMissingTzOnce = false;
}
hhmmss = std::chrono::hh_mm_ss{TPNOW - std::chrono::floor<std::chrono::days>(TPNOW)};
} else
hhmmss = std::chrono::hh_mm_ss{PCURRENTTZ->to_local(TPNOW) - std::chrono::floor<std::chrono::days>(PCURRENTTZ->to_local(TPNOW))};
hhmmss = std::chrono::hh_mm_ss{pCurrentTz->to_local(TPNOW) - std::chrono::floor<std::chrono::days>(pCurrentTz->to_local(TPNOW))};

const auto HRS = hhmmss.hours().count();
const auto MINS = hhmmss.minutes().count();
return hhmmss;
}

static std::string getTime24h() {
const auto HHMMSS = getTime();
const auto HRS = HHMMSS.hours().count();
const auto MINS = HHMMSS.minutes().count();
return (HRS < 10 ? "0" : "") + std::to_string(HRS) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS);
}

static std::string getTime12h() {
const auto PCURRENTTZ = std::chrono::current_zone();
const auto TPNOW = std::chrono::system_clock::now();

//
std::chrono::hh_mm_ss<std::chrono::system_clock::duration> hhmmss;
if (!PCURRENTTZ) {
if (logMissingTzOnce) {
Debug::log(WARN, "Current timezone unknown for $TIME12. Falling back to UTC!");
logMissingTzOnce = false;
}
hhmmss = std::chrono::hh_mm_ss{TPNOW - std::chrono::floor<std::chrono::days>(TPNOW)};
} else
hhmmss = std::chrono::hh_mm_ss{PCURRENTTZ->to_local(TPNOW) - std::chrono::floor<std::chrono::days>(PCURRENTTZ->to_local(TPNOW))};

const auto HRS = hhmmss.hours().count();
const auto MINS = hhmmss.minutes().count();
const auto HHMMSS = getTime();
const auto HRS = HHMMSS.hours().count();
const auto MINS = HHMMSS.minutes().count();
return (HRS == 12 || HRS == 0 ? "12" : (HRS % 12 < 10 ? "0" : "") + std::to_string(HRS % 12)) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS) +
(HRS < 12 ? " AM" : " PM");
}
Expand Down Expand Up @@ -184,7 +186,7 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
}

if (in.contains("$TIME")) {
replaceInString(in, "$TIME", getTime());
replaceInString(in, "$TIME", getTime24h());
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;
}

Expand Down
Loading