Add timezone support to clock module (closes #223)

pull/560/head
Michael Cordover 2020-01-20 18:47:15 -05:00 committed by Michael Cordover
parent 99dde1aff8
commit 3130a57622
5 changed files with 49 additions and 4 deletions

View File

@ -20,6 +20,11 @@ The *clock* module displays the current date and time.
default: {:%H:%M} ++ default: {:%H:%M} ++
The format, how the date and time should be displayed. 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*: ++ *max-length*: ++
typeof: integer ++ typeof: integer ++
The maximum length in character the module should display. The maximum length in character the module should display.

View File

@ -66,6 +66,8 @@ gtk_layer_shell = dependency('gtk-layer-shell-0',
required: get_option('gtk-layer-shell'), required: get_option('gtk-layer-shell'),
fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep'])
systemd = dependency('systemd', required: get_option('systemd')) 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') prefix = get_option('prefix')
conf_data = configuration_data() conf_data = configuration_data()
@ -166,7 +168,9 @@ executable(
libpulse, libpulse,
libudev, libudev,
libmpdclient, libmpdclient,
gtk_layer_shell gtk_layer_shell,
date_dep,
tz_dep
], ],
include_directories: [include_directories('include')], include_directories: [include_directories('include')],
install: true, install: true,

View File

@ -64,6 +64,7 @@
"spacing": 10 "spacing": 10
}, },
"clock": { "clock": {
// "timezone": "America/New_York",
"tooltip-format": "{:%Y-%m-%d | %H:%M}", "tooltip-format": "{:%Y-%m-%d | %H:%M}",
"format-alt": "{:%Y-%m-%d}" "format-alt": "{:%Y-%m-%d}"
}, },

View File

@ -1,5 +1,5 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
#include <time.h> #include <date/tz.h>
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) {
@ -14,8 +14,14 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
auto waybar::modules::Clock::update() -> void { auto waybar::modules::Clock::update() -> void {
tzset(); // Update timezone information tzset(); // Update timezone information
auto now = std::chrono::system_clock::now(); const date::time_zone* zone;
auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now)); auto now = std::chrono::floor<std::chrono::seconds>(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); auto text = fmt::format(format_, localtime);
label_.set_markup(text); label_.set_markup(text);
@ -29,3 +35,23 @@ auto waybar::modules::Clock::update() -> void {
} }
} }
} }
template <typename ZonedTimeInner>
struct fmt::formatter<date::zoned_time<ZonedTimeInner>> {
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 <typename FormatContext>
auto format(const date::zoned_time<ZonedTimeInner>& d, FormatContext& ctx) {
return format_to(ctx.out(), "{}", date::format(*format_string, d));
}
};

View File

@ -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