Attempt at supporting locale and timezones (#1)
parent
6e30b7af3c
commit
84b671f6b2
|
@ -20,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,13 +18,19 @@ 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*: ++
|
*timezone*: ++
|
||||||
typeof: string ++
|
typeof: string ++
|
||||||
default: inferred local timezone ++
|
default: inferred local timezone ++
|
||||||
The timezone to display the time in, e.g. America/New_York.
|
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 ++
|
||||||
The maximum length in character the module should display.
|
The maximum length in character the module should display.
|
||||||
|
|
|
@ -1,7 +1,20 @@
|
||||||
#include "modules/clock.hpp"
|
#include "modules/clock.hpp"
|
||||||
|
|
||||||
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();
|
||||||
|
@ -11,23 +24,28 @@ 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_) {
|
||||||
const date::time_zone* zone;
|
// Time zone can change. Be sure to pick that.
|
||||||
auto now = std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
|
time_zone_ = date::current_zone();
|
||||||
if (config_["timezone"].isString()) {
|
|
||||||
zone = date::locate_zone(config_["timezone"].asString());
|
|
||||||
} else {
|
|
||||||
zone = date::current_zone();
|
|
||||||
}
|
}
|
||||||
auto localtime = date::make_zoned(zone, now);
|
auto now = std::chrono::system_clock::now();
|
||||||
auto text = fmt::format(format_, localtime);
|
waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)};
|
||||||
|
|
||||||
|
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);
|
||||||
|
@ -35,22 +53,10 @@ auto waybar::modules::Clock::update() -> void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ZonedTimeInner>
|
template <>
|
||||||
struct fmt::formatter<date::zoned_time<ZonedTimeInner>> {
|
struct fmt::formatter<waybar_time> : fmt::formatter<std::tm> {
|
||||||
|
|
||||||
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>
|
template <typename FormatContext>
|
||||||
auto format(const date::zoned_time<ZonedTimeInner>& d, FormatContext& ctx) {
|
auto format(const waybar_time& t, FormatContext& ctx) {
|
||||||
return format_to(ctx.out(), "{}", date::format(std::locale(""), *format_string, d));
|
return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(tm_format), t.ztime));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue