Detect presence, call nl_langinfo() to get first day of week.

This commit is contained in:
Skirmantas Kligys 2020-02-03 16:58:18 -08:00
parent 4c40f9c635
commit cd2db19267
2 changed files with 33 additions and 2 deletions

View File

@ -42,6 +42,18 @@ if not compiler.has_header('filesystem')
endif endif
endif endif
code = '''#include<langinfo.h>
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_arguments(cpp_args, language : 'cpp')
add_global_link_arguments(cpp_link_args, language : 'cpp') add_global_link_arguments(cpp_link_args, language : 'cpp')

View File

@ -1,5 +1,8 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
#include <sstream> #include <sstream>
#ifdef HAVE_LANGINFO_1STDAY
#include <langinfo.h>
#endif
using zoned_time = date::zoned_time<std::chrono::system_clock::duration>; using zoned_time = date::zoned_time<std::chrono::system_clock::duration>;
@ -10,6 +13,17 @@ struct waybar_time {
namespace { 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) { void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) {
auto wd = first_dow; auto wd = first_dow;
do { do {
@ -38,7 +52,7 @@ struct CachedCalendar {
CachedCalendar cached_calendar; 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<date::days>(wtime.ztime.get_local_time()); const auto daypoint = date::floor<date::days>(wtime.ztime.get_local_time());
const auto ymd = date::year_month_day(daypoint); const auto ymd = date::year_month_day(daypoint);
if (cached_calendar.ymd == ymd) { 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(); const auto curr_day = ymd.day();
std::stringstream os; 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); weekdays_header(wtime.locale, first_dow, os);
// First week prefixed with spaces if needed. // First week prefixed with spaces if needed.
@ -115,7 +134,7 @@ auto waybar::modules::Clock::update() -> void {
if (tooltipEnabled()) { if (tooltipEnabled()) {
if (config_["tooltip-format"].isString()) { 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_format = config_["tooltip-format"].asString();
auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar)); auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar));
label_.set_tooltip_markup(tooltip_text); label_.set_tooltip_markup(tooltip_text);