time.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_TIME_HPP
23 #define NDN_TIME_HPP
24 
25 #include "../common.hpp"
26 #include <boost/chrono.hpp>
27 #include <boost/asio/time_traits.hpp>
28 #include <boost/date_time/posix_time/posix_time_types.hpp>
29 
30 namespace ndn {
31 namespace time {
32 
33 using boost::chrono::duration;
34 
35 typedef duration<boost::int_least32_t, boost::ratio<86400> > days;
36 using boost::chrono::hours;
37 using boost::chrono::minutes;
38 using boost::chrono::seconds;
39 
40 using boost::chrono::milliseconds;
41 using boost::chrono::microseconds;
42 using boost::chrono::nanoseconds;
43 
44 using boost::chrono::duration_cast;
45 
50 template<typename Rep, typename Period,
51  typename = typename std::enable_if<std::numeric_limits<Rep>::is_signed>::type>
52 constexpr duration<Rep, Period>
53 abs(duration<Rep, Period> d)
54 {
55  return d >= d.zero() ? d : -d;
56 }
57 
82 {
83 public:
84  typedef BOOST_SYSTEM_CLOCK_DURATION duration;
85  typedef duration::rep rep;
86  typedef duration::period period;
87  typedef boost::chrono::time_point<system_clock> time_point;
88  static constexpr bool is_steady = false;
89 
90  typedef time_point TimePoint;
91  typedef duration Duration;
92 
93  static time_point
94  now() noexcept;
95 
96  static std::time_t
97  to_time_t(const time_point& t) noexcept;
98 
99  static time_point
100  from_time_t(std::time_t t) noexcept;
101 }; // class system_clock
102 
112 {
113 public:
114  typedef nanoseconds duration;
115  typedef duration::rep rep;
116  typedef duration::period period;
117  typedef boost::chrono::time_point<steady_clock> time_point;
118  static constexpr bool is_steady = true;
119 
120  typedef time_point TimePoint;
121  typedef duration Duration;
122 
123  static time_point
124  now() noexcept;
125 
126 private:
135  static boost::posix_time::time_duration
136  to_posix_duration(const duration& duration);
137 
138  friend struct boost::asio::time_traits<steady_clock>;
139 }; // class steady_clock
140 
141 
145 const system_clock::TimePoint&
146 getUnixEpoch();
147 
151 milliseconds
152 toUnixTimestamp(const system_clock::TimePoint& point);
153 
157 system_clock::TimePoint
158 fromUnixTimestamp(const milliseconds& duration);
159 
173 std::string
174 toIsoString(const system_clock::TimePoint& timePoint);
175 
188 system_clock::TimePoint
189 fromIsoString(const std::string& isoString);
190 
204 std::string
205 toString(const system_clock::TimePoint& timePoint,
206  const std::string& format = "%Y-%m-%d %H:%M:%S",
207  const std::locale& locale = std::locale("C"));
208 
222 system_clock::TimePoint
223 fromString(const std::string& formattedTimePoint,
224  const std::string& format = "%Y-%m-%d %H:%M:%S",
225  const std::locale& locale = std::locale("C"));
226 
227 
229 
230 } // namespace time
231 } // namespace ndn
232 
233 namespace boost {
234 namespace chrono {
235 
236 template<class CharT>
237 struct clock_string<ndn::time::system_clock, CharT>
238 {
239  static std::basic_string<CharT>
240  since();
241 };
242 
243 template<class CharT>
244 struct clock_string<ndn::time::steady_clock, CharT>
245 {
246  static std::basic_string<CharT>
247  since();
248 };
249 
250 } // namespace chrono
251 } // namespace boost
252 
253 #endif // NDN_TIME_HPP
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:87
time_point TimePoint
Definition: time.hpp:120
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:117
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:152
Copyright (c) 2013-2017 Regents of the University of California.
Definition: block.hpp:31
duration::period period
Definition: time.hpp:86
BOOST_SYSTEM_CLOCK_DURATION duration
Definition: time.hpp:84
static std::time_t to_time_t(const time_point &t) noexcept
Definition: time.cpp:58
static constexpr bool is_steady
Definition: time.hpp:88
duration::period period
Definition: time.hpp:116
nanoseconds duration
Definition: time.hpp:114
STL namespace.
Steady clock.
Definition: time.hpp:111
static time_point now() noexcept
Definition: time.cpp:46
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:53
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:167
system_clock::TimePoint fromString(const std::string &formattedTimePoint, const std::string &format, const std::locale &locale)
Convert from string of specified format into time point.
Definition: time.cpp:187
duration::rep rep
Definition: time.hpp:85
System clock.
Definition: time.hpp:81
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:131
time_point TimePoint
Definition: time.hpp:90
system_clock::TimePoint fromUnixTimestamp(const milliseconds &duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.cpp:125
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:119
duration::rep rep
Definition: time.hpp:115
static time_point from_time_t(std::time_t t) noexcept
Definition: time.cpp:64
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Definition: time.cpp:112
duration< boost::int_least32_t, boost::ratio< 86400 > > days
Definition: time.hpp:35