From cd2db19267214ca2422b5ecd07d7eb99d049132e Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Mon, 3 Feb 2020 16:58:18 -0800 Subject: [PATCH] Detect presence, call nl_langinfo() to get first day of week. --- meson.build | 12 ++++++++++++ src/modules/clock.cpp | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 69439aca..98dd25e8 100644 --- a/meson.build +++ b/meson.build @@ -42,6 +42,18 @@ if not compiler.has_header('filesystem') endif endif +code = '''#include +int main(int argc, char** argv) { + char* str; + str = nl_langinfo(_NL_TIME_WEEK_1STDAY); + str = nl_langinfo(_NL_TIME_FIRST_WEEKDAY); + return 0; +} +''' +if compiler.links(code, name : 'nl_langinfo with _NL_TIME_WEEK_1STDAY, _NL_TIME_FIRST_WEEKDAY') + add_project_arguments('-DHAVE_LANGINFO_1STDAY', language: 'cpp') +endif + add_global_arguments(cpp_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp') diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index ef79274e..8419d18b 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,5 +1,8 @@ #include "modules/clock.hpp" #include +#ifdef HAVE_LANGINFO_1STDAY +#include +#endif using zoned_time = date::zoned_time; @@ -10,6 +13,17 @@ struct waybar_time { namespace { +#ifdef HAVE_LANGINFO_1STDAY +// Computations done similarly to Linux cal utility. +date::weekday first_day_of_week() { + const int i = (std::intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY); + auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100); + auto wd = date::weekday(ymd); + uint8_t j = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY); + return wd + date::days(j - 1); +} +#endif + void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) { auto wd = first_dow; do { @@ -38,7 +52,7 @@ struct CachedCalendar { CachedCalendar cached_calendar; -std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { +std::string calendar_text(const waybar_time& wtime) { const auto daypoint = date::floor(wtime.ztime.get_local_time()); const auto ymd = date::year_month_day(daypoint); if (cached_calendar.ymd == ymd) { @@ -49,6 +63,11 @@ std::string calendar_text(const waybar_time& wtime, const date::weekday& first_d const auto curr_day = ymd.day(); std::stringstream os; +#ifdef HAVE_LANGINFO_1STDAY + const auto first_dow = first_day_of_week(); +#else + const auto first_dow = date::Sunday; +#endif weekdays_header(wtime.locale, first_dow, os); // First week prefixed with spaces if needed. @@ -115,7 +134,7 @@ auto waybar::modules::Clock::update() -> void { if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { - const auto calendar = calendar_text(wtime, date::Sunday); + 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)); label_.set_tooltip_markup(tooltip_text);