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