From 93e340a081bcf32ba775baf11a2aaa468af99239 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Mon, 16 Jan 2023 17:30:06 -0800 Subject: [PATCH] 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. --- include/factory.hpp | 2 +- include/modules/clock.hpp | 3 +-- include/util/date.hpp | 28 +++++++++++++++++++++++++++- meson.build | 22 ++++++++++++++++------ src/modules/clock.cpp | 1 - 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/include/factory.hpp b/include/factory.hpp index 21dc647..558a8d4 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -1,7 +1,7 @@ #pragma once #include -#ifdef HAVE_LIBDATE +#if defined(HAVE_CHRONO_TIMEZONES) || defined(HAVE_LIBDATE) #include "modules/clock.hpp" #else #include "modules/simpleclock.hpp" diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index c97565d..a7290f6 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -1,8 +1,7 @@ #pragma once -#include - #include "ALabel.hpp" +#include "util/date.hpp" #include "util/sleeper_thread.hpp" namespace waybar::modules { diff --git a/include/util/date.hpp b/include/util/date.hpp index ec948bd..380bb6e 100644 --- a/include/util/date.hpp +++ b/include/util/date.hpp @@ -1,8 +1,34 @@ #pragma once -#include #include +#if HAVE_CHRONO_TIMEZONES +#include +#include + +/* Compatibility layer for on top of C++20 */ +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 +#endif + template struct fmt::formatter> { std::string_view specs; diff --git a/meson.build b/meson.build index 7b63181..32bfd2b 100644 --- a/meson.build +++ b/meson.build @@ -123,11 +123,18 @@ gtk_layer_shell = dependency('gtk-layer-shell-0', required: get_option('gtk-layer-shell'), fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep']) systemd = dependency('systemd', required: get_option('systemd')) -tz_dep = dependency('date', - required: false, - default_options : [ 'use_system_tzdb=true' ], - modules : [ 'date::date', 'date::date-tz' ], - fallback: [ 'date', 'tz_dep' ]) + +cpp_lib_chrono = compiler.compute_int('__cpp_lib_chrono', prefix : '#include ') +have_chrono_timezones = cpp_lib_chrono >= 201907 +if have_chrono_timezones + tz_dep = declare_dependency() +else + tz_dep = dependency('date', + required: false, + default_options : [ 'use_system_tzdb=true' ], + modules : [ 'date::date', 'date::date-tz' ], + fallback: [ 'date', 'tz_dep' ]) +endif prefix = get_option('prefix') sysconfdir = get_option('sysconfdir') @@ -312,7 +319,10 @@ if get_option('rfkill').enabled() and is_linux ) 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') src_files += 'src/modules/clock.cpp' else diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 0dbd255..76ec73c 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -9,7 +9,6 @@ #include #include -#include "util/date.hpp" #include "util/ustring_clen.hpp" #ifdef HAVE_LANGINFO_1STDAY #include