feat(clock): support chrono Time Zone extensions.

Use chrono Calendars and Time Zones (P0355R7, P1466R3) when available
instead of the `date` library.
Verified with a patched build of a recent GCC 13 snapshot.
This commit is contained in:
Aleksei Bavshin 2023-01-16 17:30:06 -08:00
parent 6225db0a48
commit 93e340a081
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
5 changed files with 45 additions and 11 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <json/json.h> #include <json/json.h>
#ifdef HAVE_LIBDATE #if defined(HAVE_CHRONO_TIMEZONES) || defined(HAVE_LIBDATE)
#include "modules/clock.hpp" #include "modules/clock.hpp"
#else #else
#include "modules/simpleclock.hpp" #include "modules/simpleclock.hpp"

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <date/tz.h>
#include "ALabel.hpp" #include "ALabel.hpp"
#include "util/date.hpp"
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
namespace waybar::modules { namespace waybar::modules {

View File

@ -1,8 +1,34 @@
#pragma once #pragma once
#include <date/tz.h>
#include <fmt/format.h> #include <fmt/format.h>
#if HAVE_CHRONO_TIMEZONES
#include <chrono>
#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
#include <date/tz.h>
#endif
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;

View File

@ -123,11 +123,18 @@ gtk_layer_shell = dependency('gtk-layer-shell-0',
required: get_option('gtk-layer-shell'), required: get_option('gtk-layer-shell'),
fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep'])
systemd = dependency('systemd', required: get_option('systemd')) systemd = dependency('systemd', required: get_option('systemd'))
tz_dep = dependency('date',
cpp_lib_chrono = compiler.compute_int('__cpp_lib_chrono', prefix : '#include <chrono>')
have_chrono_timezones = cpp_lib_chrono >= 201907
if have_chrono_timezones
tz_dep = declare_dependency()
else
tz_dep = dependency('date',
required: false, required: false,
default_options : [ 'use_system_tzdb=true' ], default_options : [ 'use_system_tzdb=true' ],
modules : [ 'date::date', 'date::date-tz' ], modules : [ 'date::date', 'date::date-tz' ],
fallback: [ 'date', 'tz_dep' ]) fallback: [ 'date', 'tz_dep' ])
endif
prefix = get_option('prefix') prefix = get_option('prefix')
sysconfdir = get_option('sysconfdir') sysconfdir = get_option('sysconfdir')
@ -312,7 +319,10 @@ if get_option('rfkill').enabled() and is_linux
) )
endif endif
if tz_dep.found() if have_chrono_timezones
add_project_arguments('-DHAVE_CHRONO_TIMEZONES', language: 'cpp')
src_files += 'src/modules/clock.cpp'
elif tz_dep.found()
add_project_arguments('-DHAVE_LIBDATE', language: 'cpp') add_project_arguments('-DHAVE_LIBDATE', language: 'cpp')
src_files += 'src/modules/clock.cpp' src_files += 'src/modules/clock.cpp'
else else

View File

@ -9,7 +9,6 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#include "util/date.hpp"
#include "util/ustring_clen.hpp" #include "util/ustring_clen.hpp"
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
#include <langinfo.h> #include <langinfo.h>