fix(util): make waybar_time formatter compatible with fmt 8.1.0

Stop using private implementation details of the `formatter<std::tm>`.
We never needed anything from the class besides the format specifier,
which is easily obtainable with public API.
This commit is contained in:
Aleksei Bavshin 2022-01-07 22:25:15 -08:00
parent 1489a539f8
commit 7f6bef2049
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
2 changed files with 28 additions and 12 deletions

View File

@ -1,12 +1,7 @@
#pragma once
#include <fmt/format.h>
#if FMT_VERSION < 60000
#include <fmt/time.h>
#else
#include <fmt/chrono.h>
#endif
#include <date/tz.h>
#include <fmt/format.h>
namespace waybar {
@ -18,12 +13,27 @@ struct waybar_time {
} // namespace waybar
template <>
struct fmt::formatter<waybar::waybar_time> : fmt::formatter<std::tm> {
struct fmt::formatter<waybar::waybar_time> {
std::string_view specs;
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin();
if (it != ctx.end() && *it == ':') {
++it;
}
auto end = it;
while (end != ctx.end() && *end != '}') {
++end;
}
if (end != it) {
specs = {it, std::string_view::size_type(end - it)};
}
return end;
}
template <typename FormatContext>
auto format(const waybar::waybar_time& t, FormatContext& ctx) {
#if FMT_VERSION >= 80000
auto& tm_format = specs;
#endif
return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(tm_format), t.ztime));
return format_to(ctx.out(), "{}", date::format(t.locale, fmt::to_string(specs), t.ztime));
}
};

View File

@ -1,10 +1,16 @@
#include "modules/clock.hpp"
#include <time.h>
#include <spdlog/spdlog.h>
#if FMT_VERSION < 60000
#include <fmt/time.h>
#else
#include <fmt/chrono.h>
#endif
#include <ctime>
#include <sstream>
#include <type_traits>
#include "util/ustring_clen.hpp"
#include "util/waybar_time.hpp"
#ifdef HAVE_LANGINFO_1STDAY