From 3130a57622ce173f43475487a9a5314694442f1b Mon Sep 17 00:00:00 2001 From: Michael Cordover Date: Mon, 20 Jan 2020 18:47:15 -0500 Subject: [PATCH] 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 959ec94..9f79f88 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 a099ad2..61f6b4c 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 4e39239..8dfa012 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 2fa0214..d910b2b 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 0000000..727bfdd --- /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