fix: prefer default fmt date formatting unless timezone specified

This commit is contained in:
Alex 2020-05-22 18:52:26 +02:00
parent c5bbedfabb
commit cef5b27b48

View File

@ -1,4 +1,7 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
#include <time.h>
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
@ -9,9 +12,7 @@
using waybar::modules::waybar_time; using waybar::modules::waybar_time;
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
: ALabel(config, "clock", id, "{:%H:%M}", 60) : ALabel(config, "clock", id, "{:%H:%M}", 60), fixed_time_zone_(false) {
, fixed_time_zone_(false)
{
if (config_["timezone"].isString()) { if (config_["timezone"].isString()) {
time_zone_ = date::locate_zone(config_["timezone"].asString()); time_zone_ = date::locate_zone(config_["timezone"].asString());
fixed_time_zone_ = true; fixed_time_zone_ = true;
@ -37,12 +38,21 @@ auto waybar::modules::Clock::update() -> void {
// Time zone can change. Be sure to pick that. // Time zone can change. Be sure to pick that.
time_zone_ = date::current_zone(); time_zone_ = date::current_zone();
} }
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
waybar_time wtime = {locale_, waybar_time wtime = {locale_,
date::make_zoned(time_zone_, date::floor<std::chrono::seconds>(now))}; date::make_zoned(time_zone_, date::floor<std::chrono::seconds>(now))};
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); auto text = fmt::format(format_, wtime);
label_.set_markup(text); label_.set_markup(text);
}
if (tooltipEnabled()) { if (tooltipEnabled()) {
if (config_["tooltip-format"].isString()) { if (config_["tooltip-format"].isString()) {
@ -98,7 +108,8 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
return result; 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; auto wd = first_dow;
do { do {
if (wd != first_dow) os << ' '; if (wd != first_dow) os << ' ';
@ -125,8 +136,8 @@ using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
// Computations done similarly to Linux cal utility. // Computations done similarly to Linux cal utility.
auto waybar::modules::Clock::first_day_of_week() -> date::weekday { auto waybar::modules::Clock::first_day_of_week() -> date::weekday {
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> posix_locale{
posix_locale{newlocale(LC_ALL, locale_.name().c_str(), nullptr)}; newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
if (posix_locale) { if (posix_locale) {
const int i = (std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, 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 ymd = date::year(i / 10000) / (i / 100 % 100) / (i % 100);