widgets: check current_zone pointer (#527)

* widgets: check current_zone pointer and fallback to utc

* misc: remove redundant printf in RASSERT

* widgets: no curly else
This commit is contained in:
Maximilian Seidler 2024-10-26 20:27:24 +02:00 committed by GitHub
parent ae3bb0fd43
commit 29dd33d6a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 11 deletions

View file

@ -18,7 +18,6 @@ enum eLogLevel {
Debug::log(CRIT, "\n==========================================================================================\nASSERTION FAILED! \n\n{}\n\nat: line {} in {}", \ Debug::log(CRIT, "\n==========================================================================================\nASSERTION FAILED! \n\n{}\n\nat: line {} in {}", \
std::format(reason, ##__VA_ARGS__), __LINE__, \ std::format(reason, ##__VA_ARGS__), __LINE__, \
([]() constexpr -> std::string { return std::string(__FILE__).substr(std::string(__FILE__).find_last_of('/') + 1); })().c_str()); \ ([]() constexpr -> std::string { return std::string(__FILE__).substr(std::string(__FILE__).find_last_of('/') + 1); })().c_str()); \
printf("Assertion failed! See the log in /tmp/hypr/hyprland.log for more info."); \
std::abort(); \ std::abort(); \
} }

View file

@ -100,21 +100,44 @@ static void replaceAllLayout(std::string& str) {
} }
} }
static bool logMissingTzOnce = true;
static std::string getTime() { static std::string getTime() {
const auto current_zone = std::chrono::current_zone(); const auto PCURRENTTZ = (true) ? nullptr : std::chrono::current_zone();
const auto HHMMSS = std::chrono::hh_mm_ss{current_zone->to_local(std::chrono::system_clock::now()) - const auto TPNOW = 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(); std::chrono::hh_mm_ss<std::chrono::system_clock::duration> hhmmss;
if (!PCURRENTTZ) {
if (logMissingTzOnce) {
Debug::log(WARN, "Current timezone unknown for $TIME. 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();
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() { static std::string getTime12h() {
const auto current_zone = std::chrono::current_zone(); const auto PCURRENTTZ = std::chrono::current_zone();
const auto HHMMSS = std::chrono::hh_mm_ss{current_zone->to_local(std::chrono::system_clock::now()) - const auto TPNOW = 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(); 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();
return (HRS % 12 < 10 ? "0" : "") + std::to_string(HRS % 12) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS) + (HRS < 12 ? " AM" : " PM"); return (HRS % 12 < 10 ? "0" : "") + std::to_string(HRS % 12) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS) + (HRS < 12 ? " AM" : " PM");
} }