c++20. clock chrono API. STL + format

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
Viktar Lukashonak 2023-11-10 17:57:26 +03:00
parent c4330a1e36
commit 86a38980e4
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
8 changed files with 379 additions and 374 deletions

View File

@ -40,7 +40,7 @@ class AModule : public IModule {
private: private:
bool handleUserEvent(GdkEventButton *const &ev); bool handleUserEvent(GdkEventButton *const &ev);
const bool isTooltip;
std::vector<int> pid_; std::vector<int> pid_;
gdouble distance_scrolled_y_; gdouble distance_scrolled_y_;
gdouble distance_scrolled_x_; gdouble distance_scrolled_x_;

View File

@ -6,38 +6,26 @@
namespace waybar::modules { namespace waybar::modules {
const std::string kCalendarPlaceholder = "calendar"; const std::string kCldPlaceholder{"calendar"};
const std::string KTimezonedTimeListPlaceholder = "timezoned_time_list"; const std::string kTZPlaceholder{"tz_list"};
enum class WeeksSide {
LEFT,
RIGHT,
HIDDEN,
};
enum class CldMode { MONTH, YEAR }; enum class CldMode { MONTH, YEAR };
enum class WS { LEFT, RIGHT, HIDDEN };
class Clock final : public ALabel { class Clock final : public ALabel {
public: public:
Clock(const std::string&, const Json::Value&); Clock(const std::string&, const Json::Value&);
virtual ~Clock() = default; virtual ~Clock() = default;
auto update() -> void override; auto update() -> void override;
auto doAction(const std::string& name) -> void override; auto doAction(const std::string&) -> void override;
private: private:
util::SleeperThread thread_; const std::locale locale_;
std::locale locale_; // tooltip
std::vector<const date::time_zone*> time_zones_; const std::string tlpFmt_;
int current_time_zone_idx_; std::string tlpText_{""}; // tooltip text to print
bool is_calendar_in_tooltip_; // Calendar
bool is_timezoned_list_in_tooltip_; const bool cldInTooltip_; // calendar in tooltip
auto first_day_of_week() -> date::weekday;
const date::time_zone* current_timezone();
auto timezones_text(std::chrono::system_clock::time_point now) -> std::string;
/*Calendar properties*/
WeeksSide cldWPos_{WeeksSide::HIDDEN};
/* /*
0 - calendar.format.months 0 - calendar.format.months
1 - calendar.format.weekdays 1 - calendar.format.weekdays
@ -47,28 +35,37 @@ class Clock final : public ALabel {
5 - tooltip-format 5 - tooltip-format
*/ */
std::map<int, std::string const> fmtMap_; std::map<int, std::string const> fmtMap_;
uint cldMonCols_{3}; // calendar count month columns
int cldWnLen_{3}; // calendar week number length
const int cldMonColLen_{20}; // calendar month column length
WS cldWPos_{WS::HIDDEN}; // calendar week side to print
months cldCurrShift_{0}; // calendar months shift
year_month_day cldYearShift_; // calendar Year mode. Cached ymd
std::string cldYearCached_; // calendar Year mode. Cached calendar
year_month cldMonShift_; // calendar Month mode. Cached ym
std::string cldMonCached_; // calendar Month mode. Cached calendar
day cldBaseDay_{0}; // calendar Cached day. Is used when today is changing(midnight)
std::string cldText_{""}; // calendar text to print
CldMode cldMode_{CldMode::MONTH}; CldMode cldMode_{CldMode::MONTH};
uint cldMonCols_{3}; // Count of the month in the row auto get_calendar(const year_month_day& today, const year_month_day& ymd, const time_zone* tz)
int cldMonColLen_{20}; // Length of the month column -> const std::string;
int cldWnLen_{3}; // Length of the week number
date::year_month_day cldYearShift_; // time zoned time in tooltip
date::year_month cldMonShift_; const bool tzInTooltip_; // if need to print time zones text
date::months cldCurrShift_{0}; std::vector<const time_zone*> tzList_; // time zones list
date::months cldShift_{0}; int tzCurrIdx_; // current time zone index for tzList_
std::string cldYearCached_{}; std::string tzText_{""}; // time zones text to print
std::string cldMonCached_{}; util::SleeperThread thread_;
date::day cldBaseDay_{0};
/*Calendar functions*/ auto getTZtext(sys_seconds now) -> std::string;
auto get_calendar(const date::year_month_day& today, const date::year_month_day& ymd, auto first_day_of_week() -> weekday;
const date::time_zone* tz) -> const std::string; // Module actions
/*Clock actions*/
void cldModeSwitch(); void cldModeSwitch();
void cldShift_up(); void cldShift_up();
void cldShift_down(); void cldShift_down();
void tz_up(); void tz_up();
void tz_down(); void tz_down();
// Module Action Map
// ModuleActionMap
static inline std::map<const std::string, void (waybar::modules::Clock::*const)()> actionMap_{ static inline std::map<const std::string, void (waybar::modules::Clock::*const)()> actionMap_{
{"mode", &waybar::modules::Clock::cldModeSwitch}, {"mode", &waybar::modules::Clock::cldModeSwitch},
{"shift_up", &waybar::modules::Clock::cldShift_up}, {"shift_up", &waybar::modules::Clock::cldShift_up},
@ -76,4 +73,5 @@ class Clock final : public ALabel {
{"tz_up", &waybar::modules::Clock::tz_up}, {"tz_up", &waybar::modules::Clock::tz_up},
{"tz_down", &waybar::modules::Clock::tz_down}}; {"tz_down", &waybar::modules::Clock::tz_down}};
}; };
} // namespace waybar::modules } // namespace waybar::modules

View File

@ -1,34 +1,52 @@
#pragma once #pragma once
#include <fmt/format.h> #include <chrono>
#if HAVE_CHRONO_TIMEZONES #if HAVE_CHRONO_TIMEZONES
#include <chrono>
#include <format> #include <format>
/* Compatibility layer for <date/tz.h> on top of C++20 <chrono> */
namespace date {
using namespace std::chrono;
namespace literals {
using std::chrono::last;
}
inline auto format(const std::string& spec, const auto& ztime) {
return spec.empty() ? "" : std::vformat("{:L" + spec + "}", std::make_format_args(ztime));
}
inline auto format(const std::locale& loc, const std::string& spec, const auto& ztime) {
return spec.empty() ? "" : std::vformat(loc, "{:L" + spec + "}", std::make_format_args(ztime));
}
} // namespace date
#else #else
#include <date/tz.h> #include <date/tz.h>
#include <fmt/format.h>
#include <regex>
#endif #endif
// Date
namespace date {
#if HAVE_CHRONO_TIMEZONES
using namespace std::chrono;
using namespace std;
#else
using system_clock = std::chrono::system_clock;
using seconds = std::chrono::seconds;
template <typename T>
inline auto format(const char* spec, const T& arg) {
return date::format(std::regex_replace(spec, std::regex("\\{:L|\\}"), ""), arg);
}
template <typename T>
inline auto format(const std::locale& loc, const char* spec, const T& arg) {
return date::format(loc, std::regex_replace(spec, std::regex("\\{:L|\\}"), ""), arg);
}
constexpr decltype(auto) operator""d(unsigned long long d) noexcept {
return date::operator""_d(d); // very verbose, but it works
}
#endif
} // namespace date
// Format
namespace waybar::util::date::format {
#if HAVE_CHRONO_TIMEZONES
using namespace std;
#else
using namespace fmt;
#endif
} // namespace waybar::util::date::format
#if not HAVE_CHRONO_TIMEZONES
template <typename Duration, typename TimeZonePtr> template <typename Duration, typename TimeZonePtr>
struct fmt::formatter<date::zoned_time<Duration, TimeZonePtr>> { struct fmt::formatter<date::zoned_time<Duration, TimeZonePtr>> {
std::string_view specs; std::string_view specs;
@ -58,3 +76,6 @@ struct fmt::formatter<date::zoned_time<Duration, TimeZonePtr>> {
return fmt::format_to(ctx.out(), "{}", date::format(fmt::to_string(specs), ztime)); return fmt::format_to(ctx.out(), "{}", date::format(fmt::to_string(specs), ztime));
} }
}; };
#endif
using namespace date;

View File

@ -85,7 +85,7 @@ $XDG_CONFIG_HOME/waybar/config ++
:[ same as format :[ same as format
:[ Tooltip on hover :[ Tooltip on hover
View all valid format options in *strftime(3)* or have a look <https://fmt.dev/latest/syntax.html#chrono-specs> View all valid format options in *strftime(3)* or have a look https://en.cppreference.com/w/cpp/chrono/duration/formatter
2. Addressed by *clock: calendar* 2. Addressed by *clock: calendar*
[- *Option* [- *Option*
@ -156,7 +156,7 @@ View all valid format options in *strftime(3)* or have a look <https://fmt.dev/l
# FORMAT REPLACEMENTS # FORMAT REPLACEMENTS
- *{calendar}*: Current month calendar - *{calendar}*: Current month calendar
- *{timezoned_time_list}*: List of time in the rest timezones, if more than one timezone is set in the config - *{tz_list}*: List of time in the rest timezones, if more than one timezone is set in the config
# EXAMPLES # EXAMPLES

View File

@ -126,7 +126,22 @@ gtk_layer_shell = dependency('gtk-layer-shell-0',
systemd = dependency('systemd', required: get_option('systemd')) systemd = dependency('systemd', required: get_option('systemd'))
cpp_lib_chrono = compiler.compute_int('__cpp_lib_chrono', prefix : '#include <chrono>') cpp_lib_chrono = compiler.compute_int('__cpp_lib_chrono', prefix : '#include <chrono>')
have_chrono_timezones = cpp_lib_chrono >= 201907 have_chrono_timezones = cpp_lib_chrono >= 201611
if have_chrono_timezones
code = '''
#include <chrono>
using namespace std::chrono;
int main(int argc, char** argv) {
const time_zone* tz;
return 0;
}
'''
if not compiler.links(code)
have_chrono_timezones = false
endif
endif
if have_chrono_timezones if have_chrono_timezones
tz_dep = declare_dependency() tz_dep = declare_dependency()
else else

View File

@ -10,6 +10,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
bool enable_click, bool enable_scroll) bool enable_click, bool enable_scroll)
: name_(std::move(name)), : name_(std::move(name)),
config_(std::move(config)), config_(std::move(config)),
isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true},
distance_scrolled_y_(0.0), distance_scrolled_y_(0.0),
distance_scrolled_x_(0.0) { distance_scrolled_x_(0.0) {
// Configure module action Map // Configure module action Map
@ -189,9 +190,7 @@ bool AModule::handleScroll(GdkEventScroll* e) {
return true; return true;
} }
bool AModule::tooltipEnabled() { bool AModule::tooltipEnabled() { return isTooltip; }
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
}
AModule::operator Gtk::Widget&() { return event_box_; } AModule::operator Gtk::Widget&() { return event_box_; }

View File

@ -1,325 +1,286 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
#include <fmt/chrono.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <ctime>
#include <iomanip> #include <iomanip>
#include <regex> #include <regex>
#include <sstream>
#include <type_traits>
#include "util/ustring_clen.hpp" #include "util/ustring_clen.hpp"
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
#include <langinfo.h> #include <langinfo.h>
#include <locale.h> #include <locale.h>
#endif #endif
namespace fmt_lib = waybar::util::date::format;
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
: ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true), : ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true),
current_time_zone_idx_{0}, locale_{std::locale(config_["locale"].isString() ? config_["locale"].asString() : "")},
is_calendar_in_tooltip_{false}, tlpFmt_{(config_["tooltip-format"].isString()) ? config_["tooltip-format"].asString() : ""},
is_timezoned_list_in_tooltip_{false} { cldInTooltip_{tlpFmt_.find("{" + kCldPlaceholder + "}") != std::string::npos},
tzInTooltip_{tlpFmt_.find("{" + kTZPlaceholder + "}") != std::string::npos},
tzCurrIdx_{0} {
tlpText_ = tlpFmt_;
if (config_["timezones"].isArray() && !config_["timezones"].empty()) { if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
for (const auto& zone_name : config_["timezones"]) { for (const auto& zone_name : config_["timezones"]) {
if (!zone_name.isString()) continue; if (!zone_name.isString()) continue;
if (zone_name.asString().empty()) if (zone_name.asString().empty())
// local time should be shown // local time should be shown
time_zones_.push_back(date::current_zone()); tzList_.push_back(current_zone());
else else
try { try {
time_zones_.push_back(date::locate_zone(zone_name.asString())); tzList_.push_back(locate_zone(zone_name.asString()));
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::warn("Timezone: {0}. {1}", zone_name.asString(), e.what()); spdlog::warn("Timezone: {0}. {1}", zone_name.asString(), e.what());
} }
} }
} else if (config_["timezone"].isString()) { } else if (config_["timezone"].isString()) {
if (config_["timezone"].asString().empty()) if (config_["timezone"].asString().empty())
time_zones_.push_back(date::current_zone()); // local time should be shown
tzList_.push_back(current_zone());
else else
try { try {
time_zones_.push_back(date::locate_zone(config_["timezone"].asString())); tzList_.push_back(locate_zone(config_["timezone"].asString()));
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::warn("Timezone: {0}. {1}", config_["timezone"].asString(), e.what()); spdlog::warn("Timezone: {0}. {1}", config_["timezone"].asString(), e.what());
} }
} }
if (!tzList_.size()) tzList_.push_back(current_zone());
// If all timezones are parsed and no one is good // Calendar properties
if (!time_zones_.size()) { if (cldInTooltip_) {
time_zones_.push_back(date::current_zone()); if (config_[kCldPlaceholder]["mode"].isString()) {
} const std::string cfgMode{config_[kCldPlaceholder]["mode"].asString()};
const std::map<std::string_view, const CldMode&> monthModes{{"month", CldMode::MONTH},
// Check if a particular placeholder is present in the tooltip format, to know what to calculate
// on update.
if (config_["tooltip-format"].isString()) {
std::string trimmed_format{config_["tooltip-format"].asString()};
fmtMap_.insert({5, trimmed_format});
trimmed_format.erase(std::remove_if(trimmed_format.begin(), trimmed_format.end(),
[](unsigned char x) { return std::isspace(x); }),
trimmed_format.end());
if (trimmed_format.find("{" + kCalendarPlaceholder + "}") != std::string::npos) {
is_calendar_in_tooltip_ = true;
}
if (trimmed_format.find("{" + KTimezonedTimeListPlaceholder + "}") != std::string::npos) {
is_timezoned_list_in_tooltip_ = true;
}
}
// Calendar configuration
if (is_calendar_in_tooltip_) {
if (config_[kCalendarPlaceholder]["weeks-pos"].isString()) {
if (config_[kCalendarPlaceholder]["weeks-pos"].asString() == "left") {
cldWPos_ = WeeksSide::LEFT;
} else if (config_[kCalendarPlaceholder]["weeks-pos"].asString() == "right") {
cldWPos_ = WeeksSide::RIGHT;
}
}
if (config_[kCalendarPlaceholder]["format"]["months"].isString())
fmtMap_.insert({0, config_[kCalendarPlaceholder]["format"]["months"].asString()});
else
fmtMap_.insert({0, "{}"});
if (config_[kCalendarPlaceholder]["format"]["days"].isString())
fmtMap_.insert({2, config_[kCalendarPlaceholder]["format"]["days"].asString()});
else
fmtMap_.insert({2, "{}"});
if (config_[kCalendarPlaceholder]["format"]["weeks"].isString() &&
cldWPos_ != WeeksSide::HIDDEN) {
fmtMap_.insert(
{4, std::regex_replace(config_[kCalendarPlaceholder]["format"]["weeks"].asString(),
std::regex("\\{\\}"),
(first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}")});
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
cldWnLen_ += tmp.size();
} else {
if (cldWPos_ != WeeksSide::HIDDEN)
fmtMap_.insert({4, (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}"});
else
cldWnLen_ = 0;
}
if (config_[kCalendarPlaceholder]["format"]["weekdays"].isString())
fmtMap_.insert({1, config_[kCalendarPlaceholder]["format"]["weekdays"].asString()});
else
fmtMap_.insert({1, "{}"});
if (config_[kCalendarPlaceholder]["format"]["today"].isString()) {
fmtMap_.insert({3, config_[kCalendarPlaceholder]["format"]["today"].asString()});
cldBaseDay_ =
date::year_month_day{date::floor<date::days>(std::chrono::system_clock::now())}.day();
} else
fmtMap_.insert({3, "{}"});
if (config_[kCalendarPlaceholder]["mode"].isString()) {
const std::string cfgMode{(config_[kCalendarPlaceholder]["mode"].isString())
? config_[kCalendarPlaceholder]["mode"].asString()
: "month"};
const std::map<std::string, const CldMode&> monthModes{{"month", CldMode::MONTH},
{"year", CldMode::YEAR}}; {"year", CldMode::YEAR}};
if (monthModes.find(cfgMode) != monthModes.end()) if (monthModes.find(cfgMode) != monthModes.end())
cldMode_ = monthModes.at(cfgMode); cldMode_ = monthModes.at(cfgMode);
else else
spdlog::warn( spdlog::warn(
"Clock calendar configuration \"mode\"\"\" \"{0}\" is not recognized. Mode = \"month\" " "Clock calendar configuration mode \"{0}\" is not recognized. Mode = \"month\" is "
"is using instead", "using instead",
cfgMode); cfgMode);
} }
if (config_[kCalendarPlaceholder]["mode-mon-col"].isInt()) { if (config_[kCldPlaceholder]["weeks-pos"].isString()) {
cldMonCols_ = config_[kCalendarPlaceholder]["mode-mon-col"].asInt(); if (config_[kCldPlaceholder]["weeks-pos"].asString() == "left") cldWPos_ = WS::LEFT;
if (cldMonCols_ == 0u || 12 % cldMonCols_ != 0u) { if (config_[kCldPlaceholder]["weeks-pos"].asString() == "right") cldWPos_ = WS::RIGHT;
cldMonCols_ = 3u; }
if (config_[kCldPlaceholder]["format"]["months"].isString())
fmtMap_.insert({0, config_[kCldPlaceholder]["format"]["months"].asString()});
else
fmtMap_.insert({0, "{}"});
if (config_[kCldPlaceholder]["format"]["weekdays"].isString())
fmtMap_.insert({1, config_[kCldPlaceholder]["format"]["weekdays"].asString()});
else
fmtMap_.insert({1, "{}"});
if (config_[kCldPlaceholder]["format"]["days"].isString())
fmtMap_.insert({2, config_[kCldPlaceholder]["format"]["days"].asString()});
else
fmtMap_.insert({2, "{}"});
if (config_[kCldPlaceholder]["format"]["today"].isString()) {
fmtMap_.insert({3, config_[kCldPlaceholder]["format"]["today"].asString()});
cldBaseDay_ =
year_month_day{
floor<days>(zoned_time{current_zone(), system_clock::now()}.get_local_time())}
.day();
} else
fmtMap_.insert({3, "{}"});
if (config_[kCldPlaceholder]["format"]["weeks"].isString() && cldWPos_ != WS::HIDDEN) {
fmtMap_.insert({4, std::regex_replace(config_[kCldPlaceholder]["format"]["weeks"].asString(),
std::regex("\\{\\}"),
(first_day_of_week() == Monday) ? "{:%W}" : "{:%U}")});
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
cldWnLen_ += tmp.size();
} else {
if (cldWPos_ != WS::HIDDEN)
fmtMap_.insert({4, (first_day_of_week() == Monday) ? "{:%W}" : "{:%U}"});
else
cldWnLen_ = 0;
}
if (config_[kCldPlaceholder]["mode-mon-col"].isInt()) {
cldMonCols_ = config_[kCldPlaceholder]["mode-mon-col"].asInt();
if (cldMonCols_ == 0u || (12 % cldMonCols_) != 0u) {
spdlog::warn( spdlog::warn(
"Clock calendar configuration \"mode-mon-col\" = {0} must be one of [1, 2, 3, 4, 6, " "Clock calendar configuration mode-mon-col = {0} must be one of [1, 2, 3, 4, 6, 12]. "
"12]. Value 3 is using instead", "Value 3 is using instead",
cldMonCols_); cldMonCols_);
cldMonCols_ = 3u;
} }
} else } else
cldMonCols_ = 1; cldMonCols_ = 1;
if (config_[kCalendarPlaceholder]["on-scroll"].isInt()) { if (config_[kCldPlaceholder]["on-scroll"].isInt()) {
cldShift_ = date::months{config_[kCalendarPlaceholder]["on-scroll"].asInt()};
event_box_.add_events(Gdk::LEAVE_NOTIFY_MASK); event_box_.add_events(Gdk::LEAVE_NOTIFY_MASK);
event_box_.signal_leave_notify_event().connect([this](GdkEventCrossing*) { event_box_.signal_leave_notify_event().connect([this](GdkEventCrossing*) {
cldCurrShift_ = date::months{0}; cldCurrShift_ = months{0};
return false; return false;
}); });
} }
} }
if (config_["locale"].isString())
locale_ = std::locale(config_["locale"].asString());
else
locale_ = std::locale("");
thread_ = [this] { thread_ = [this] {
dp.emit(); dp.emit();
auto now = std::chrono::system_clock::now(); thread_.sleep_for(interval_ - system_clock::now().time_since_epoch() % interval_);
/* difference with projected wakeup time */
auto diff = now.time_since_epoch() % interval_;
/* sleep until the next projected time */
thread_.sleep_for(interval_ - diff);
}; };
} }
const date::time_zone* waybar::modules::Clock::current_timezone() {
return time_zones_[current_time_zone_idx_];
}
auto waybar::modules::Clock::update() -> void { auto waybar::modules::Clock::update() -> void {
const auto* tz{current_timezone()}; auto tz{tzList_[tzCurrIdx_]};
const date::zoned_time now{ const zoned_time now{tz, floor<seconds>(system_clock::now())};
tz,
date::floor<std::chrono::seconds>(
std::chrono::system_clock::now())}; // Define local time is based on provided time zone
const date::year_month_day today{
date::floor<date::days>(now.get_local_time())}; // Convert now to year_month_day
const date::year_month_day shiftedDay{today + cldCurrShift_}; // Shift today
// Define shift local time
const auto shiftedNow{date::make_zoned(
tz, date::local_days(shiftedDay) +
(now.get_sys_time() - date::floor<date::days>(now.get_sys_time())))};
label_.set_markup(fmt::format(locale_, fmt::runtime(format_), now)); label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));
if (tooltipEnabled()) { if (tooltipEnabled()) {
const std::string tz_text{(is_timezoned_list_in_tooltip_) ? timezones_text(now.get_sys_time()) const year_month_day today{floor<days>(now.get_local_time())};
: ""}; const auto shiftedDay{today + cldCurrShift_};
const std::string cld_text{(is_calendar_in_tooltip_) ? get_calendar(today, shiftedDay, tz) const zoned_time shiftedNow{
: ""}; tz, local_days(shiftedDay) + (now.get_local_time() - floor<days>(now.get_local_time()))};
const std::string text{fmt::format(locale_, fmt::runtime(fmtMap_[5]), shiftedNow, if (tzInTooltip_) tzText_ = getTZtext(now.get_sys_time());
fmt::arg(KTimezonedTimeListPlaceholder.c_str(), tz_text), if (cldInTooltip_) cldText_ = get_calendar(today, shiftedDay, tz);
fmt::arg(kCalendarPlaceholder.c_str(), cld_text))}; if (tzInTooltip_ || cldInTooltip_) {
label_.set_tooltip_markup(text); // std::vformat doesn't support named arguments.
tlpText_ = std::regex_replace(tlpFmt_, std::regex("\\{" + kTZPlaceholder + "\\}"), tzText_);
tlpText_ =
std::regex_replace(tlpText_, std::regex("\\{" + kCldPlaceholder + "\\}"), cldText_);
}
tlpText_ = fmt_lib::vformat(locale_, tlpText_, fmt_lib::make_format_args(shiftedNow));
label_.set_tooltip_markup(tlpText_);
} }
// Call parent update
ALabel::update(); ALabel::update();
} }
auto waybar::modules::Clock::doAction(const std::string& name) -> void { auto waybar::modules::Clock::getTZtext(sys_seconds now) -> std::string {
if ((actionMap_[name])) { if (tzList_.size() == 1) return "";
(this->*actionMap_[name])();
update(); std::stringstream os;
} else for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) {
spdlog::error("Clock. Unsupported action \"{0}\"", name); if (static_cast<int>(tz_idx) == tzCurrIdx_) continue;
auto zt{zoned_time{tzList_[tz_idx], now}};
os << fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(zt)) << '\n';
}
return os.str();
} }
// The number of weeks in calendar month layout plus 1 more for calendar titles const unsigned cldRowsInMonth(const year_month& ym, const weekday& firstdow) {
const unsigned cldRowsInMonth(const date::year_month& ym, const date::weekday& firstdow) { return 2u + ceil<weeks>((weekday{ym / 1} - firstdow) + ((ym / last).day() - day{0})).count();
using namespace date;
return static_cast<unsigned>(
ceil<weeks>((weekday{ym / 1} - firstdow) + ((ym / last).day() - day{0})).count()) +
2;
} }
auto cldGetWeekForLine(const date::year_month& ym, const date::weekday& firstdow, auto cldGetWeekForLine(const year_month& ym, const weekday& firstdow, const unsigned line)
unsigned const line) -> const date::year_month_weekday { -> const year_month_weekday {
unsigned index = line - 2; unsigned index{line - 2};
auto sd = date::sys_days{ym / 1}; if (weekday{ym / 1} == firstdow) ++index;
if (date::weekday{sd} == firstdow) ++index; return ym / firstdow[index];
auto ymdw = ym / firstdow[index];
return ymdw;
} }
auto getCalendarLine(const date::year_month_day& currDate, const date::year_month ym, auto getCalendarLine(const year_month_day& currDate, const year_month ym, const unsigned line,
const unsigned line, const date::weekday& firstdow, const weekday& firstdow, const std::locale* const locale_) -> std::string {
const std::locale* const locale_) -> std::string { std::ostringstream os;
using namespace date::literals;
std::ostringstream res;
switch (line) { switch (line) {
// Print month and year title
case 0: { case 0: {
// Output month and year title os << date::format(*locale_, "{:L%B %Y}", ym);
res << date::format(*locale_, "%B %Y", ym);
break; break;
} }
// Print weekday names title
case 1: { case 1: {
// Output weekday names title
auto wd{firstdow}; auto wd{firstdow};
Glib::ustring wdStr;
Glib::ustring::size_type wdLen{0};
int clen{0};
do { do {
Glib::ustring wd_ustring{date::format(*locale_, "%a", wd)}; wdStr = date::format(*locale_, "{:L%a}", wd);
auto clen{ustring_clen(wd_ustring)}; clen = ustring_clen(wdStr);
auto wd_len{wd_ustring.length()}; wdLen = wdStr.length();
while (clen > 2) { while (clen > 2) {
wd_ustring = wd_ustring.substr(0, wd_len - 1); wdStr = wdStr.substr(0, wdLen - 1);
--wd_len; --wdLen;
clen = ustring_clen(wd_ustring); clen = ustring_clen(wdStr);
} }
const std::string pad(2 - clen, ' '); const std::string pad(2 - clen, ' ');
if (wd != firstdow) res << ' '; if (wd != firstdow) os << ' ';
res << pad << wd_ustring; os << pad << wdStr;
} while (++wd != firstdow); } while (++wd != firstdow);
break; break;
} }
// Print first week prefixed with spaces if necessary
case 2: { case 2: {
// Output first week prefixed with spaces if necessary auto wd{weekday{ym / 1}};
auto wd = date::weekday{ym / 1}; os << std::string((wd - firstdow).count() * 3, ' ');
res << std::string(static_cast<unsigned>((wd - firstdow).count()) * 3, ' ');
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / 1_d) if (currDate != ym / 1d)
res << date::format("%e", 1_d); os << date::format(*locale_, "{:L%e}", 1d);
else else
res << "{today}"; os << "{today}";
auto d = 2_d;
auto d{2d};
while (++wd != firstdow) { while (++wd != firstdow) {
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d) if (currDate != ym / d)
res << date::format(" %e", d); os << date::format(*locale_, " {:L%e}", d);
else else
res << " {today}"; os << " {today}";
++d; ++d;
} }
break; break;
} }
// Print non-first week
default: { default: {
// Output a non-first week: auto ymdTmp{cldGetWeekForLine(ym, firstdow, line)};
auto ymdw{cldGetWeekForLine(ym, firstdow, line)}; if (ymdTmp.ok()) {
if (ymdw.ok()) { auto d{year_month_day{ymdTmp}.day()};
auto d = date::year_month_day{ymdw}.day(); const auto dlast{(ym / last).day()};
auto const e = (ym / last).day(); auto wd{firstdow};
auto wd = firstdow;
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d) if (currDate != ym / d)
res << date::format("%e", d); os << date::format(*locale_, "{:L%e}", d);
else else
res << "{today}"; os << "{today}";
while (++wd != firstdow && ++d <= e) { while (++wd != firstdow && ++d <= dlast) {
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d) if (currDate != ym / d)
res << date::format(" %e", d); os << date::format(*locale_, " {:L%e}", d);
else else
res << " {today}"; os << " {today}";
} }
// Append row with spaces if the week did not complete // Append row with spaces if the week was not completed
res << std::string(static_cast<unsigned>((firstdow - wd).count()) * 3, ' '); os << std::string((firstdow - wd).count() * 3, ' ');
} }
break; break;
} }
} }
return res.str(); return os.str();
} }
auto waybar::modules::Clock::get_calendar(const date::year_month_day& today, auto waybar::modules::Clock::get_calendar(const year_month_day& today, const year_month_day& ymd,
const date::year_month_day& ymd, const time_zone* tz) -> const std::string {
const date::time_zone* tz) -> const std::string { const auto firstdow{first_day_of_week()};
const auto maxRows{12 / cldMonCols_};
const auto ym{ymd.year() / ymd.month()}; const auto ym{ymd.year() / ymd.month()};
const auto y{ymd.year()}; const auto y{ymd.year()};
const auto d{ymd.day()}; const auto d{ymd.day()};
const auto firstdow = first_day_of_week();
const auto maxRows{12 / cldMonCols_};
std::ostringstream os; std::ostringstream os;
std::ostringstream tmp; std::ostringstream tmp;
if (cldMode_ == CldMode::YEAR) { if (cldMode_ == CldMode::YEAR) {
if (y / date::month{1} / 1 == cldYearShift_) if (y / month{1} / 1 == cldYearShift_)
if (d == cldBaseDay_ || (uint)cldBaseDay_ == 0u) if (d == cldBaseDay_ || (uint)cldBaseDay_ == 0u)
return cldYearCached_; return cldYearCached_;
else else
cldBaseDay_ = d; cldBaseDay_ = d;
else else
cldYearShift_ = y / date::month{1} / 1; cldYearShift_ = y / month{1} / 1;
} }
if (cldMode_ == CldMode::MONTH) { if (cldMode_ == CldMode::MONTH) {
if (ym == cldMonShift_) if (ym == cldMonShift_)
@ -330,67 +291,69 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
else else
cldMonShift_ = ym; cldMonShift_ = ym;
} }
// Pad object
const std::string pads(cldWnLen_, ' ');
// Compute number of lines needed for each calendar month // Compute number of lines needed for each calendar month
unsigned ml[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; unsigned ml[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
for (auto& m : ml) { for (auto& m : ml) {
if (cldMode_ == CldMode::YEAR || m == static_cast<unsigned>(ymd.month())) if (cldMode_ == CldMode::YEAR || m == static_cast<unsigned>(ymd.month()))
m = cldRowsInMonth(y / date::month{m}, firstdow); m = cldRowsInMonth(y / month{m}, firstdow);
else else
m = 0u; m = 0u;
} }
for (auto row{0u}; row < maxRows; ++row) { for (auto row{0u}; row < maxRows; ++row) {
const auto lines = *std::max_element(std::begin(ml) + (row * cldMonCols_), const auto lines{*std::max_element(std::begin(ml) + (row * cldMonCols_),
std::begin(ml) + ((row + 1) * cldMonCols_)); std::begin(ml) + ((row + 1) * cldMonCols_))};
for (auto line{0u}; line < lines; ++line) { for (auto line{0u}; line < lines; ++line) {
for (auto col{0u}; col < cldMonCols_; ++col) { for (auto col{0u}; col < cldMonCols_; ++col) {
const auto mon{date::month{row * cldMonCols_ + col + 1}}; const auto mon{month{row * cldMonCols_ + col + 1}};
if (cldMode_ == CldMode::YEAR || y / mon == ym) { if (cldMode_ == CldMode::YEAR || y / mon == ym) {
date::year_month ymTmp{y / mon}; const year_month ymTmp{y / mon};
if (col != 0 && cldMode_ == CldMode::YEAR) os << " "; if (col != 0 && cldMode_ == CldMode::YEAR) os << std::string(3, ' ');
// Week numbers on the left // Week numbers on the left
if (cldWPos_ == WeeksSide::LEFT && line > 0) { if (cldWPos_ == WS::LEFT && line > 0) {
if (line > 1) { if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u]) if (line < ml[(unsigned)ymTmp.month() - 1u])
os << fmt::format(fmt::runtime(fmtMap_[4]), os << fmt_lib::vformat(
(line == 2) locale_, fmtMap_[4],
? date::zoned_seconds{tz, date::local_days{ymTmp / 1}} fmt_lib::make_format_args(
: date::zoned_seconds{tz, date::local_days{cldGetWeekForLine( (line == 2) ? zoned_seconds{tz, local_days{ymTmp / 1}}
ymTmp, firstdow, line)}}) : zoned_seconds{tz, local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}}))
<< ' '; << ' ';
else else
os << std::string(cldWnLen_, ' '); os << pads;
} }
} }
os << fmt::format( os << Glib::ustring::format((cldWPos_ != WS::LEFT || line == 0) ? std::left : std::right,
fmt::runtime((cldWPos_ != WeeksSide::LEFT || line == 0) ? "{:<{}}" : "{:>{}}"), std::setfill(L' '),
getCalendarLine(today, ymTmp, line, firstdow, &locale_), std::setw(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)),
(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0))); getCalendarLine(today, ymTmp, line, firstdow, &locale_));
// Week numbers on the right // Week numbers on the right
if (cldWPos_ == WeeksSide ::RIGHT && line > 0) { if (cldWPos_ == WS::RIGHT && line > 0) {
if (line > 1) { if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u]) if (line < ml[(unsigned)ymTmp.month() - 1u])
os << ' ' os << ' '
<< fmt::format(fmt::runtime(fmtMap_[4]), << fmt_lib::vformat(
(line == 2) locale_, fmtMap_[4],
? date::zoned_seconds{tz, date::local_days{ymTmp / 1}} fmt_lib::make_format_args(
: date::zoned_seconds{tz, date::local_days{cldGetWeekForLine( (line == 2) ? zoned_seconds{tz, local_days{ymTmp / 1}}
ymTmp, firstdow, line)}}); : zoned_seconds{tz, local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}}));
else else
os << std::string(cldWnLen_, ' '); os << pads;
} }
} }
} }
} }
// Apply user's formats
// Apply user formats to calendar
if (line < 2) if (line < 2)
tmp << fmt::format(fmt::runtime(fmtMap_[line]), os.str()); tmp << fmt_lib::vformat(locale_, fmtMap_[line], fmt_lib::make_format_args(os.str()));
else else
tmp << os.str(); tmp << os.str();
// Clear ostringstream // Clear ostringstream
@ -400,10 +363,10 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
if (row + 1u != maxRows && cldMode_ == CldMode::YEAR) tmp << '\n'; if (row + 1u != maxRows && cldMode_ == CldMode::YEAR) tmp << '\n';
} }
os << fmt::format( // Apply days format os << std::regex_replace(
fmt::runtime(fmt::format(fmt::runtime(fmtMap_[2]), tmp.str())), fmt_lib::vformat(locale_, fmtMap_[2], fmt_lib::make_format_args(tmp.str())),
// Apply today format std::regex("\\{today\\}"),
fmt::arg("today", fmt::format(fmt::runtime(fmtMap_[3]), date::format("%e", d)))); fmt_lib::vformat(locale_, fmtMap_[3], fmt_lib::make_format_args(date::format("{:L%e}", d))));
if (cldMode_ == CldMode::YEAR) if (cldMode_ == CldMode::YEAR)
cldYearCached_ = os.str(); cldYearCached_ = os.str();
@ -413,50 +376,34 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
return os.str(); return os.str();
} }
/*Clock actions*/ // Actions handler
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
if (actionMap_[name]) {
(this->*actionMap_[name])();
} else
spdlog::error("Clock. Unsupported action \"{0}\"", name);
}
// Module actions
void waybar::modules::Clock::cldModeSwitch() { void waybar::modules::Clock::cldModeSwitch() {
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR; cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR;
} }
void waybar::modules::Clock::cldShift_up() { void waybar::modules::Clock::cldShift_up() {
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_; cldCurrShift_ += (months)((cldMode_ == CldMode::YEAR) ? 12 : 1);
} }
void waybar::modules::Clock::cldShift_down() { void waybar::modules::Clock::cldShift_down() {
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_; cldCurrShift_ -= (months)((cldMode_ == CldMode::YEAR) ? 12 : 1);
} }
void waybar::modules::Clock::tz_up() { void waybar::modules::Clock::tz_up() {
auto nr_zones = time_zones_.size(); const auto tzSize{tzList_.size()};
if (tzSize == 1) return;
if (nr_zones == 1) return; size_t newIdx{tzCurrIdx_ + 1lu};
tzCurrIdx_ = (newIdx == tzSize) ? 0 : newIdx;
size_t new_idx = current_time_zone_idx_ + 1;
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
} }
void waybar::modules::Clock::tz_down() { void waybar::modules::Clock::tz_down() {
auto nr_zones = time_zones_.size(); const auto tzSize{tzList_.size()};
if (tzSize == 1) return;
if (nr_zones == 1) return; tzCurrIdx_ = (tzCurrIdx_ == 0) ? tzSize - 1 : tzCurrIdx_ - 1;
current_time_zone_idx_ = current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
}
auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point now)
-> std::string {
if (time_zones_.size() == 1) {
return "";
}
std::stringstream os;
for (size_t time_zone_idx = 0; time_zone_idx < time_zones_.size(); ++time_zone_idx) {
if (static_cast<int>(time_zone_idx) == current_time_zone_idx_) {
continue;
}
const date::time_zone* timezone = time_zones_[time_zone_idx];
if (!timezone) {
timezone = date::current_zone();
}
auto ztime = date::zoned_time{timezone, date::floor<std::chrono::seconds>(now)};
os << fmt::format(locale_, fmt::runtime(format_), ztime) << '\n';
}
return os.str();
} }
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
@ -468,17 +415,16 @@ using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
#endif #endif
// Computations done similarly to Linux cal utility. // Computations done similarly to Linux cal utility.
auto waybar::modules::Clock::first_day_of_week() -> date::weekday { auto waybar::modules::Clock::first_day_of_week() -> weekday {
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> posix_locale{ deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> posix_locale{
newlocale(LC_ALL, locale_.name().c_str(), nullptr)}; newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
if (posix_locale) { if (posix_locale) {
const int i = (std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()); const auto i{(int)((std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()))};
auto ymd = date::year(i / 10000) / (i / 100 % 100) / (i % 100); const weekday wd{year_month_day{year(i / 10000) / month(i / 100 % 100) / day(i % 100)}};
auto wd = date::weekday(ymd); const auto j{(uint8_t)*nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get())};
uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get()); return wd + days{j - 1};
return wd + date::days(j - 1);
} }
#endif #endif
return date::Sunday; return Sunday;
} }

