From cef5b27b485ddaa84fab9e2afdce2878dce9809a Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 22 May 2020 18:52:26 +0200 Subject: [PATCH 1/3] fix: prefer default fmt date formatting unless timezone specified --- src/modules/clock.cpp | 49 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 20aa1ce0..c9f09a3a 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,4 +1,7 @@ #include "modules/clock.hpp" + +#include + #include #include #ifdef HAVE_LANGINFO_1STDAY @@ -9,9 +12,7 @@ using waybar::modules::waybar_time; waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) - : ALabel(config, "clock", id, "{:%H:%M}", 60) - , fixed_time_zone_(false) -{ + : ALabel(config, "clock", id, "{:%H:%M}", 60), fixed_time_zone_(false) { if (config_["timezone"].isString()) { time_zone_ = date::locate_zone(config_["timezone"].asString()); fixed_time_zone_ = true; @@ -37,18 +38,27 @@ auto waybar::modules::Clock::update() -> void { // Time zone can change. Be sure to pick that. time_zone_ = date::current_zone(); } + auto now = std::chrono::system_clock::now(); waybar_time wtime = {locale_, date::make_zoned(time_zone_, date::floor(now))}; - auto text = fmt::format(format_, wtime); - label_.set_markup(text); + if (!fixed_time_zone_) { + // As date dep is not fully compatible, prefer fmt + tzset(); + auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now)); + auto text = fmt::format(format_, localtime); + label_.set_markup(text); + } else { + auto text = fmt::format(format_, wtime); + label_.set_markup(text); + } if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { const auto calendar = calendar_text(wtime); - auto tooltip_format = config_["tooltip-format"].asString(); - auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar)); + auto tooltip_format = config_["tooltip-format"].asString(); + auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar)); label_.set_tooltip_markup(tooltip_text); } else { label_.set_tooltip_markup(text); @@ -66,19 +76,19 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str } const date::year_month ym(ymd.year(), ymd.month()); - const auto curr_day = ymd.day(); + const auto curr_day = ymd.day(); std::stringstream os; - const auto first_dow = first_day_of_week(); + const auto first_dow = first_day_of_week(); weekdays_header(first_dow, os); // First week prefixed with spaces if needed. - auto wd = date::weekday(ym/1); + auto wd = date::weekday(ym / 1); auto empty_days = (wd - first_dow).count(); if (empty_days > 0) { os << std::string(empty_days * 3 - 1, ' '); } - auto last_day = (ym/date::literals::last).day(); + auto last_day = (ym / date::literals::last).day(); for (auto d = date::day(1); d <= last_day; ++d, ++wd) { if (wd != first_dow) { os << ' '; @@ -98,12 +108,13 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str return result; } -auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void { +auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std::ostream& os) + -> void { auto wd = first_dow; do { if (wd != first_dow) os << ' '; Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); - auto wd_len = wd_ustring.length(); + auto wd_len = wd_ustring.length(); if (wd_len > 2) { wd_ustring = wd_ustring.substr(0, 2); wd_len = 2; @@ -125,13 +136,13 @@ using deleting_unique_ptr = std::unique_ptr>; // Computations done similarly to Linux cal utility. auto waybar::modules::Clock::first_day_of_week() -> date::weekday { #ifdef HAVE_LANGINFO_1STDAY - deleting_unique_ptr::type, freelocale> - posix_locale{newlocale(LC_ALL, locale_.name().c_str(), nullptr)}; + deleting_unique_ptr::type, freelocale> posix_locale{ + newlocale(LC_ALL, locale_.name().c_str(), nullptr)}; if (posix_locale) { - const int i = (std::intptr_t) nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()); - auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100); - auto wd = date::weekday(ymd); - uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get()); + const int i = (std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()); + auto ymd = date::year(i / 10000) / (i / 100 % 100) / (i % 100); + auto wd = date::weekday(ymd); + uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get()); return wd + date::days(j - 1); } #endif From a871dcaebee460d603aa7f3d00ef32b28d8b7052 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 22 May 2020 18:56:32 +0200 Subject: [PATCH 2/3] fix: type --- src/modules/clock.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index c9f09a3a..f829d94b 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -43,14 +43,15 @@ auto waybar::modules::Clock::update() -> void { waybar_time wtime = {locale_, date::make_zoned(time_zone_, date::floor(now))}; + std::string text; if (!fixed_time_zone_) { // As date dep is not fully compatible, prefer fmt tzset(); auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now)); - auto text = fmt::format(format_, localtime); + text = fmt::format(format_, localtime); label_.set_markup(text); } else { - auto text = fmt::format(format_, wtime); + text = fmt::format(format_, wtime); label_.set_markup(text); } From 60bad8279eabbf8867269c9d12f1fe6ba287224d Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 22 May 2020 19:09:50 +0200 Subject: [PATCH 3/3] feat: add comment --- src/modules/clock.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index f829d94b..b8c7387a 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -14,6 +14,7 @@ using waybar::modules::waybar_time; waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60), fixed_time_zone_(false) { if (config_["timezone"].isString()) { + spdlog::warn("As using a timezone, some format args may be missing as the date library havn't got a release since 2018."); time_zone_ = date::locate_zone(config_["timezone"].asString()); fixed_time_zone_ = true; }