2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/clock.hpp"
|
2020-01-21 00:47:15 +01:00
|
|
|
#include <date/tz.h>
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2019-05-22 12:06:24 +02:00
|
|
|
: ALabel(config, "clock", id, "{:%H:%M}", 60) {
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_ = [this] {
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2019-03-07 12:34:12 +01:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2018-11-23 11:57:37 +01:00
|
|
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
2019-05-22 11:51:33 +02:00
|
|
|
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());
|
|
|
|
thread_.sleep_until(timeout - diff);
|
2018-08-08 23:54:58 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Clock::update() -> void {
|
2019-10-04 23:25:09 +02:00
|
|
|
tzset(); // Update timezone information
|
2020-01-21 00:47:15 +01:00
|
|
|
const date::time_zone* zone;
|
|
|
|
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);
|
2019-02-24 09:25:34 +01:00
|
|
|
auto text = fmt::format(format_, localtime);
|
|
|
|
label_.set_markup(text);
|
2019-04-18 17:52:00 +02:00
|
|
|
|
2019-02-24 09:25:34 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
auto tooltip_text = fmt::format(tooltip_format, localtime);
|
|
|
|
label_.set_tooltip_text(tooltip_text);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(text);
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2020-01-21 00:47:15 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|