From 6de3b58d6aa7b17271da9c637a7ac6cbf92c49ba Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Thu, 13 Apr 2023 17:16:03 +0300 Subject: [PATCH] ISSUE#2073 Signed-off-by: Viktar Lukashonak --- include/modules/clock.hpp | 1 + src/modules/clock.cpp | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index 999c843..fab3811 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -50,6 +50,7 @@ class Clock final : public ALabel { date::months cldShift_{0}; std::string cldYearCached_{}; std::string cldMonCached_{}; + date::day cldBaseDay_{0}; /*Calendar functions*/ auto get_calendar(const date::zoned_seconds& now, const date::zoned_seconds& wtime) -> std::string; diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 2edd21b..7e351d4 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -93,9 +93,11 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) fmtMap_.insert({1, config_[kCalendarPlaceholder]["format"]["weekdays"].asString()}); else fmtMap_.insert({1, "{}"}); - if (config_[kCalendarPlaceholder]["format"]["today"].isString()) + if (config_[kCalendarPlaceholder]["format"]["today"].isString()) { fmtMap_.insert({3, config_[kCalendarPlaceholder]["format"]["today"].asString()}); - else + cldBaseDay_ = + date::year_month_day{date::floor(std::chrono::system_clock::now())}.day(); + } else fmtMap_.insert({3, "{}"}); if (config_[kCalendarPlaceholder]["mode"].isString()) { const std::string cfgMode{(config_[kCalendarPlaceholder]["mode"].isString()) @@ -315,6 +317,7 @@ auto waybar::modules::Clock::get_calendar(const date::zoned_seconds& now, const auto ymd{date::year_month_day{daypoint}}; const auto ym{ymd.year() / ymd.month()}; const auto y{ymd.year()}; + const auto d{ymd.day()}; const auto firstdow = first_day_of_week(); const auto maxRows{12 / cldMonCols_}; std::ostringstream os; @@ -325,13 +328,19 @@ auto waybar::modules::Clock::get_calendar(const date::zoned_seconds& now, if (cldMode_ == CldMode::YEAR) { if (y / date::month{1} / 1 == cldYearShift_) - return cldYearCached_; + if (d == cldBaseDay_ || (uint)cldBaseDay_ == 0u) + return cldYearCached_; + else + cldBaseDay_ = d; else cldYearShift_ = y / date::month{1} / 1; } if (cldMode_ == CldMode::MONTH) { if (ym == cldMonShift_) - return cldMonCached_; + if (d == cldBaseDay_ || (uint)cldBaseDay_ == 0u) + return cldMonCached_; + else + cldBaseDay_ = d; else cldMonShift_ = ym; }