From 7bb2267f8918988a1245694a8cf294a9dbc2a0e6 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:30:44 +0200 Subject: [PATCH 1/7] Added cpp --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5b845f08..3712d564 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ This results in the following JSON string: We can also create structs from the string: -``` +```cpp auto homer2 = rfl::json::read(json_string).value(); // Fields can be accessed like this: From 59e864e9d25b1dec6d680dec046f297592c49cd6 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:31:20 +0200 Subject: [PATCH 2/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3712d564..db63886a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Design principles for reflect-cpp include: #include #include -// "firstName", "lastName" and "children" are the field names +// "firstName", "lastName", "birthday" and "children" are the field names // as they will appear in the JSON. The C++ standard is // snake case, the JSON standard is camel case, so the names // will not always be identical. From fcd018b142fb2a1cf1deac1a55af90ae41f3377e Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:46:22 +0200 Subject: [PATCH 3/7] Added documentation on rfl::Timestamp --- docs/timestamps.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 docs/timestamps.md diff --git a/docs/timestamps.md b/docs/timestamps.md new file mode 100644 index 00000000..95fcb288 --- /dev/null +++ b/docs/timestamps.md @@ -0,0 +1,30 @@ +# `rfl::Timestamp` + +`rfl::Timestamp` is a simple wrapper around `std::tm` (https://en.cppreference.com/w/cpp/chrono/c/tm). + +As its template parameter it includes the time stamp format for serialization and deserialization. + +Here is a reference for for how you can specify the format: + +https://en.cppreference.com/w/cpp/chrono/c/strftime + +In fact, `rfl::Timestamp` does call `strftime` and `strptime` under-the-hood. + +They can be used in your struct like this: + +```cpp +struct Person { + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; +}; +``` + +An `rfl::Timestamp` can be constructed from either a string or a `std::tm` struct. + +You can access the underlying `std::tm` struct using the `.tm()` method and you can generate +the string representation using the `.str()` method. + +Note that constructing `rfl::Timestamp` from a string is convenient, but it might result in +an exception. + +To avoid the exception you can also use `Timestamp<...>::from_string(...)`, which will +return an `rfl::Result>` or `rfl::Error`. From be7eb55fc9ae8947e96f8f2a88c28d7ad1ded3e7 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:46:57 +0200 Subject: [PATCH 4/7] Update README.md --- docs/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/README.md b/docs/README.md index 69301885..5007e48a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,9 @@ 1.6) [rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_ref.md) - For defining recursive structures. +1.7) [rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps. + + ## 2) Custom classes 2.1) [Custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) - For custom classes with private fields. From 20b8d1e303715ca450229d296d9752703bad2223 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:47:11 +0200 Subject: [PATCH 5/7] Update README.md --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 5007e48a..41029c1d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,7 +16,7 @@ 1.6) [rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_ref.md) - For defining recursive structures. -1.7) [rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps. +1.7) [rfl::Timestamp](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps. ## 2) Custom classes From 97201bb48f48c58c56945dfc4185ff385f3d0ed7 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 21:47:49 +0200 Subject: [PATCH 6/7] Update timestamps.md --- docs/timestamps.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/timestamps.md b/docs/timestamps.md index 95fcb288..168acddb 100644 --- a/docs/timestamps.md +++ b/docs/timestamps.md @@ -8,7 +8,7 @@ Here is a reference for for how you can specify the format: https://en.cppreference.com/w/cpp/chrono/c/strftime -In fact, `rfl::Timestamp` does call `strftime` and `strptime` under-the-hood. +In fact, `rfl::Timestamp` calls `strftime` and `strptime` under-the-hood. They can be used in your struct like this: From 8bc53d3272eb0246c8e00f6d9f65619679a0d9f7 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 23:01:36 +0200 Subject: [PATCH 7/7] Added some clarifying examples --- docs/timestamps.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/timestamps.md b/docs/timestamps.md index 168acddb..c5e95d0c 100644 --- a/docs/timestamps.md +++ b/docs/timestamps.md @@ -20,11 +20,26 @@ struct Person { An `rfl::Timestamp` can be constructed from either a string or a `std::tm` struct. +```cpp +const auto person1 = Person{.birthday = "1970-01-01"}; +const auto person2 = Person{.birthday = std::tm{...}}; +``` + You can access the underlying `std::tm` struct using the `.tm()` method and you can generate the string representation using the `.str()` method. +```cpp +const std::tm birthday = person1.birthday().tm(); +std::cout << person1.birthday().str() << std::endl; +``` + Note that constructing `rfl::Timestamp` from a string is convenient, but it might result in an exception. To avoid the exception you can also use `Timestamp<...>::from_string(...)`, which will return an `rfl::Result>` or `rfl::Error`. + +```cpp +const rfl::Result> result = rfl::Timestamp<"%Y-%m-%d">::from_string("1970-01-01"); +const rfl::Result> error = rfl::Timestamp<"%Y-%m-%d">::from_string("not a proper time format"); +```