2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/clock.hpp"
|
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2018-11-23 10:57:37 +00:00
|
|
|
: ALabel(config, "{:%H:%M}", 60)
|
2018-08-08 21:54:58 +00:00
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_name("clock");
|
2018-12-18 16:30:54 +00:00
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_ = [this] {
|
2018-12-26 10:13:36 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2018-11-23 10:57:37 +00:00
|
|
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
|
|
|
auto time_s = std::chrono::time_point_cast<std::chrono::seconds>(timeout);
|
|
|
|
auto sub_m =
|
2018-12-04 08:38:08 +00:00
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(time_s.time_since_epoch()).count() % interval_.count();
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_.sleep_until(timeout - std::chrono::seconds(sub_m - 1));
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-08-09 10:05:48 +00:00
|
|
|
auto waybar::modules::Clock::update() -> void
|
|
|
|
{
|
2018-08-15 18:53:27 +00:00
|
|
|
auto localtime = fmt::localtime(std::time(nullptr));
|
2018-11-21 19:49:09 +00:00
|
|
|
label_.set_markup(fmt::format(format_, localtime));
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|