From 8bc53d3272eb0246c8e00f6d9f65619679a0d9f7 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 23:01:36 +0200 Subject: [PATCH] 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"); +```