2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/clock.hpp"
|
2020-01-31 16:54:41 +00:00
|
|
|
#include <sstream>
|
2020-02-04 00:58:18 +00:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
2020-01-31 16:54:41 +00:00
|
|
|
|
|
|
|
using zoned_time = date::zoned_time<std::chrono::system_clock::duration>;
|
|
|
|
|
|
|
|
struct waybar_time {
|
|
|
|
std::locale locale;
|
|
|
|
zoned_time ztime;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-02-04 00:58:18 +00:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
// Computations done similarly to Linux cal utility.
|
|
|
|
date::weekday first_day_of_week() {
|
|
|
|
const int i = (std::intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
|
|
|
|
auto ymd = date::year(i / 10000)/(i / 100 % 100)/(i % 100);
|
|
|
|
auto wd = date::weekday(ymd);
|
|
|
|
uint8_t j = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY);
|
|
|
|
return wd + date::days(j - 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-31 16:54:41 +00:00
|
|
|
void weekdays_header(const std::locale& locale, const date::weekday& first_dow, std::ostream& os) {
|
|
|
|
auto wd = first_dow;
|
|
|
|
do {
|
|
|
|
if (wd != first_dow) os << ' ';
|
2020-02-02 22:35:07 +00:00
|
|
|
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;
|
2020-01-31 16:54:41 +00:00
|
|
|
}
|
2020-02-02 22:35:07 +00:00
|
|
|
const std::string pad(2 - wd_len, ' ');
|
|
|
|
os << pad << wd_ustring;
|
2020-01-31 16:54:41 +00:00
|
|
|
} while (++wd != first_dow);
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
|
2020-02-02 22:44:26 +00:00
|
|
|
struct CachedCalendar {
|
|
|
|
date::year_month_day ymd;
|
|
|
|
std::string text;
|
|
|
|
|
|
|
|
void set(const date::year_month_day& ymd_, std::string text_) {
|
|
|
|
ymd = ymd_;
|
|
|
|
text = text_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CachedCalendar cached_calendar;
|
|
|
|
|
2020-02-04 00:58:18 +00:00
|
|
|
std::string calendar_text(const waybar_time& wtime) {
|
2020-01-31 16:54:41 +00:00
|
|
|
const auto daypoint = date::floor<date::days>(wtime.ztime.get_local_time());
|
|
|
|
const auto ymd = date::year_month_day(daypoint);
|
2020-02-02 22:44:26 +00:00
|
|
|
if (cached_calendar.ymd == ymd) {
|
|
|
|
return cached_calendar.text;
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:54:41 +00:00
|
|
|
const date::year_month ym(ymd.year(), ymd.month());
|
|
|
|
const auto curr_day = ymd.day();
|
|
|
|
|
|
|
|
std::stringstream os;
|
2020-02-04 00:58:18 +00:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
const auto first_dow = first_day_of_week();
|
|
|
|
#else
|
|
|
|
const auto first_dow = date::Sunday;
|
|
|
|
#endif
|
2020-01-31 16:54:41 +00:00
|
|
|
weekdays_header(wtime.locale, first_dow, os);
|
|
|
|
|
|
|
|
// 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 22:55:37 +00: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 16:54:41 +00:00
|
|
|
if (d == curr_day) {
|
|
|
|
os << "<b><u>" << date::format("%e", d) << "</u></b>";
|
|
|
|
} else {
|
|
|
|
os << date::format("%e", d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 22:44:26 +00:00
|
|
|
auto result = os.str();
|
|
|
|
cached_calendar.set(ymd, result);
|
|
|
|
return result;
|
2020-01-31 16:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2020-01-21 22:48:16 +00:00
|
|
|
: 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("");
|
|
|
|
}
|
|
|
|
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_ = [this] {
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2019-03-07 11:34:12 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2018-11-23 10:57:37 +00:00
|
|
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
2019-05-22 09:51:33 +00:00
|
|
|
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());
|
|
|
|
thread_.sleep_until(timeout - diff);
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Clock::update() -> void {
|
2020-01-21 22:48:16 +00:00
|
|
|
if (!fixed_time_zone_) {
|
|
|
|
// Time zone can change. Be sure to pick that.
|
|
|
|
time_zone_ = date::current_zone();
|
2020-01-20 23:47:15 +00:00
|
|
|
}
|
2020-01-21 22:48:16 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
waybar_time wtime = {locale_, date::make_zoned(time_zone_, now)};
|
|
|
|
|
|
|
|
auto text = fmt::format(format_, wtime);
|
2019-02-24 08:25:34 +00:00
|
|
|
label_.set_markup(text);
|
2019-04-18 15:52:00 +00:00
|
|
|
|
2019-02-24 08:25:34 +00:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
2020-02-04 00:58:18 +00:00
|
|
|
const auto calendar = calendar_text(wtime);
|
2019-02-24 08:25:34 +00:00
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
2020-01-31 16:54:41 +00:00
|
|
|
auto tooltip_text = fmt::format(tooltip_format, wtime, fmt::arg("calendar", calendar));
|
|
|
|
label_.set_tooltip_markup(tooltip_text);
|
2019-02-24 08:25:34 +00:00
|
|
|
} else {
|
2020-01-31 16:54:41 +00:00
|
|
|
label_.set_tooltip_markup(text);
|
2019-02-24 08:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|
2020-01-20 23:47:15 +00:00
|
|
|
|
2020-01-21 22:48:16 +00:00
|
|
|
template <>
|
|
|
|
struct fmt::formatter<waybar_time> : fmt::formatter<std::tm> {
|
2020-01-20 23:47:15 +00:00
|
|
|
template <typename FormatContext>
|
2020-01-21 22:48:16 +00: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-20 23:47:15 +00:00
|
|
|
}
|
|
|
|
};
|