ORE Studio 0.0.4
Loading...
Searching...
No Matches
time_point_parser.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_UTILITY_RFL_TIME_POINT_PARSER_HPP
21#define ORES_UTILITY_RFL_TIME_POINT_PARSER_HPP
22
23#include <chrono>
24#include <string>
25#include <rfl.hpp>
26#include "ores.platform/time/datetime.hpp"
27
28namespace rfl::parsing {
29
30template <class ReaderType, class WriterType, class ProcessorsType>
31struct Parser<ReaderType, WriterType,
32 std::chrono::system_clock::time_point, ProcessorsType> {
33 using InputVarType = typename ReaderType::InputVarType;
34 using OutputVarType = typename WriterType::OutputVarType;
35
36 // std::chrono::system_clock::time_point is always UTC by definition (C++20).
37 // We serialise with a 'Z' suffix and parse with UTC interpretation so that
38 // server and client agree regardless of the host machine's local timezone.
39 static Result<std::chrono::system_clock::time_point> read(
40 const ReaderType& _r, const InputVarType& _var) noexcept {
41 const auto str_result = Parser<ReaderType, WriterType,
42 std::string, ProcessorsType>::read(_r, _var);
43 if (!str_result) {
44 return rfl::Unexpected(Error(str_result.error().what()));
45 }
46 try {
48 str_result.value());
49 } catch (const std::exception& e) {
50 return rfl::Unexpected(Error(e.what()));
51 }
52 }
53
54 template <class P>
55 static void write(const WriterType& _w,
56 const std::chrono::system_clock::time_point& _tp,
57 const P& _parent) noexcept {
59 Parser<ReaderType, WriterType, std::string, ProcessorsType>::write(
60 _w, str, _parent);
61 }
62
63 // time_point serialises as an ISO 8601 UTC string — delegate schema
64 // generation to the string parser so rfl::json::to_schema works correctly.
65 template <class SchemaType>
66 static SchemaType to_schema(
67 std::map<std::string, SchemaType>* _definitions) {
68 return Parser<ReaderType, WriterType,
69 std::string, ProcessorsType>::to_schema(_definitions);
70 }
71};
72
73}
74
75#endif
STL namespace.
static std::chrono::system_clock::time_point from_iso8601_utc(const std::string &str)
Parses an ISO 8601 UTC string to a time point.
Definition datetime.cpp:45
static std::string to_iso8601_utc(const std::chrono::system_clock::time_point &tp)
Serialises a time point to ISO 8601 UTC string with 'Z' suffix.
Definition datetime.cpp:30