View File

@ -1,6 +1,5 @@
#include "util/date.hpp" #include "util/date.hpp"
#include <chrono>
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
@ -20,13 +19,13 @@
#endif #endif
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
namespace fmt_lib = waybar::util::date::format;
/* /*
* Check that the date/time formatter with locale and timezone support is working as expected. * Check that the date/time formatter with locale and timezone support is working as expected.
*/ */
const date::zoned_time<std::chrono::seconds> TEST_TIME = date::zoned_time{ const zoned_time<std::chrono::seconds> TEST_TIME{
"UTC", date::local_days{date::Monday[1] / date::January / 2022} + 13h + 4min + 5s}; "UTC", local_days{Monday[1] / January / 2022} + 13h + 4min + 5s};
/* /*
* Check if the date formatted with LC_TIME=en_US is within expectations. * Check if the date formatted with LC_TIME=en_US is within expectations.
@ -52,10 +51,11 @@ static const bool LC_TIME_is_sane = []() {
TEST_CASE("Format UTC time", "[clock][util]") { TEST_CASE("Format UTC time", "[clock][util]") {
const auto loc = std::locale("C"); const auto loc = std::locale("C");
const auto tm = TEST_TIME; const auto tm = TEST_TIME;
#if not HAVE_CHRONO_TIMEZONES
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 13:04:05 2022 UTC"); #endif
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405"); CHECK(fmt_lib::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 13:04:05 2022 UTC");
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103130405");
if (!LC_TIME_is_sane) { if (!LC_TIME_is_sane) {
SKIP("Locale support check failed, skip tests"); SKIP("Locale support check failed, skip tests");
@ -66,11 +66,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
try { try {
const auto loc = std::locale("en_US.UTF-8"); const auto loc = std::locale("en_US.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "01/03/2022 01:04:05 PM"); CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405"); #else
CHECK(fmt_lib::format(loc, "{:%F %r}", tm) == "2022-01-03 01:04:05 PM");
#endif
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103130405");
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests"); WARN("Locale en_US not found, skip tests");
} }
@ -79,11 +83,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
try { try {
const auto loc = std::locale("en_GB.UTF-8"); const auto loc = std::locale("en_GB.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 13:04:05")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 13:04:05"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "03/01/22 13:04:05"); CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "03/01/22 13:04:05");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405"); #else
CHECK(fmt_lib::format(loc, "{:%F %T}", tm) == "2022-01-03 13:04:05");
#endif
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103130405");
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
WARN("Locale en_GB not found, skip tests"); WARN("Locale en_GB not found, skip tests");
} }
@ -92,11 +100,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
try { try {
const auto loc = std::locale::global(std::locale("en_US.UTF-8")); const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
CHECK(fmt::format("{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format("{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 01:04:05 PM"));
CHECK(fmt::format("{:%x %X}", tm) == "01/03/2022 01:04:05 PM"); CHECK(fmt_lib::format("{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
CHECK(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405"); #else
CHECK(fmt_lib::format("{:%F %r}", tm) == "2022-01-03 01:04:05 PM");
#endif
CHECK(fmt_lib::format("{:%Y%m%d%H%M%S}", tm) == "20220103130405");
std::locale::global(loc); std::locale::global(loc);
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
@ -107,11 +119,13 @@ TEST_CASE("Format UTC time", "[clock][util]") {
TEST_CASE("Format zoned time", "[clock][util]") { TEST_CASE("Format zoned time", "[clock][util]") {
const auto loc = std::locale("C"); const auto loc = std::locale("C");
const auto tm = date::zoned_time{"America/New_York", TEST_TIME}; const auto tm = zoned_time{"America/New_York", TEST_TIME};
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 08:04:05 2022 EST"); CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405"); #endif
CHECK(fmt_lib::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 08:04:05 2022 EST");
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103080405");
if (!LC_TIME_is_sane) { if (!LC_TIME_is_sane) {
SKIP("Locale support check failed, skip tests"); SKIP("Locale support check failed, skip tests");
@ -122,11 +136,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
try { try {
const auto loc = std::locale("en_US.UTF-8"); const auto loc = std::locale("en_US.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "01/03/2022 08:04:05 AM"); CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405"); #else
CHECK(fmt_lib::format(loc, "{:%F %r}", tm) == "2022-01-03 08:04:05 AM");
#endif
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103080405");
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
WARN("Locale en_US not found, skip tests"); WARN("Locale en_US not found, skip tests");
} }
@ -135,11 +153,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
try { try {
const auto loc = std::locale("en_GB.UTF-8"); const auto loc = std::locale("en_GB.UTF-8");
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format(loc, "{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05"));
CHECK(fmt::format(loc, "{:%x %X}", tm) == "03/01/22 08:04:05"); CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "03/01/22 08:04:05");
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405"); #else
CHECK(fmt_lib::format(loc, "{:%F %T}", tm) == "2022-01-03 08:04:05");
#endif
CHECK(fmt_lib::format(loc, "{:%Y%m%d%H%M%S}", tm) == "20220103080405");
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {
WARN("Locale en_GB not found, skip tests"); WARN("Locale en_GB not found, skip tests");
} }
@ -148,11 +170,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
try { try {
const auto loc = std::locale::global(std::locale("en_US.UTF-8")); const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
CHECK(fmt::format("{}", tm).empty()); // no format specified #if not HAVE_CHRONO_TIMEZONES
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704 CHECK(fmt_lib::format("{}", tm).empty()); // no format specified
CHECK_THAT(fmt_lib::format("{:%c}", tm), // HowardHinnant/date#704
Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM")); Catch::Matchers::StartsWith("Mon 03 Jan 2022 08:04:05 AM"));
CHECK(fmt::format("{:%x %X}", tm) == "01/03/2022 08:04:05 AM"); CHECK(fmt_lib::format("{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
CHECK(fmt::format("{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405"); #else
CHECK(fmt_lib::format("{:%F %r}", tm) == "2022-01-03 08:04:05 AM");
#endif
CHECK(fmt_lib::format("{:%Y%m%d%H%M%S}", tm) == "20220103080405");
std::locale::global(loc); std::locale::global(loc);
} catch (const std::runtime_error &) { } catch (const std::runtime_error &) {