NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018 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_UTIL_TIME_HPP
23 #define NDN_UTIL_TIME_HPP
24 
25 #include "../common.hpp"
26 
27 #include <boost/asio/wait_traits.hpp>
28 #include <boost/chrono.hpp>
29 
30 namespace ndn {
31 namespace time {
32 
33 using boost::chrono::duration;
34 using boost::chrono::duration_cast;
35 
36 using days = duration<int_fast32_t, boost::ratio<86400>>;
37 using boost::chrono::hours;
38 using boost::chrono::minutes;
39 using boost::chrono::seconds;
40 using boost::chrono::milliseconds;
41 using boost::chrono::microseconds;
42 using boost::chrono::nanoseconds;
43 
48 template<typename Rep, typename Period,
49  typename = typename std::enable_if<std::numeric_limits<Rep>::is_signed>::type>
50 constexpr duration<Rep, Period>
51 abs(duration<Rep, Period> d)
52 {
53  return d >= d.zero() ? d : -d;
54 }
55 
56 } // namespace time
57 
58 inline namespace literals {
59 inline namespace time_literals {
60 
61 constexpr time::days
62 operator "" _day(unsigned long long days)
63 {
64  return time::days{days};
65 }
66 
67 constexpr time::duration<long double, time::days::period>
68 operator "" _day(long double days)
69 {
70  return time::duration<long double, time::days::period>{days};
71 }
72 
73 constexpr time::days
74 operator "" _days(unsigned long long days)
75 {
76  return time::days{days};
77 }
78 
79 constexpr time::duration<long double, time::days::period>
80 operator "" _days(long double days)
81 {
82  return time::duration<long double, time::days::period>{days};
83 }
84 
85 constexpr time::hours
86 operator "" _h(unsigned long long hrs)
87 {
88  return time::hours{hrs};
89 }
90 
91 constexpr time::duration<long double, time::hours::period>
92 operator "" _h(long double hrs)
93 {
94  return time::duration<long double, time::hours::period>{hrs};
95 }
96 
97 constexpr time::minutes
98 operator "" _min(unsigned long long mins)
99 {
100  return time::minutes{mins};
101 }
102 
103 constexpr time::duration<long double, time::minutes::period>
104 operator "" _min(long double mins)
105 {
106  return time::duration<long double, time::minutes::period>{mins};
107 }
108 
109 constexpr time::seconds
110 operator "" _s(unsigned long long secs)
111 {
112  return time::seconds{secs};
113 }
114 
115 constexpr time::duration<long double, time::seconds::period>
116 operator "" _s(long double secs)
117 {
118  return time::duration<long double, time::seconds::period>{secs};
119 }
120 
121 constexpr time::milliseconds
122 operator "" _ms(unsigned long long msecs)
123 {
124  return time::milliseconds{msecs};
125 }
126 
127 constexpr time::duration<long double, time::milliseconds::period>
128 operator "" _ms(long double msecs)
129 {
130  return time::duration<long double, time::milliseconds::period>{msecs};
131 }
132 
133 constexpr time::microseconds
134 operator "" _us(unsigned long long usecs)
135 {
136  return time::microseconds{usecs};
137 }
138 
139 constexpr time::duration<long double, time::microseconds::period>
140 operator "" _us(long double usecs)
141 {
142  return time::duration<long double, time::microseconds::period>{usecs};
143 }
144 
145 constexpr time::nanoseconds
146 operator "" _ns(unsigned long long nsecs)
147 {
148  return time::nanoseconds{nsecs};
149 }
150 
151 constexpr time::duration<long double, time::nanoseconds::period>
152 operator "" _ns(long double nsecs)
153 {
154  return time::duration<long double, time::nanoseconds::period>{nsecs};
155 }
156 
157 } // inline namespace time_literals
158 } // inline namespace literals
159 
160 namespace time {
161 
162 using namespace literals::time_literals;
163 
188 {
189 public:
190  using duration = boost::chrono::system_clock::duration;
191  using rep = duration::rep;
192  using period = duration::period;
193  using time_point = boost::chrono::time_point<system_clock>;
194  static constexpr bool is_steady = boost::chrono::system_clock::is_steady;
195 
198 
199  static time_point
200  now() noexcept;
201 
202  static std::time_t
203  to_time_t(const time_point& t) noexcept;
204 
205  static time_point
206  from_time_t(std::time_t t) noexcept;
207 };
208 
218 {
219 public:
220  using duration = boost::chrono::steady_clock::duration;
221  using rep = duration::rep;
222  using period = duration::period;
223  using time_point = boost::chrono::time_point<steady_clock>;
224  static constexpr bool is_steady = true;
225 
228 
229  static time_point
230  now() noexcept;
231 
232 private:
241  static duration
242  to_wait_duration(duration d);
243 
244  friend struct boost::asio::wait_traits<steady_clock>; // see steady-timer.hpp
245 };
246 
250 const system_clock::TimePoint&
251 getUnixEpoch();
252 
256 milliseconds
257 toUnixTimestamp(const system_clock::TimePoint& point);
258 
263 fromUnixTimestamp(milliseconds duration);
264 
278 std::string
279 toIsoString(const system_clock::TimePoint& timePoint);
280 
294 fromIsoString(const std::string& isoString);
295 
309 std::string
310 toString(const system_clock::TimePoint& timePoint,
311  const std::string& format = "%Y-%m-%d %H:%M:%S",
312  const std::locale& locale = std::locale("C"));
313 
328 fromString(const std::string& timePointStr,
329  const std::string& format = "%Y-%m-%d %H:%M:%S",
330  const std::locale& locale = std::locale("C"));
331 
332 } // namespace time
333 } // namespace ndn
334 
335 namespace boost {
336 namespace chrono {
337 
338 template<class CharT>
339 struct clock_string<ndn::time::system_clock, CharT>
340 {
341  static std::basic_string<CharT>
342  since();
343 };
344 
345 template<class CharT>
346 struct clock_string<ndn::time::steady_clock, CharT>
347 {
348  static std::basic_string<CharT>
349  since();
350 };
351 
352 extern template struct clock_string<ndn::time::system_clock, char>;
353 extern template struct clock_string<ndn::time::steady_clock, char>;
354 
355 } // namespace chrono
356 } // namespace boost
357 
358 #endif // NDN_UTIL_TIME_HPP
boost::chrono::system_clock::duration duration
Definition: time.hpp:190
time_point TimePoint
Definition: time.hpp:226
Copyright (c) 2011-2015 Regents of the University of California.
system_clock::TimePoint fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.cpp:119
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:164
duration::rep rep
Definition: time.hpp:221
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:193
boost::chrono::steady_clock::duration duration
Definition: time.hpp:220
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:223
STL namespace.
Steady clock.
Definition: time.hpp:217
duration::period period
Definition: time.hpp:222
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:51
system_clock::TimePoint 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:185
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
Definition: time.cpp:170
System clock.
Definition: time.hpp:187
duration::period period
Definition: time.hpp:192
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:145
time_point TimePoint
Definition: time.hpp:196
duration< int_fast32_t, boost::ratio< 86400 > > days
Definition: time.hpp:36
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:113
duration::rep rep
Definition: time.hpp:191
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Definition: time.cpp:106