Skip to content

Commit

Permalink
widgets: check current_zone pointer and fallback to utc
Browse files Browse the repository at this point in the history
  • Loading branch information
PaideiaDilemma committed Oct 25, 2024
1 parent a093a9e commit ff2b3d6
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/renderer/widgets/IWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,46 @@ static void replaceAllLayout(std::string& str) {
}
}

static bool logMissingTzOnce = true;
static std::string getTime() {
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();
const auto PCURRENTTZ = (true) ? nullptr : 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 $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);
}

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();
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();
return (HRS % 12 < 10 ? "0" : "") + std::to_string(HRS % 12) + ":" + (MINS < 10 ? "0" : "") + std::to_string(MINS) + (HRS < 12 ? " AM" : " PM");
}

Expand Down

0 comments on commit ff2b3d6

Please sign in to comment.