From 7fbc101a14177d89df1cef6e97bb488a52372f3f Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Mon, 26 Jan 2026 14:35:59 +0100 Subject: [PATCH 1/2] Support parsing arbitrary duration representations --- include/rfl/parsing/Parser_duration.hpp | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/include/rfl/parsing/Parser_duration.hpp b/include/rfl/parsing/Parser_duration.hpp index 4656444b..2f205ded 100644 --- a/include/rfl/parsing/Parser_duration.hpp +++ b/include/rfl/parsing/Parser_duration.hpp @@ -99,43 +99,46 @@ struct Parser, ProcessorsType> { } } + template + inline static constexpr bool samePeriodAs = + std::is_same_v; + static auto make_unit() noexcept { - if constexpr (std::is_same_v) { + if constexpr (samePeriodAs) { return Unit::make<"nanoseconds">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"microseconds">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"milliseconds">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"seconds">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"minutes">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"hours">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"days">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"weeks">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"months">(); - } else if constexpr (std::is_same_v) { + } else if constexpr (samePeriodAs) { return Unit::make<"years">(); } else { static_assert(always_false_v, "Unsupported type."); } - }; + } }; } // namespace rfl::parsing From 76c8ec6e988df3c54eff57d471a26233895e6002 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Tue, 27 Jan 2026 10:05:01 +0100 Subject: [PATCH 2/2] Add chrono duration test --- tests/json/test_chrono_duration.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/json/test_chrono_duration.cpp diff --git a/tests/json/test_chrono_duration.cpp b/tests/json/test_chrono_duration.cpp new file mode 100644 index 00000000..c78a11c7 --- /dev/null +++ b/tests/json/test_chrono_duration.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_chrono_duration { + +struct TestStruct { + std::chrono::milliseconds ms_duration; + + // Not std::chrono::seconds, but should be treated as such. + std::chrono::duration> custom_duration; +}; + +TEST(json, test_chrono_duration) { + const auto test = TestStruct{ + .ms_duration = std::chrono::milliseconds(1500), + .custom_duration = std::chrono::duration>(10), + }; + + write_and_read( + test, + R"({"ms_duration":{"count":1500,"unit":"milliseconds"},"custom_duration":{"count":10,"unit":"seconds"}})"); +} + +} // namespace test_chrono_duration