Use $TZ for local time if it is set

libstdc++ doesn't.
master
Hristo Venev 2024-04-09 20:33:53 +03:00
parent 42dc9cb85f
commit f68ac9119a
2 changed files with 18 additions and 3 deletions

View File

@ -52,6 +52,9 @@ class Clock final : public ALabel {
auto get_calendar(const year_month_day& today, const year_month_day& ymd, const time_zone* tz) auto get_calendar(const year_month_day& today, const year_month_day& ymd, const time_zone* tz)
-> const std::string; -> const std::string;
// get local time zone
auto local_zone() -> const time_zone*;
// time zoned time in tooltip // time zoned time in tooltip
const bool tzInTooltip_; // if need to print time zones text const bool tzInTooltip_; // if need to print time zones text
std::vector<const time_zone*> tzList_; // time zones list std::vector<const time_zone*> tzList_; // time zones list

View File

@ -87,7 +87,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
fmtMap_.insert({3, config_[kCldPlaceholder]["format"]["today"].asString()}); fmtMap_.insert({3, config_[kCldPlaceholder]["format"]["today"].asString()});
cldBaseDay_ = cldBaseDay_ =
year_month_day{ year_month_day{
floor<days>(zoned_time{current_zone(), system_clock::now()}.get_local_time())} floor<days>(zoned_time{local_zone(), system_clock::now()}.get_local_time())}
.day(); .day();
} else } else
fmtMap_.insert({3, "{}"}); fmtMap_.insert({3, "{}"});
@ -131,7 +131,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
} }
auto waybar::modules::Clock::update() -> void { auto waybar::modules::Clock::update() -> void {
const auto* tz = tzList_[tzCurrIdx_] != nullptr ? tzList_[tzCurrIdx_] : current_zone(); const auto* tz = tzList_[tzCurrIdx_] != nullptr ? tzList_[tzCurrIdx_] : local_zone();
const zoned_time now{tz, floor<seconds>(system_clock::now())}; const zoned_time now{tz, floor<seconds>(system_clock::now())};
label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now))); label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));
@ -168,7 +168,7 @@ auto waybar::modules::Clock::getTZtext(sys_seconds now) -> std::string {
std::stringstream os; std::stringstream os;
for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) { for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) {
if (static_cast<int>(tz_idx) == tzCurrIdx_) continue; if (static_cast<int>(tz_idx) == tzCurrIdx_) continue;
const auto* tz = tzList_[tz_idx] != nullptr ? tzList_[tz_idx] : current_zone(); const auto* tz = tzList_[tz_idx] != nullptr ? tzList_[tz_idx] : local_zone();
auto zt{zoned_time{tz, now}}; auto zt{zoned_time{tz, now}};
os << fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(zt)) << '\n'; os << fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(zt)) << '\n';
} }
@ -393,6 +393,18 @@ auto waybar::modules::Clock::get_calendar(const year_month_day& today, const yea
return os.str(); return os.str();
} }
auto waybar::modules::Clock::local_zone() -> const time_zone* {
const char* tz_name = getenv("TZ");
if (tz_name) {
try {
return locate_zone(tz_name);
} catch (const std::runtime_error& e) {
spdlog::warn("Timezone: {0}. {1}", tz_name, e.what());
}
}
return current_zone();
}
// Actions handler // Actions handler
auto waybar::modules::Clock::doAction(const std::string& name) -> void { auto waybar::modules::Clock::doAction(const std::string& name) -> void {
if (actionMap_[name]) { if (actionMap_[name]) {