All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
time.hpp
Go to the documentation of this file.
1 
6 #ifndef NDN_TIME_HPP
7 #define NDN_TIME_HPP
8 
9 #include "../common.hpp"
10 #include <boost/chrono.hpp>
11 #include <boost/date_time/posix_time/posix_time.hpp>
12 
13 namespace ndn {
14 namespace time {
15 
16 using boost::chrono::duration;
17 
18 typedef duration<boost::int_least32_t, boost::ratio<86400> > days;
19 using boost::chrono::hours;
20 using boost::chrono::minutes;
21 using boost::chrono::seconds;
22 
23 using boost::chrono::milliseconds;
24 using boost::chrono::microseconds;
25 using boost::chrono::nanoseconds;
26 
27 using boost::chrono::duration_cast;
28 
52 class system_clock : public boost::chrono::system_clock
53 {
54 public:
55  typedef time_point TimePoint;
56  typedef duration Duration;
57 
58  // /// \brief Get current TimePoint
59  // TimePoint
60  // now();
61 }; // class system_clock
62 
74 class steady_clock : public
75 #ifdef __APPLE__
76 // steady_clock may go backwards on OS X platforms, so use system_clock
77 // instead
78  boost::chrono::system_clock
79 #else
80  boost::chrono::steady_clock
81 #endif
82 {
83 public:
84  typedef time_point TimePoint;
85  typedef duration Duration;
86 
87  // /// \brief Get current TimePoint
88  // TimePoint
89  // now();
90 }; // class steady_clock
91 
92 
96 inline const system_clock::TimePoint&
98 {
99  static system_clock::TimePoint epoch = system_clock::from_time_t(0);
100  return epoch;
101 }
102 
106 inline milliseconds
108 {
109  return duration_cast<milliseconds>(point - getUnixEpoch());
110 }
111 
116 fromUnixTimestamp(const milliseconds& duration)
117 {
118  return getUnixEpoch() + duration;
119 }
120 
134 inline std::string
136 {
137  namespace bpt = boost::posix_time;
138  bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
139 
140  uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
141  if (micro > 0)
142  {
143  ptime += bpt::microseconds(micro);
144  return bpt::to_iso_string(ptime);
145  }
146  else
147  return bpt::to_iso_string(ptime);
148 }
149 
163 fromIsoString(const std::string& isoString)
164 {
165  namespace bpt = boost::posix_time;
166  static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
167 
168  bpt::ptime ptime = bpt::from_iso_string(isoString);
169 
171  system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
172  point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
173  return point;
174 }
175 
189 inline std::string
191  const std::string& format = "%Y-%m-%d %H:%M:%S",
192  const std::locale& locale = std::locale("C"))
193 {
194  namespace bpt = boost::posix_time;
195  bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
196 
197  uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
198  ptime += bpt::microseconds(micro);
199 
200  bpt::time_facet* facet = new bpt::time_facet(format.c_str());
201  std::ostringstream formattedTimePoint;
202  formattedTimePoint.imbue(std::locale(locale, facet));
203  formattedTimePoint << ptime;
204 
205  return formattedTimePoint.str();
206 }
207 
222 fromString(const std::string& formattedTimePoint,
223  const std::string& format = "%Y-%m-%d %H:%M:%S",
224  const std::locale& locale = std::locale("C"))
225 {
226  namespace bpt = boost::posix_time;
227  static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
228 
229  bpt::time_input_facet* facet = new bpt::time_input_facet(format);
230  std::istringstream is(formattedTimePoint);
231 
232  is.imbue(std::locale(locale, facet));
233  bpt::ptime ptime;
234  is >> ptime;
235 
237  system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
238  point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
239  return point;
240 }
241 
242 } // namespace time
243 } // namespace ndn
244 
245 #endif // NDN_TIME_HPP
time_point TimePoint
Definition: time.hpp:84
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.hpp:163
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Definition: time.hpp:97
Steady clock.
Definition: time.hpp:74
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format="%Y-%m-%d %H:%M:%S", const std::locale &locale=std::locale("C"))
Convert time point to string with specified format.
Definition: time.hpp:190
system_clock::TimePoint fromString(const std::string &formattedTimePoint, const std::string &format="%Y-%m-%d %H:%M:%S", const std::locale &locale=std::locale("C"))
Convert from string of specified format into time point.
Definition: time.hpp:222
System clock.
Definition: time.hpp:52
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.hpp:135
time_point TimePoint
Definition: time.hpp:55
system_clock::TimePoint fromUnixTimestamp(const milliseconds &duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.hpp:116
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.hpp:107
duration< boost::int_least32_t, boost::ratio< 86400 > > days
Definition: time.hpp:18