Fix Clock crash on empty string in timezones field

Also fixed timezones behavior: now waybar starting with the first timezone in timezones list and falling back to timezone field only if timezones omit or has no elements.
This commit is contained in:
Sergey Mishin 2021-10-03 03:27:54 +00:00
parent 8b4dafd701
commit 174db444d6
No known key found for this signature in database
GPG Key ID: BFFA09D74430117B
3 changed files with 22 additions and 12 deletions

View File

@ -37,6 +37,7 @@ class Clock : public ALabel {
auto calendar_text(const waybar_time& wtime) -> std::string;
auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void;
auto first_day_of_week() -> date::weekday;
bool setTimeZone(Json::Value zone_name);
};
} // namespace waybar::modules

View File

@ -24,7 +24,8 @@ The *clock* module displays the current date and time.
*timezone*: ++
typeof: string ++
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. ++
This field will be ignored if *timezones* field is set and have at least one value.
*timezones*: ++
typeof: list of strings ++

View File

@ -15,10 +15,14 @@ using waybar::modules::waybar_time;
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
: ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true), fixed_time_zone_(false) {
if (config_["timezone"].isString()) {
if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
time_zone_idx_ = 0;
setTimeZone(config_["timezones"][time_zone_idx_]);
} else {
setTimeZone(config_["timezone"]);
}
if (fixed_time_zone_) {
spdlog::warn("As using a timezone, some format args may be missing as the date library haven't got a release since 2018.");
time_zone_ = date::locate_zone(config_["timezone"].asString());
fixed_time_zone_ = true;
}
if (config_["locale"].isString()) {
@ -72,6 +76,17 @@ auto waybar::modules::Clock::update() -> void {
ALabel::update();
}
bool waybar::modules::Clock::setTimeZone(Json::Value zone_name) {
if (!zone_name.isString() || zone_name.asString().empty()) {
fixed_time_zone_ = false;
return false;
}
time_zone_ = date::locate_zone(zone_name.asString());
fixed_time_zone_ = true;
return true;
}
bool waybar::modules::Clock::handleScroll(GdkEventScroll *e) {
// defer to user commands if set
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
@ -92,14 +107,7 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll *e) {
} else {
time_zone_idx_ = time_zone_idx_ == 0 ? nr_zones - 1 : time_zone_idx_ - 1;
}
auto zone_name = config_["timezones"][time_zone_idx_];
if (!zone_name.isString() || zone_name.empty()) {
fixed_time_zone_ = false;
} else {
time_zone_ = date::locate_zone(zone_name.asString());
fixed_time_zone_ = true;
}
setTimeZone(config_["timezones"][time_zone_idx_]);
update();
return true;
}