25 #include <boost/date_time/posix_time/posix_time.hpp> 37 shared_ptr<CustomSystemClock> systemClock)
39 g_systemClock = systemClock;
40 g_steadyClock = steadyClock;
48 if (g_systemClock ==
nullptr) {
50 return time_point(boost::chrono::system_clock::now().time_since_epoch());
53 return g_systemClock->getNow();
60 return duration_cast<seconds>(t.time_since_epoch()).count();
82 if (g_steadyClock ==
nullptr) {
84 return time_point(base_steady_clock::now().time_since_epoch());
87 return g_steadyClock->getNow();
91 boost::posix_time::time_duration
94 if (g_steadyClock ==
nullptr) {
97 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS 98 boost::posix_time::nanoseconds(duration_cast<nanoseconds>(duration).count())
100 boost::posix_time::microseconds(duration_cast<microseconds>(duration).count())
105 return g_steadyClock->toPosixDuration(duration);
121 return duration_cast<milliseconds>(point -
getUnixEpoch());
133 namespace bpt = boost::posix_time;
134 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
136 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG 137 using BptResolutionUnit = nanoseconds;
139 using BptResolutionUnit = microseconds;
141 constexpr
auto unitsPerHour = duration_cast<BptResolutionUnit>(hours(1)).count();
143 auto sinceEpoch = duration_cast<BptResolutionUnit>(timePoint -
getUnixEpoch()).count();
144 bpt::ptime ptime = epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0,
145 sinceEpoch % unitsPerHour);
147 return bpt::to_iso_string(ptime);
154 namespace bpt = boost::posix_time;
155 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
157 bpt::ptime ptime = bpt::from_iso_string(isoString);
161 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
168 const std::string& format,
169 const std::locale& locale)
171 namespace bpt = boost::posix_time;
174 uint64_t micro = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count() % 1000000;
175 ptime += bpt::microseconds(micro);
177 bpt::time_facet* facet =
new bpt::time_facet(format.c_str());
178 std::ostringstream formattedTimePoint;
179 formattedTimePoint.imbue(std::locale(locale, facet));
180 formattedTimePoint << ptime;
182 return formattedTimePoint.str();
188 const std::string& format,
189 const std::locale& locale)
191 namespace bpt = boost::posix_time;
192 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
194 bpt::time_input_facet* facet =
new bpt::time_input_facet(format);
195 std::istringstream is(formattedTimePoint);
197 is.imbue(std::locale(locale, facet));
203 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
215 template<
class CharT>
216 std::basic_string<CharT>
217 clock_string<ndn::time::system_clock, CharT>::since()
221 return clock_string<system_clock, CharT>::since();
229 struct clock_string<ndn::time::system_clock, char>;
233 template<
class CharT>
234 std::basic_string<CharT>
235 clock_string<ndn::time::steady_clock, CharT>::since()
239 return clock_string<ndn::time::base_steady_clock, CharT>::since();
247 struct clock_string<ndn::time::steady_clock, char>;
boost::chrono::time_point< system_clock > time_point
boost::chrono::time_point< steady_clock > time_point
Copyright (c) 2013-2017 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...
Copyright (c) 2013-2017 Regents of the University of California.
BOOST_SYSTEM_CLOCK_DURATION duration
static time_point now() noexcept
static std::time_t to_time_t(const time_point &t) noexcept
static time_point now() noexcept
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
boost::chrono::steady_clock base_steady_clock
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.
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
void setCustomClocks(shared_ptr< CustomSteadyClock > steadyClock=nullptr, shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
static shared_ptr< CustomSteadyClock > g_steadyClock
system_clock::TimePoint fromUnixTimestamp(const milliseconds &duration)
Convert UNIX timestamp to system_clock::TimePoint.
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
static shared_ptr< CustomSystemClock > g_systemClock
static time_point from_time_t(std::time_t t) noexcept
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...