25 #include <boost/date_time/posix_time/posix_time.hpp> 36 shared_ptr<CustomSystemClock> systemClock)
38 g_systemClock = systemClock;
39 g_steadyClock = steadyClock;
47 if (g_systemClock ==
nullptr) {
49 return time_point(boost::chrono::system_clock::now().time_since_epoch());
52 return g_systemClock->getNow();
59 return duration_cast<seconds>(t.time_since_epoch()).count();
81 if (g_steadyClock ==
nullptr) {
83 return time_point(base_steady_clock::now().time_since_epoch());
86 return g_steadyClock->getNow();
90 boost::posix_time::time_duration
93 if (g_steadyClock ==
nullptr) {
96 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS 97 boost::posix_time::nanoseconds(duration_cast<nanoseconds>(duration).count())
99 boost::posix_time::microseconds(duration_cast<microseconds>(duration).count())
104 return g_steadyClock->toPosixDuration(duration);
120 return duration_cast<milliseconds>(point -
getUnixEpoch());
132 namespace bpt = boost::posix_time;
135 uint64_t micro = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count() % 1000000;
138 ptime += bpt::microseconds(micro);
139 return bpt::to_iso_string(ptime);
142 return bpt::to_iso_string(ptime);
149 namespace bpt = boost::posix_time;
150 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
152 bpt::ptime ptime = bpt::from_iso_string(isoString);
156 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
163 const std::string& format,
164 const std::locale& locale)
166 namespace bpt = boost::posix_time;
169 uint64_t micro = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count() % 1000000;
170 ptime += bpt::microseconds(micro);
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;
177 return formattedTimePoint.str();
183 const std::string& format,
184 const std::locale& locale)
186 namespace bpt = boost::posix_time;
187 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
189 bpt::time_input_facet* facet =
new bpt::time_input_facet(format);
190 std::istringstream is(formattedTimePoint);
192 is.imbue(std::locale(locale, facet));
198 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
210 template<
class CharT>
211 std::basic_string<CharT>
212 clock_string<ndn::time::system_clock, CharT>::since()
216 return clock_string<system_clock, CharT>::since();
224 struct clock_string<ndn::time::system_clock, char>;
228 template<
class CharT>
229 std::basic_string<CharT>
230 clock_string<ndn::time::steady_clock, CharT>::since()
234 return clock_string<ndn::time::base_steady_clock, CharT>::since();
242 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) 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...
Copyright (c) 2013-2016 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) ...