2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/clock.hpp"
|
2020-01-31 17:54:41 +01:00
|
|
|
#include <sstream>
|
2020-02-05 20:02:42 +01:00
|
|
|
#include <type_traits>
|
2020-02-04 01:58:18 +01:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
#include <langinfo.h>
|
2020-02-05 20:02:42 +01:00
|
|
|
#include <locale.h>
|
2020-02-04 01:58:18 +01:00
|
|
|
#endif
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
using waybar::modules::waybar_time;
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
|
|
|
: 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;
|
|
|
|
}
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
if (config_["locale"].isString()) {
|
|
|
|
locale_ = std::locale(config_["locale"].asString());
|
|
|
|
} else {
|
|
|
|
locale_ = std::locale("");
|
|
|
|
}
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
thread_ = [this] {
|
|
|
|
dp.emit();
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
|
|
|
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());
|
|
|
|
thread_.sleep_until(timeout - diff);
|
|
|
|
};
|
2020-02-04 01:58:18 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
auto waybar::modules::Clock::update() -> void {
|
|
|
|
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();
|
|
|
|
waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)};
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
auto text = fmt::format(format_, wtime);
|
|
|
|
label_.set_markup(text);
|
2020-02-02 23:44:26 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
const auto calendar = calendar_text(wtime);
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar));
|
|
|
|
label_.set_tooltip_markup(tooltip_text);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_markup(text);
|
|
|
|
}
|
2020-02-02 23:44:26 +01:00
|
|
|
}
|
2020-02-05 20:02:42 +01:00
|
|
|
}
|
2020-02-02 23:44:26 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string {
|
2020-01-31 17:54:41 +01:00
|
|
|
const auto daypoint = date::floor<date::days>(wtime.ztime.get_local_time());
|
|
|
|
const auto ymd = date::year_month_day(daypoint);
|
2020-02-05 20:02:42 +01:00
|
|
|
if (cached_calendar_ymd_ == ymd) {
|
|
|
|
return cached_calendar_text_;
|
2020-02-02 23:44:26 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 17:54:41 +01:00
|
|
|
const date::year_month ym(ymd.year(), ymd.month());
|
|
|
|
const auto curr_day = ymd.day();
|
|
|
|
|
|
|
|
std::stringstream os;
|
2020-02-04 01:58:18 +01:00
|
|
|
const auto first_dow = first_day_of_week();
|
2020-02-05 20:02:42 +01:00
|
|
|
weekdays_header(first_dow, os);
|
2020-01-31 17:54:41 +01:00
|
|
|
|
|
|
|
// First week prefixed with spaces if needed.
|
|
|
|
auto wd = date::weekday(ym/1);
|
|
|
|
auto empty_days = (wd - first_dow).count();
|
|
|
|
if (empty_days > 0) {
|
|
|
|
os << std::string(empty_days * 3 - 1, ' ');
|
|
|
|
}
|
|
|
|
auto last_day = (ym/date::literals::last).day();
|
2020-02-02 23:55:37 +01:00
|
|
|
for (auto d = date::day(1); d <= last_day; ++d, ++wd) {
|
|
|
|
if (wd != first_dow) {
|
|
|
|
os << ' ';
|
|
|
|
} else if (unsigned(d) != 1) {
|
|
|
|
os << '\n';
|
|
|
|
}
|
2020-01-31 17:54:41 +01:00
|
|
|
if (d == curr_day) {
|
|
|
|
os << "<b><u>" << date::format("%e", d) << "</u></b>";
|
|
|
|
} else {
|
|
|
|
os << date::format("%e", d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:44:26 +01:00
|
|
|
auto result = os.str();
|
2020-02-05 20:02:42 +01:00
|
|
|
cached_calendar_ymd_ = ymd;
|
|
|
|
cached_calendar_text_ = result;
|
2020-02-02 23:44:26 +01:00
|
|
|
return result;
|
2020-01-31 17:54:41 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void {
|
|
|
|
auto wd = first_dow;
|
|
|
|
do {
|
|
|
|
if (wd != first_dow) os << ' ';
|
|
|
|
Glib::ustring wd_ustring(date::format(locale_, "%a", wd));
|
|
|
|
auto wd_len = wd_ustring.length();
|
|
|
|
if (wd_len > 2) {
|
|
|
|
wd_ustring = wd_ustring.substr(0, 2);
|
|
|
|
wd_len = 2;
|
|
|
|
}
|
|
|
|
const std::string pad(2 - wd_len, ' ');
|
|
|
|
os << pad << wd_ustring;
|
|
|
|
} while (++wd != first_dow);
|
|
|
|
os << "\n";
|
2020-01-31 17:54:41 +01:00
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
template <auto fn>
|
|
|
|
using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
|
2020-01-21 23:48:16 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
template <typename T, auto fn>
|
|
|
|
using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
|
|
|
|
#endif
|
2019-04-18 17:52:00 +02:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
// Computations done similarly to Linux cal utility.
|
|
|
|
auto waybar::modules::Clock::first_day_of_week() -> date::weekday {
|
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale>
|
|
|
|
posix_locale{newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
|
|
|
|
if (posix_locale) {
|
|
|
|
const int i = (std::intptr_t) nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get());
|
|
|
|
auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100);
|
|
|
|
auto wd = date::weekday(ymd);
|
|
|
|
uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get());
|
|
|
|
return wd + date::days(j - 1);
|
2019-02-24 09:25:34 +01:00
|
|
|
}
|
2020-02-05 20:02:42 +01:00
|
|
|
#endif
|
|
|
|
return date::Sunday;
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2020-01-21 00:47:15 +01:00
|
|
|
|
2020-01-21 23:48:16 +01:00
|
|
|
template <>
|
|
|
|
struct fmt::formatter<waybar_time> : fmt::formatter<std::tm> {
|
2020-01-21 00:47:15 +01:00
|
|
|
template <typename FormatContext>
|
2020-01-21 23:48:16 +01:00
|
|
|
auto format(const waybar_time& t, FormatContext& ctx) {
|
|
|
|
return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(tm_format), t.ztime));
|
2020-01-21 00:47:15 +01:00
|
|
|
}
|
|
|
|
};
|