mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Merge pull request #560 from mjec/master
Add timezone support to clock module (closes #223)
This commit is contained in:
commit
e9b0365327
@ -6,6 +6,7 @@
|
|||||||
#else
|
#else
|
||||||
#include <fmt/chrono.h>
|
#include <fmt/chrono.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <date/tz.h>
|
||||||
#include "ALabel.hpp"
|
#include "ALabel.hpp"
|
||||||
#include "util/sleeper_thread.hpp"
|
#include "util/sleeper_thread.hpp"
|
||||||
|
|
||||||
@ -19,6 +20,9 @@ class Clock : public ALabel {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
|
std::locale locale_;
|
||||||
|
const date::time_zone* time_zone_;
|
||||||
|
bool fixed_time_zone_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules
|
} // namespace waybar::modules
|
||||||
|
@ -18,7 +18,18 @@ The *clock* module displays the current date and time.
|
|||||||
*format*: ++
|
*format*: ++
|
||||||
typeof: string ++
|
typeof: string ++
|
||||||
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. ++
|
||||||
|
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*: ++
|
*max-length*: ++
|
||||||
typeof: integer ++
|
typeof: integer ++
|
||||||
|
@ -66,6 +66,7 @@ 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'))
|
||||||
|
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 +167,8 @@ executable(
|
|||||||
libpulse,
|
libpulse,
|
||||||
libudev,
|
libudev,
|
||||||
libmpdclient,
|
libmpdclient,
|
||||||
gtk_layer_shell
|
gtk_layer_shell,
|
||||||
|
tz_dep
|
||||||
],
|
],
|
||||||
include_directories: [include_directories('include')],
|
include_directories: [include_directories('include')],
|
||||||
install: true,
|
install: true,
|
||||||
|
@ -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}"
|
||||||
},
|
},
|
||||||
|
@ -1,8 +1,20 @@
|
|||||||
#include "modules/clock.hpp"
|
#include "modules/clock.hpp"
|
||||||
#include <time.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)
|
||||||
|
, 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] {
|
thread_ = [this] {
|
||||||
dp.emit();
|
dp.emit();
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
@ -12,20 +24,39 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using zoned_time = date::zoned_time<std::chrono::system_clock::duration>;
|
||||||
|
|
||||||
|
struct waybar_time {
|
||||||
|
std::locale locale;
|
||||||
|
zoned_time ztime;
|
||||||
|
};
|
||||||
|
|
||||||
auto waybar::modules::Clock::update() -> void {
|
auto waybar::modules::Clock::update() -> void {
|
||||||
tzset(); // Update timezone information
|
if (!fixed_time_zone_) {
|
||||||
|
// Time zone can change. Be sure to pick that.
|
||||||
|
time_zone_ = date::current_zone();
|
||||||
|
}
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now));
|
waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)};
|
||||||
auto text = fmt::format(format_, localtime);
|
|
||||||
|
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()) {
|
||||||
auto tooltip_format = config_["tooltip-format"].asString();
|
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);
|
label_.set_tooltip_text(tooltip_text);
|
||||||
} else {
|
} else {
|
||||||
label_.set_tooltip_text(text);
|
label_.set_tooltip_text(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct fmt::formatter<waybar_time> : fmt::formatter<std::tm> {
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format(const waybar_time& t, FormatContext& ctx) {
|
||||||
|
return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(tm_format), t.ztime));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
9
subprojects/date.wrap
Normal file
9
subprojects/date.wrap
Normal 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://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
|
Loading…
Reference in New Issue
Block a user