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-2021 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 
26 
27 #include <boost/asio/wait_traits.hpp>
28 #include <boost/chrono.hpp>
29 
30 namespace ndn {
31 namespace time {
32 
33 template<typename Rep, typename Period>
34 using duration = boost::chrono::duration<Rep, Period>;
35 
36 using boost::chrono::duration_cast;
37 
38 // C++20
43 
44 // C++11
51 
56 template<typename Rep, typename Period, typename = std::enable_if_t<std::numeric_limits<Rep>::is_signed>>
57 constexpr duration<Rep, Period>
59 {
60  return d >= d.zero() ? d : -d;
61 }
62 
63 } // namespace time
64 
65 inline namespace literals {
66 inline namespace time_literals {
67 
68 constexpr time::days
69 operator "" _day(unsigned long long days)
70 {
71  return time::days{days};
72 }
73 
74 constexpr time::duration<long double, time::days::period>
75 operator "" _day(long double days)
76 {
78 }
79 
80 constexpr time::days
81 operator "" _days(unsigned long long days)
82 {
83  return time::days{days};
84 }
85 
86 constexpr time::duration<long double, time::days::period>
87 operator "" _days(long double days)
88 {
90 }
91 
92 constexpr time::hours
93 operator "" _h(unsigned long long hrs)
94 {
95  return time::hours{hrs};
96 }
97 
98 constexpr time::duration<long double, time::hours::period>
99 operator "" _h(long double hrs)
100 {
102 }
103 
104 constexpr time::minutes
105 operator "" _min(unsigned long long mins)
106 {
107  return time::minutes{mins};
108 }
109 
110 constexpr time::duration<long double, time::minutes::period>
111 operator "" _min(long double mins)
112 {
114 }
115 
116 constexpr time::seconds
117 operator "" _s(unsigned long long secs)
118 {
119  return time::seconds{secs};
120 }
121 
122 constexpr time::duration<long double, time::seconds::period>
123 operator "" _s(long double secs)
124 {
126 }
127 
128 constexpr time::milliseconds
129 operator "" _ms(unsigned long long msecs)
130 {
131  return time::milliseconds{msecs};
132 }
133 
134 constexpr time::duration<long double, time::milliseconds::period>
135 operator "" _ms(long double msecs)
136 {
138 }
139 
140 constexpr time::microseconds
141 operator "" _us(unsigned long long usecs)
142 {
143  return time::microseconds{usecs};
144 }
145 
146 constexpr time::duration<long double, time::microseconds::period>
147 operator "" _us(long double usecs)
148 {
150 }
151 
152 constexpr time::nanoseconds
153 operator "" _ns(unsigned long long nsecs)
154 {
155  return time::nanoseconds{nsecs};
156 }
157 
158 constexpr time::duration<long double, time::nanoseconds::period>
159 operator "" _ns(long double nsecs)
160 {
162 }
163 
164 } // inline namespace time_literals
165 } // inline namespace literals
166 
167 namespace time {
168 
169 using namespace literals::time_literals;
170 
195 {
196 public:
198  using rep = duration::rep;
199  using period = duration::period;
200  using time_point = boost::chrono::time_point<system_clock>;
201  static constexpr bool is_steady = boost::chrono::system_clock::is_steady;
202 
205 
206  static time_point
207  now() noexcept;
208 
209  static std::time_t
210  to_time_t(const time_point& t) noexcept;
211 
212  static time_point
213  from_time_t(std::time_t t) noexcept;
214 };
215 
225 {
226 public:
228  using rep = duration::rep;
229  using period = duration::period;
230  using time_point = boost::chrono::time_point<steady_clock>;
231  static constexpr bool is_steady = true;
232 
235 
236  static time_point
237  now() noexcept;
238 
239 private:
248  static duration
249  to_wait_duration(duration d);
250 
251  friend struct boost::asio::wait_traits<steady_clock>; // see steady-timer.hpp
252 };
253 
258 const system_clock::time_point&
259 getUnixEpoch();
260 
266 
272 
285 std::string
286 toIsoString(const system_clock::time_point& timePoint);
287 
300 fromIsoString(const std::string& isoString);
301 
305 std::string
306 toIsoExtendedString(const system_clock::time_point& timePoint);
307 
313 fromIsoExtendedString(const std::string& isoString);
314 
328 std::string
329 toString(const system_clock::time_point& timePoint,
330  const std::string& format = "%Y-%m-%d %H:%M:%S",
331  const std::locale& locale = std::locale("C"));
332 
347 fromString(const std::string& timePointStr,
348  const std::string& format = "%Y-%m-%d %H:%M:%S",
349  const std::locale& locale = std::locale("C"));
350 
351 } // namespace time
352 } // namespace ndn
353 
354 namespace boost {
355 namespace chrono {
356 
357 template<typename CharT>
358 struct clock_string<ndn::time::system_clock, CharT>
359 {
360  static std::basic_string<CharT>
361  since();
362 };
363 
364 template<typename CharT>
365 struct clock_string<ndn::time::steady_clock, CharT>
366 {
367  static std::basic_string<CharT>
368  since();
369 };
370 
371 extern template struct clock_string<ndn::time::system_clock, char>;
372 extern template struct clock_string<ndn::time::steady_clock, char>;
373 
374 } // namespace chrono
375 } // namespace boost
376 
377 #endif // NDN_CXX_UTIL_TIME_HPP
Steady clock.
Definition: time.hpp:225
duration::rep rep
Definition: time.hpp:228
boost::chrono::steady_clock::duration duration
Definition: time.hpp:227
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:230
time_point TimePoint
Definition: time.hpp:233
duration::period period
Definition: time.hpp:229
System clock.
Definition: time.hpp:195
boost::chrono::system_clock::duration duration
Definition: time.hpp:197
duration::rep rep
Definition: time.hpp:198
time_point TimePoint
Definition: time.hpp:203
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:200
duration::period period
Definition: time.hpp:199
Common includes and macros used throughout the library.
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:149
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:58
duration< int_fast32_t, boost::ratio< 86400 > > days
Definition: time.hpp:39
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.cpp:113
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:180
boost::chrono::minutes minutes
Definition: time.hpp:46
std::string toIsoString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, basic format (YYYYMMDDTHHMMSS,fffffffff).
Definition: time.cpp:143
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:106
boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:34
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
boost::chrono::microseconds microseconds
Definition: time.hpp:49
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:168
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:195
duration< int_fast32_t, boost::ratio< 604800 > > weeks
Definition: time.hpp:40
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:174
system_clock::time_point fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::time_point.
Definition: time.cpp:119
duration< int_fast32_t, boost::ratio< 31556952 > > years
Definition: time.hpp:42
boost::chrono::hours hours
Definition: time.hpp:45
boost::chrono::seconds seconds
Definition: time.hpp:47
duration< int_fast32_t, boost::ratio< 2629746 > > months
Definition: time.hpp:41
Definition: data.cpp:25