$darkmode
duration.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 
25 #include <chrono> // for duration<>, duration_cast<>, nanoseconds
26 #include <ratio> // for micro, milli
27 #include <string> // for string
28 
29 #include <nlohmann/json.hpp> // for Json, adl_serializer
30 
31 namespace cloe {
32 
43 using Duration = std::chrono::nanoseconds;
44 
45 using Microseconds = std::chrono::duration<double, std::micro>;
46 using Milliseconds = std::chrono::duration<double, std::milli>;
47 using Seconds = std::chrono::duration<double>;
48 
49 std::string to_string(const Duration& ns);
50 
68 Duration parse_duration(const std::string& fmt);
69 
70 // Occasionally, we want to have a human-readable and a machine-readable
71 // representation of a duration. This function does exactly that.
72 nlohmann::json to_convenient_json(const Duration& ns);
73 
74 } // namespace cloe
75 
76 /*
77  * In order to provide serialization for third-party types, we need to either
78  * use their namespace or provide a specialization in that of nlohmann. It is
79  * illegal to define anything in the std namespace, so we are left no choice in
80  * this regard.
81  *
82  * See: https://github.com/nlohmann/json
83  */
84 namespace nlohmann {
85 
86 inline std::string to_string_hr(double d) {
87  auto n = std::to_string(d);
88  n.erase(n.find_last_not_of('0') + 1, std::string::npos);
89  if (n[n.size() - 1] == '.') {
90  n.pop_back();
91  }
92  return n;
93 }
94 
95 template <>
96 struct adl_serializer<cloe::Microseconds> {
97  static void to_json(json& j, const cloe::Microseconds& us) {
98  j = to_string_hr(us.count()) + "us";
99  }
100 };
101 
102 template <>
103 struct adl_serializer<cloe::Milliseconds> {
104  static void to_json(json& j, const cloe::Milliseconds& ms) {
105  j = to_string_hr(ms.count()) + "ms";
106  }
107 };
108 
109 template <>
110 struct adl_serializer<cloe::Seconds> {
111  static void to_json(json& j, const cloe::Seconds& s) { j = to_string_hr(s.count()) + "s"; }
112 };
113 
114 template <>
115 struct adl_serializer<cloe::Duration> {
116  static void to_json(json& j, const cloe::Duration& ns) {
117  auto count = ns.count();
118  if (count > 1e9) {
119  j = std::chrono::duration_cast<cloe::Seconds>(ns);
120  } else if (count > 1e6) {
121  j = std::chrono::duration_cast<cloe::Milliseconds>(ns);
122  } else if (count > 1e3) {
123  j = std::chrono::duration_cast<cloe::Microseconds>(ns);
124  } else {
125  j = to_string_hr(count) + "ns";
126  }
127  }
128 };
129 
130 } // namespace nlohmann
Duration parse_duration(const std::string &s)
Definition: chrono.hpp:56
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36