time.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2024 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_UTIL_TIME_HPP
23 #define NDN_CXX_UTIL_TIME_HPP
24 
25 #include <boost/asio/wait_traits.hpp>
26 #include <boost/chrono.hpp>
27 
28 #include <cstdint>
29 #include <limits>
30 #include <locale>
31 #include <string>
32 #include <type_traits>
33 
34 namespace ndn {
35 namespace time {
36 
37 template<typename Rep, typename Period>
38 using duration = ::boost::chrono::duration<Rep, Period>;
39 
40 using ::boost::chrono::duration_cast;
41 
42 // C++20
47 
48 // C++11
55 
61 template<typename Rep, typename Period, typename = std::enable_if_t<std::numeric_limits<Rep>::is_signed>>
62 constexpr duration<Rep, Period>
64 {
65  return d >= d.zero() ? d : -d;
66 }
67 
68 } // namespace time
69 
70 inline namespace literals {
71 inline namespace time_literals {
72 
73 constexpr time::days
74 operator ""_day(unsigned long long days)
75 {
76  return time::days{days};
77 }
78 
79 constexpr time::duration<long double, time::days::period>
80 operator ""_day(long double days)
81 {
83 }
84 
85 constexpr time::days
86 operator ""_days(unsigned long long days)
87 {
88  return time::days{days};
89 }
90 
91 constexpr time::duration<long double, time::days::period>
92 operator ""_days(long double days)
93 {
95 }
96 
97 constexpr time::hours
98 operator ""_h(unsigned long long hrs)
99 {
100  return time::hours{hrs};
101 }
102 
103 constexpr time::duration<long double, time::hours::period>
104 operator ""_h(long double hrs)
105 {
107 }
108 
109 constexpr time::minutes
110 operator ""_min(unsigned long long mins)
111 {
112  return time::minutes{mins};
113 }
114 
115 constexpr time::duration<long double, time::minutes::period>
116 operator ""_min(long double mins)
117 {
119 }
120 
121 constexpr time::seconds
122 operator ""_s(unsigned long long secs)
123 {
124  return time::seconds{secs};
125 }
126 
127 constexpr time::duration<long double, time::seconds::period>
128 operator ""_s(long double secs)
129 {
131 }
132 
133 constexpr time::milliseconds
134 operator ""_ms(unsigned long long msecs)
135 {
136  return time::milliseconds{msecs};
137 }
138 
139 constexpr time::duration<long double, time::milliseconds::period>
140 operator ""_ms(long double msecs)
141 {
143 }
144 
145 constexpr time::microseconds
146 operator ""_us(unsigned long long usecs)
147 {
148  return time::microseconds{usecs};
149 }
150 
151 constexpr time::duration<long double, time::microseconds::period>
152 operator ""_us(long double usecs)
153 {
155 }
156 
157 constexpr time::nanoseconds
158 operator ""_ns(unsigned long long nsecs)
159 {
160  return time::nanoseconds{nsecs};
161 }
162 
163 constexpr time::duration<long double, time::nanoseconds::period>
164 operator ""_ns(long double nsecs)
165 {
167 }
168 
169 } // inline namespace time_literals
170 } // inline namespace literals
171 
172 namespace time {
173 
174 using namespace ::ndn::literals::time_literals;
175 
200 {
201 public:
203  using rep = duration::rep;
204  using period = duration::period;
205  using time_point = ::boost::chrono::time_point<system_clock>;
206  static constexpr bool is_steady = ::boost::chrono::system_clock::is_steady;
207 
208  static time_point
209  now() noexcept;
210 
211  static std::time_t
212  to_time_t(const time_point& t) noexcept;
213 
214  static time_point
215  from_time_t(std::time_t t) noexcept;
216 };
217 
227 {
228 public:
230  using rep = duration::rep;
231  using period = duration::period;
232  using time_point = ::boost::chrono::time_point<steady_clock>;
233  static constexpr bool is_steady = true;
234 
235  static time_point
236  now() noexcept;
237 
238 private:
247  static duration
248  to_wait_duration(duration d);
249 
250  friend struct boost::asio::wait_traits<steady_clock>; // see steady-timer.hpp
251 };
252 
257 const system_clock::time_point&
258 getUnixEpoch();
259 
263 template<typename Duration = milliseconds>
264 constexpr Duration
266 {
267  return duration_cast<Duration>(tp.time_since_epoch());
268 }
269 
273 constexpr system_clock::time_point
275 {
276  return system_clock::time_point{d};
277 }
278 
291 std::string
292 toIsoString(const system_clock::time_point& timePoint);
293 
306 fromIsoString(const std::string& isoString);
307 
311 std::string
313 
319 fromIsoExtendedString(const std::string& isoString);
320 
334 std::string
335 toString(const system_clock::time_point& timePoint,
336  const std::string& format = "%Y-%m-%d %H:%M:%S",
337  const std::locale& locale = std::locale("C"));
338 
353 fromString(const std::string& timePointStr,
354  const std::string& format = "%Y-%m-%d %H:%M:%S",
355  const std::locale& locale = std::locale("C"));
356 
357 } // namespace time
358 } // namespace ndn
359 
360 namespace boost::chrono {
361 
362 template<typename CharT>
363 struct clock_string<ndn::time::system_clock, CharT>
364 {
365  static std::basic_string<CharT>
366  since();
367 };
368 
369 template<typename CharT>
370 struct clock_string<ndn::time::steady_clock, CharT>
371 {
372  static std::basic_string<CharT>
373  since();
374 };
375 
376 extern template struct clock_string<ndn::time::system_clock, char>;
377 extern template struct clock_string<ndn::time::steady_clock, char>;
378 
379 } // namespace boost::chrono
380 
381 #endif // NDN_CXX_UTIL_TIME_HPP
Steady clock.
Definition: time.hpp:227
duration::rep rep
Definition: time.hpp:230
::boost::chrono::steady_clock::duration duration
Definition: time.hpp:229
::boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:232
duration::period period
Definition: time.hpp:231
System clock.
Definition: time.hpp:200
::boost::chrono::system_clock::duration duration
Definition: time.hpp:202
duration::rep rep
Definition: time.hpp:203
static time_point now() noexcept
Definition: time.cpp:45
static std::time_t to_time_t(const time_point &t) noexcept
Definition: time.cpp:57
::boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:205
duration::period period
Definition: time.hpp:204
static constexpr bool is_steady
Definition: time.hpp:206
static time_point from_time_t(std::time_t t) noexcept
Definition: time.cpp:63
std::string toIsoExtendedString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, extended format (YYYY-MM-DDTHH:MM:SS,...
Definition: time.cpp:136
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Returns the absolute value of the duration d.
Definition: time.hpp:63
duration< int_fast32_t, boost::ratio< 86400 > > days
Definition: time.hpp:43
constexpr system_clock::time_point fromUnixTimestamp(system_clock::duration d)
Convert UNIX timestamp to system_clock::time_point.
Definition: time.hpp:274
std::string toString(const system_clock::time_point &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
Definition: time.cpp:167
::boost::chrono::seconds seconds
Definition: time.hpp:51
std::string toIsoString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, basic format (YYYYMMDDTHHMMSS,fffffffff).
Definition: time.cpp:130
constexpr Duration toUnixTimestamp(const system_clock::time_point &tp)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.hpp:265
const system_clock::time_point & getUnixEpoch()
Return a system_clock::time_point representing the UNIX time epoch, i.e., 00:00:00 UTC on 1 January 1...
Definition: time.cpp:105
::boost::chrono::microseconds microseconds
Definition: time.hpp:53
::boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:38
::boost::chrono::minutes minutes
Definition: time.hpp:50
system_clock::time_point fromIsoString(const std::string &isoString)
Convert from the ISO 8601 basic string format (YYYYMMDDTHHMMSS,fffffffff) to the internal time format...
Definition: time.cpp:155
system_clock::time_point fromString(const std::string &timePointStr, const std::string &format, const std::locale &locale)
Convert from string of specified format into time point.
Definition: time.cpp:182
duration< int_fast32_t, boost::ratio< 604800 > > weeks
Definition: time.hpp:44
system_clock::time_point fromIsoExtendedString(const std::string &isoString)
Convert from the ISO 8601 extended string format (YYYY-MM-DDTHH:MM:SS,fffffffff) to the internal time...
Definition: time.cpp:161
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
duration< int_fast32_t, boost::ratio< 31556952 > > years
Definition: time.hpp:46
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54
duration< int_fast32_t, boost::ratio< 2629746 > > months
Definition: time.hpp:45
::boost::chrono::hours hours
Definition: time.hpp:49
Definition: data.cpp:25