Merge pull request #715 from Alexays/fix-clock
This commit is contained in:
commit
a9b17681b0
|
@ -1,4 +1,7 @@
|
|||
#include "modules/clock.hpp"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
#ifdef HAVE_LANGINFO_1STDAY
|
||||
|
@ -9,10 +12,9 @@
|
|||
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()) {
|
||||
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;
|
||||
}
|
||||
|
@ -37,12 +39,22 @@ 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<std::chrono::seconds>(now))};
|
||||
|
||||
auto text = fmt::format(format_, wtime);
|
||||
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));
|
||||
text = fmt::format(format_, localtime);
|
||||
label_.set_markup(text);
|
||||
} else {
|
||||
text = fmt::format(format_, wtime);
|
||||
label_.set_markup(text);
|
||||
}
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
|
@ -73,12 +85,12 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
|
|||
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,7 +110,8 @@ 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 << ' ';
|
||||
|
@ -125,11 +138,11 @@ using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
|
|||
// Computations done similarly to Linux cal utility.
|
||||
auto waybar::modules::Clock::first_day_of_week() -> date::weekday {
|
||||
#ifdef HAVE_LANGINFO_1STDAY
|
||||
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale>
|
||||
posix_locale{newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
|
||||
deleting_unique_ptr<std::remove_pointer<locale_t>::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);
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue