mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-13 14:42:29 +02:00
c++20. clock chrono API. STL + format
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
#include "util/date.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
@ -20,13 +19,13 @@
|
||||
#endif
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
const date::zoned_time<std::chrono::seconds> TEST_TIME = date::zoned_time{
|
||||
"UTC", date::local_days{date::Monday[1] / date::January / 2022} + 13h + 4min + 5s};
|
||||
const zoned_time<std::chrono::seconds> TEST_TIME{
|
||||
"UTC", local_days{Monday[1] / January / 2022} + 13h + 4min + 5s};
|
||||
|
||||
/*
|
||||
* 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]") {
|
||||
const auto loc = std::locale("C");
|
||||
const auto tm = TEST_TIME;
|
||||
|
||||
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
|
||||
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 13:04:05 2022 UTC");
|
||||
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103130405");
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
|
||||
#endif
|
||||
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) {
|
||||
SKIP("Locale support check failed, skip tests");
|
||||
@ -66,11 +66,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale("en_US.UTF-8");
|
||||
|
||||
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
|
||||
#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 &) {
|
||||
WARN("Locale en_US not found, skip tests");
|
||||
}
|
||||
@ -79,11 +83,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale("en_GB.UTF-8");
|
||||
|
||||
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "03/01/22 13:04:05");
|
||||
#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 &) {
|
||||
WARN("Locale en_GB not found, skip tests");
|
||||
}
|
||||
@ -92,11 +100,15 @@ TEST_CASE("Format UTC time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
|
||||
|
||||
CHECK(fmt::format("{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format("{:%x %X}", tm) == "01/03/2022 01:04:05 PM");
|
||||
#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);
|
||||
} catch (const std::runtime_error &) {
|
||||
@ -107,11 +119,13 @@ TEST_CASE("Format UTC time", "[clock][util]") {
|
||||
|
||||
TEST_CASE("Format zoned time", "[clock][util]") {
|
||||
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
|
||||
CHECK(fmt::format(loc, "{:%c %Z}", tm) == "Mon Jan 3 08:04:05 2022 EST");
|
||||
CHECK(fmt::format(loc, "{arg:%Y%m%d%H%M%S}", fmt::arg("arg", tm)) == "20220103080405");
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
CHECK(fmt_lib::format(loc, "{}", tm).empty()); // no format specified
|
||||
#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) {
|
||||
SKIP("Locale support check failed, skip tests");
|
||||
@ -122,11 +136,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale("en_US.UTF-8");
|
||||
|
||||
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
|
||||
#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 &) {
|
||||
WARN("Locale en_US not found, skip tests");
|
||||
}
|
||||
@ -135,11 +153,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale("en_GB.UTF-8");
|
||||
|
||||
CHECK(fmt::format(loc, "{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format(loc, "{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format(loc, "{:%x %X}", tm) == "03/01/22 08:04:05");
|
||||
#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 &) {
|
||||
WARN("Locale en_GB not found, skip tests");
|
||||
}
|
||||
@ -148,11 +170,15 @@ TEST_CASE("Format zoned time", "[clock][util]") {
|
||||
try {
|
||||
const auto loc = std::locale::global(std::locale("en_US.UTF-8"));
|
||||
|
||||
CHECK(fmt::format("{}", tm).empty()); // no format specified
|
||||
CHECK_THAT(fmt::format("{:%c}", tm), // HowardHinnant/date#704
|
||||
#if not HAVE_CHRONO_TIMEZONES
|
||||
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"));
|
||||
CHECK(fmt::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");
|
||||
CHECK(fmt_lib::format("{:%x %X}", tm) == "01/03/2022 08:04:05 AM");
|
||||
#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);
|
||||
} catch (const std::runtime_error &) {
|
||||
|
Reference in New Issue
Block a user