From 3130a57622ce173f43475487a9a5314694442f1b Mon Sep 17 00:00:00 2001 From: Michael Cordover Date: Mon, 20 Jan 2020 18:47:15 -0500 Subject: [PATCH 1/4] Add timezone support to clock module (closes #223) --- man/waybar-clock.5.scd | 5 +++++ meson.build | 6 +++++- resources/config | 1 + src/modules/clock.cpp | 32 +++++++++++++++++++++++++++++--- subprojects/date.wrap | 9 +++++++++ 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 subprojects/date.wrap diff --git a/man/waybar-clock.5.scd b/man/waybar-clock.5.scd index 959ec94b..9f79f88a 100644 --- a/man/waybar-clock.5.scd +++ b/man/waybar-clock.5.scd @@ -20,6 +20,11 @@ The *clock* module displays the current date and time. default: {:%H:%M} ++ The format, how the date and time should be displayed. +*timezone*: ++ + typeof: string ++ + default: inferred local timezone ++ + The timezone to display the time in, e.g. America/New_York. + *max-length*: ++ typeof: integer ++ The maximum length in character the module should display. diff --git a/meson.build b/meson.build index a099ad2b..61f6b4c2 100644 --- a/meson.build +++ b/meson.build @@ -66,6 +66,8 @@ gtk_layer_shell = dependency('gtk-layer-shell-0', required: get_option('gtk-layer-shell'), fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) systemd = dependency('systemd', required: get_option('systemd')) +date_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'date_dep' ]) +tz_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'tz_dep' ]) prefix = get_option('prefix') conf_data = configuration_data() @@ -166,7 +168,9 @@ executable( libpulse, libudev, libmpdclient, - gtk_layer_shell + gtk_layer_shell, + date_dep, + tz_dep ], include_directories: [include_directories('include')], install: true, diff --git a/resources/config b/resources/config index 4e392399..8dfa012b 100644 --- a/resources/config +++ b/resources/config @@ -64,6 +64,7 @@ "spacing": 10 }, "clock": { + // "timezone": "America/New_York", "tooltip-format": "{:%Y-%m-%d | %H:%M}", "format-alt": "{:%Y-%m-%d}" }, diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 2fa02142..d910b2b3 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,5 +1,5 @@ #include "modules/clock.hpp" -#include +#include waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) { @@ -14,8 +14,14 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) auto waybar::modules::Clock::update() -> void { tzset(); // Update timezone information - auto now = std::chrono::system_clock::now(); - auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now)); + const date::time_zone* zone; + auto now = std::chrono::floor(std::chrono::system_clock::now()); + if (config_["timezone"].isString()) { + zone = date::locate_zone(config_["timezone"].asString()); + } else { + zone = date::current_zone(); + } + auto localtime = date::make_zoned(zone, now); auto text = fmt::format(format_, localtime); label_.set_markup(text); @@ -29,3 +35,23 @@ auto waybar::modules::Clock::update() -> void { } } } + +template +struct fmt::formatter> { + + std::string *format_string; + + constexpr auto parse(format_parse_context& ctx) { + format_string = new std::string[1]; + auto it = ctx.begin(), end = ctx.end(); + while (it != (end - 1)) { + *format_string += *it++; + } + return it; + } + + template + auto format(const date::zoned_time& d, FormatContext& ctx) { + return format_to(ctx.out(), "{}", date::format(*format_string, d)); + } +}; diff --git a/subprojects/date.wrap b/subprojects/date.wrap new file mode 100644 index 00000000..727bfddf --- /dev/null +++ b/subprojects/date.wrap @@ -0,0 +1,9 @@ +[wrap-file] +source_url=https://github.com/HowardHinnant/date/archive/v2.4.1.tar.gz +source_filename=date-2.4.1.tar.gz +source_hash=98907d243397483bd7ad889bf6c66746db0d7d2a39cc9aacc041834c40b65b98 +directory=date-2.4.1 + +patch_url = https://wrapdb.mesonbuild.com/v1/projects/hinnant-date/2.4.1/1/get_zip +patch_filename = hinnant-date-2.4.1-1-wrap.zip +patch_hash = 2061673a6f8e6d63c3a40df4da58fa2b3de2835fd9b3e74649e8279599f3a8f6 From 6e30b7af3c233cf18fb514ca4e1572fec94e7db8 Mon Sep 17 00:00:00 2001 From: Michael Cordover Date: Tue, 21 Jan 2020 09:36:41 -0500 Subject: [PATCH 2/4] Remove duplicate dependency, use current locale --- include/modules/clock.hpp | 1 + meson.build | 2 -- src/modules/clock.cpp | 3 +-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index aa9a0a22..a10f96a6 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -6,6 +6,7 @@ #else #include #endif +#include #include "ALabel.hpp" #include "util/sleeper_thread.hpp" diff --git a/meson.build b/meson.build index 61f6b4c2..69439aca 100644 --- a/meson.build +++ b/meson.build @@ -66,7 +66,6 @@ gtk_layer_shell = dependency('gtk-layer-shell-0', required: get_option('gtk-layer-shell'), fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) systemd = dependency('systemd', required: get_option('systemd')) -date_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'date_dep' ]) tz_dep = dependency('date', default_options : [ 'use_system_tzdb=true' ], fallback: [ 'date', 'tz_dep' ]) prefix = get_option('prefix') @@ -169,7 +168,6 @@ executable( libudev, libmpdclient, gtk_layer_shell, - date_dep, tz_dep ], include_directories: [include_directories('include')], diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index d910b2b3..1ad29a3d 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,5 +1,4 @@ #include "modules/clock.hpp" -#include waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) : ALabel(config, "clock", id, "{:%H:%M}", 60) { @@ -52,6 +51,6 @@ struct fmt::formatter> { template auto format(const date::zoned_time& d, FormatContext& ctx) { - return format_to(ctx.out(), "{}", date::format(*format_string, d)); + return format_to(ctx.out(), "{}", date::format(std::locale(""), *format_string, d)); } }; From 84b671f6b27090fc1d77e277a73b52e05aa4d867 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Tue, 21 Jan 2020 23:48:16 +0100 Subject: [PATCH 3/4] Attempt at supporting locale and timezones (#1) --- include/modules/clock.hpp | 3 ++ man/waybar-clock.5.scd | 8 +++++- src/modules/clock.cpp | 60 +++++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index a10f96a6..e54f4d2c 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -20,6 +20,9 @@ class Clock : public ALabel { private: util::SleeperThread thread_; + std::locale locale_; + const date::time_zone* time_zone_; + bool fixed_time_zone_; }; } // namespace waybar::modules diff --git a/man/waybar-clock.5.scd b/man/waybar-clock.5.scd index 9f79f88a..e3213f39 100644 --- a/man/waybar-clock.5.scd +++ b/man/waybar-clock.5.scd @@ -18,13 +18,19 @@ The *clock* module displays the current date and time. *format*: ++ typeof: string ++ default: {:%H:%M} ++ - The format, how the date and time should be displayed. + The format, how the date and time should be displayed. ++ + It uses the format of the date library. See https://howardhinnant.github.io/date/date.html#to_stream_formatting for details. *timezone*: ++ typeof: string ++ default: inferred local timezone ++ The timezone to display the time in, e.g. America/New_York. +*locale*: ++ + typeof: string ++ + default: inferred from current locale ++ + A locale to be used to display the time. Intended to render times in custom timezones with the proper language and format. + *max-length*: ++ typeof: integer ++ The maximum length in character the module should display. diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 1ad29a3d..38af1152 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,7 +1,20 @@ #include "modules/clock.hpp" 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) +{ + if (config_["timezone"].isString()) { + time_zone_ = date::locate_zone(config_["timezone"].asString()); + fixed_time_zone_ = true; + } + + if (config_["locale"].isString()) { + locale_ = std::locale(config_["locale"].asString()); + } else { + locale_ = std::locale(""); + } + thread_ = [this] { dp.emit(); auto now = std::chrono::system_clock::now(); @@ -11,23 +24,28 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) }; } +using zoned_time = date::zoned_time; + +struct waybar_time { + std::locale locale; + zoned_time ztime; +}; + auto waybar::modules::Clock::update() -> void { - tzset(); // Update timezone information - const date::time_zone* zone; - auto now = std::chrono::floor(std::chrono::system_clock::now()); - if (config_["timezone"].isString()) { - zone = date::locate_zone(config_["timezone"].asString()); - } else { - zone = date::current_zone(); + if (!fixed_time_zone_) { + // Time zone can change. Be sure to pick that. + time_zone_ = date::current_zone(); } - auto localtime = date::make_zoned(zone, now); - auto text = fmt::format(format_, localtime); + auto now = std::chrono::system_clock::now(); + waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)}; + + auto text = fmt::format(format_, wtime); label_.set_markup(text); if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { auto tooltip_format = config_["tooltip-format"].asString(); - auto tooltip_text = fmt::format(tooltip_format, localtime); + auto tooltip_text = fmt::format(tooltip_format, wtime); label_.set_tooltip_text(tooltip_text); } else { label_.set_tooltip_text(text); @@ -35,22 +53,10 @@ auto waybar::modules::Clock::update() -> void { } } -template -struct fmt::formatter> { - - std::string *format_string; - - constexpr auto parse(format_parse_context& ctx) { - format_string = new std::string[1]; - auto it = ctx.begin(), end = ctx.end(); - while (it != (end - 1)) { - *format_string += *it++; - } - return it; - } - +template <> +struct fmt::formatter : fmt::formatter { template - auto format(const date::zoned_time& d, FormatContext& ctx) { - return format_to(ctx.out(), "{}", date::format(std::locale(""), *format_string, d)); + auto format(const waybar_time& t, FormatContext& ctx) { + return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(tm_format), t.ztime)); } }; From 1e969a48ae74e5c8f8a45a89295c1be44c6312ad Mon Sep 17 00:00:00 2001 From: Michael Cordover Date: Thu, 23 Jan 2020 08:25:53 -0500 Subject: [PATCH 4/4] Use github instead of mesonbuild for hinnant-date patch --- subprojects/date.wrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subprojects/date.wrap b/subprojects/date.wrap index 727bfddf..ea73f0fa 100644 --- a/subprojects/date.wrap +++ b/subprojects/date.wrap @@ -4,6 +4,6 @@ source_filename=date-2.4.1.tar.gz source_hash=98907d243397483bd7ad889bf6c66746db0d7d2a39cc9aacc041834c40b65b98 directory=date-2.4.1 -patch_url = https://wrapdb.mesonbuild.com/v1/projects/hinnant-date/2.4.1/1/get_zip +patch_url = https://github.com/mesonbuild/hinnant-date/releases/download/2.4.1-1/hinnant-date.zip patch_filename = hinnant-date-2.4.1-1-wrap.zip patch_hash = 2061673a6f8e6d63c3a40df4da58fa2b3de2835fd9b3e74649e8279599f3a8f6