NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
time.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "time.hpp"
23 #include "time-custom-clock.hpp"
24 
25 #include <boost/date_time/posix_time/posix_time.hpp>
26 
27 namespace ndn {
28 namespace time {
29 
30 static shared_ptr<CustomSystemClock> g_systemClock;
31 static shared_ptr<CustomSteadyClock> g_steadyClock;
32 
33 // this method is defined in time-custom-clock.hpp
34 void
35 setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock,
36  shared_ptr<CustomSystemClock> systemClock)
37 {
38  g_systemClock = systemClock;
39  g_steadyClock = steadyClock;
40 }
41 
43 
46 {
47  if (g_systemClock == nullptr) {
48  // optimized default version
49  return time_point(boost::chrono::system_clock::now().time_since_epoch());
50  }
51  else {
52  return g_systemClock->getNow();
53  }
54 }
55 
56 std::time_t
58 {
59  return duration_cast<seconds>(t.time_since_epoch()).count();
60 }
61 
63 system_clock::from_time_t(std::time_t t) noexcept
64 {
65  return time_point(seconds(t));
66 }
67 
69 
70 #ifdef __APPLE__
71  // Note that on OS X platform boost::steady_clock is not truly monotonic, so we use
72  // system_clock instead. Refer to https://svn.boost.org/trac/boost/ticket/7719)
73  typedef boost::chrono::system_clock base_steady_clock;
74 #else
75  typedef boost::chrono::steady_clock base_steady_clock;
76 #endif
77 
80 {
81  if (g_steadyClock == nullptr) {
82  // optimized default version
83  return time_point(base_steady_clock::now().time_since_epoch());
84  }
85  else {
86  return g_steadyClock->getNow();
87  }
88 }
89 
90 boost::posix_time::time_duration
91 steady_clock::to_posix_duration(const duration& duration)
92 {
93  if (g_steadyClock == nullptr) {
94  // optimized default version
95  return
96 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
97  boost::posix_time::nanoseconds(duration_cast<nanoseconds>(duration).count())
98 #else
99  boost::posix_time::microseconds(duration_cast<microseconds>(duration).count())
100 #endif
101  ;
102  }
103  else {
104  return g_steadyClock->toPosixDuration(duration);
105  }
106 }
107 
109 
112 {
114  return epoch;
115 }
116 
117 milliseconds
119 {
120  return duration_cast<milliseconds>(point - getUnixEpoch());
121 }
122 
124 fromUnixTimestamp(const milliseconds& duration)
125 {
126  return getUnixEpoch() + duration;
127 }
128 
129 std::string
131 {
132  namespace bpt = boost::posix_time;
133  bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
134 
135  uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
136  if (micro > 0)
137  {
138  ptime += bpt::microseconds(micro);
139  return bpt::to_iso_string(ptime);
140  }
141  else
142  return bpt::to_iso_string(ptime);
143 }
144 
145 
147 fromIsoString(const std::string& isoString)
148 {
149  namespace bpt = boost::posix_time;
150  static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
151 
152  bpt::ptime ptime = bpt::from_iso_string(isoString);
153 
155  system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
156  point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
157  return point;
158 }
159 
160 
161 std::string
163  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
164  const std::locale& locale/* = std::locale("C")*/)
165 {
166  namespace bpt = boost::posix_time;
167  bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
168 
169  uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
170  ptime += bpt::microseconds(micro);
171 
172  bpt::time_facet* facet = new bpt::time_facet(format.c_str());
173  std::ostringstream formattedTimePoint;
174  formattedTimePoint.imbue(std::locale(locale, facet));
175  formattedTimePoint << ptime;
176 
177  return formattedTimePoint.str();
178 }
179 
180 
182 fromString(const std::string& formattedTimePoint,
183  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
184  const std::locale& locale/* = std::locale("C")*/)
185 {
186  namespace bpt = boost::posix_time;
187  static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
188 
189  bpt::time_input_facet* facet = new bpt::time_input_facet(format);
190  std::istringstream is(formattedTimePoint);
191 
192  is.imbue(std::locale(locale, facet));
193  bpt::ptime ptime;
194  is >> ptime;
195 
197  system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
198  point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
199  return point;
200 }
201 
202 } // namespace time
203 } // namespace ndn
204 
205 namespace boost {
206 namespace chrono {
207 
209 
210 template<class CharT>
211 std::basic_string<CharT>
212 clock_string<ndn::time::system_clock, CharT>::since()
213 {
214  if (ndn::time::g_systemClock == nullptr) {
215  // optimized default version
216  return clock_string<system_clock, CharT>::since();
217  }
218  else {
219  return ndn::time::g_systemClock->getSince();
220  }
221 }
222 
223 template
224 struct clock_string<ndn::time::system_clock, char>;
225 
227 
228 template<class CharT>
229 std::basic_string<CharT>
230 clock_string<ndn::time::steady_clock, CharT>::since()
231 {
232  if (ndn::time::g_steadyClock == nullptr) {
233  // optimized default version
234  return clock_string<ndn::time::base_steady_clock, CharT>::since();
235  }
236  else {
237  return ndn::time::g_steadyClock->getSince();
238  }
239 }
240 
241 template
242 struct clock_string<ndn::time::steady_clock, char>;
243 
244 } // namespace chrono
245 } // namespace boost
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:87
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:117
Copyright (c) 2011-2015 Regents of the University of California.
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:147
Copyright (c) 2013-2016 Regents of the University of California.
BOOST_SYSTEM_CLOCK_DURATION duration
Definition: time.hpp:84
static time_point now() noexcept
Definition: time.cpp:79
static std::time_t to_time_t(const time_point &t) noexcept
Definition: time.cpp:57
nanoseconds duration
Definition: time.hpp:114
static time_point now() noexcept
Definition: time.cpp:45
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:162
boost::chrono::steady_clock base_steady_clock
Definition: time.cpp:75
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:182
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:130
void setCustomClocks(shared_ptr< CustomSteadyClock > steadyClock=nullptr, shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
Definition: time.cpp:35
time_point TimePoint
Definition: time.hpp:90
static shared_ptr< CustomSteadyClock > g_steadyClock
Definition: time.cpp:31
system_clock::TimePoint fromUnixTimestamp(const milliseconds &duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.cpp:124
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:118
static shared_ptr< CustomSystemClock > g_systemClock
Definition: time.cpp:30
static time_point from_time_t(std::time_t t) noexcept
Definition: time.cpp:63
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Definition: time.cpp:111