Skip to content

Commit

Permalink
Use $TZ for local time if it is set
Browse files Browse the repository at this point in the history
libstdc++ doesn't.
  • Loading branch information
hvenev committed Apr 9, 2024
1 parent 42dc9cb commit c5dcf04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/modules/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const time_zone*> tzList_; // time zones list
Expand Down
18 changes: 15 additions & 3 deletions src/modules/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<days>(zoned_time{current_zone(), system_clock::now()}.get_local_time())}
floor<days>(zoned_time{local_zone(), system_clock::now()}.get_local_time())}
.day();
} else
fmtMap_.insert({3, "{}"});
Expand Down Expand Up @@ -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<seconds>(system_clock::now())};

label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));
Expand Down Expand Up @@ -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<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}};
os << fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(zt)) << '\n';
}
Expand Down Expand Up @@ -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]) {
Expand Down

0 comments on commit c5dcf04

Please sign in to comment.