diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index c50b7ae5..e7c3872c 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -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) -> const std::string; + // get local time zone + auto local_zone() -> const time_zone*; + // time zoned time in tooltip const bool tzInTooltip_; // if need to print time zones text std::vector tzList_; // time zones list diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index e2cdf9fc..83537405 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -87,7 +87,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) fmtMap_.insert({3, config_[kCldPlaceholder]["format"]["today"].asString()}); cldBaseDay_ = year_month_day{ - floor(zoned_time{current_zone(), system_clock::now()}.get_local_time())} + floor(zoned_time{local_zone(), system_clock::now()}.get_local_time())} .day(); } else 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 { - 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(system_clock::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; for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) { if (static_cast(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}}; 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(); } +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 auto waybar::modules::Clock::doAction(const std::string& name) -> void { if (actionMap_[name]) {