File tree Expand file tree Collapse file tree
include/userver/chaotic/io/std/chrono
src/chaotic/io/std/chrono Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,10 +14,13 @@ USERVER_NAMESPACE_BEGIN
1414namespace chaotic ::convert {
1515
1616template <typename T>
17+ requires std::is_integral_v<T> || std::is_floating_point_v<T>
1718T Convert (std::chrono::milliseconds value, chaotic::convert::To<T>) {
1819 return utils::numeric_cast<T>(value.count ());
1920}
2021
22+ std::string Convert (const std::chrono::milliseconds& value, chaotic::convert::To<std::string>);
23+
2124std::chrono::milliseconds Convert (const std::string& str, chaotic::convert::To<std::chrono::milliseconds>);
2225
2326std::chrono::milliseconds Convert (std::string_view str, chaotic::convert::To<std::chrono::milliseconds>);
Original file line number Diff line number Diff line change @@ -28,6 +28,9 @@ definitions:
2828 integer :
2929 type : integer
3030 x-usrv-cpp-type : std::chrono::milliseconds
31+ ms :
32+ type : string
33+ x-usrv-cpp-type : std::chrono::milliseconds
3134 sizet :
3235 type : integer
3336 minimum : 0
Original file line number Diff line number Diff line change @@ -22,6 +22,18 @@ TEST(Custom, Int) {
2222 EXPECT_EQ (custom2, custom);
2323}
2424
25+ TEST (Custom, Ms) {
26+ auto json = formats::json::MakeObject (" ms" , " 12ms" );
27+ auto custom = json.As <ns::ObjWithCustom>();
28+ EXPECT_EQ (custom.ms , std::chrono::milliseconds (12 ));
29+
30+ auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
31+ EXPECT_EQ (json_back, json);
32+
33+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
34+ EXPECT_EQ (custom2, custom);
35+ }
36+
2537TEST (Custom, String) {
2638 auto json = formats::json::MakeObject (" string" , " make love" );
2739 auto custom = json.As <ns::ObjWithCustom>();
Original file line number Diff line number Diff line change 22
33#include < userver/utils/string_to_duration.hpp>
44
5+ #include < fmt/format.h>
6+
57USERVER_NAMESPACE_BEGIN
68
79namespace chaotic ::convert {
810
11+ std::string Convert (const std::chrono::milliseconds& value, chaotic::convert::To<std::string>) {
12+ return fmt::format (" {}ms" , value.count ());
13+ }
14+
915std::chrono::milliseconds Convert (const std::string& str, chaotic::convert::To<std::chrono::milliseconds>) {
1016 return std::chrono::milliseconds{utils::StringToDuration (str)};
1117}
You can’t perform that action at this time.
0 commit comments