25 #include <boost/date_time/posix_time/posix_time.hpp>
30 static std::shared_ptr<CustomSystemClock> g_systemClock;
31 static std::shared_ptr<CustomSteadyClock> g_steadyClock;
36 std::shared_ptr<CustomSystemClock> systemClock)
38 g_systemClock = std::move(systemClock);
39 g_steadyClock = std::move(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();
93 if (g_steadyClock ==
nullptr) {
98 return g_steadyClock->toWaitDuration(d);
111 static boost::posix_time::ptime
114 namespace bpt = boost::posix_time;
115 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
117 using BptResolution =
118 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
123 constexpr
auto unitsPerHour = duration_cast<BptResolution>(1_h).count();
125 auto sinceEpoch = duration_cast<BptResolution>(timePoint -
getUnixEpoch()).count();
126 return epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
132 return boost::posix_time::to_iso_string(convertToPosixTime(timePoint));
138 return boost::posix_time::to_iso_extended_string(convertToPosixTime(timePoint));
142 convertToTimePoint(
const boost::posix_time::ptime& ptime)
144 namespace bpt = boost::posix_time;
145 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
149 auto sinceEpoch = ptime - epoch;
151 return point +
microseconds(sinceEpoch.total_microseconds() % 1000000);
157 return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
163 return convertToTimePoint(boost::posix_time::from_iso_extended_string(isoString));
168 const std::string& format,
169 const std::locale& locale)
171 namespace bpt = boost::posix_time;
173 std::ostringstream os;
174 auto* facet =
new bpt::time_facet(format.data());
175 os.imbue(std::locale(locale, facet));
176 os << convertToPosixTime(timePoint);
183 const std::string& format,
184 const std::locale& locale)
186 namespace bpt = boost::posix_time;
188 std::istringstream is(timePointStr);
189 auto* facet =
new bpt::time_input_facet(format);
190 is.imbue(std::locale(locale, facet));
194 return convertToTimePoint(ptime);
201 template<
class CharT>
202 std::basic_string<CharT>
203 clock_string<ndn::time::system_clock, CharT>::since()
205 if (ndn::time::g_systemClock ==
nullptr) {
207 return clock_string<system_clock, CharT>::since();
210 return ndn::time::g_systemClock->getSince();
214 template<
class CharT>
215 std::basic_string<CharT>
216 clock_string<ndn::time::steady_clock, CharT>::since()
218 if (ndn::time::g_steadyClock ==
nullptr) {
220 return clock_string<ndn::time::base_steady_clock, CharT>::since();
223 return ndn::time::g_steadyClock->getSince();
227 template struct clock_string<ndn::time::system_clock, char>;
228 template struct clock_string<ndn::time::steady_clock, char>;
static time_point now() noexcept
::boost::chrono::steady_clock::duration duration
::boost::chrono::time_point< steady_clock > time_point
static time_point now() noexcept
static std::time_t to_time_t(const time_point &t) noexcept
::boost::chrono::time_point< system_clock > time_point
static time_point from_time_t(std::time_t t) noexcept
std::string toIsoExtendedString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, extended format (YYYY-MM-DDTHH:MM:SS,...
boost::chrono::steady_clock base_steady_clock
void setCustomClocks(std::shared_ptr< CustomSteadyClock > steadyClock=nullptr, std::shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
std::string toString(const system_clock::time_point &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
::boost::chrono::seconds seconds
std::string toIsoString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, basic format (YYYYMMDDTHHMMSS,fffffffff).
const system_clock::time_point & getUnixEpoch()
Return a system_clock::time_point representing the UNIX time epoch, i.e., 00:00:00 UTC on 1 January 1...
::boost::chrono::microseconds microseconds
system_clock::time_point fromIsoString(const std::string &isoString)
Convert from the ISO 8601 basic string format (YYYYMMDDTHHMMSS,fffffffff) to the internal time format...
system_clock::time_point fromString(const std::string &timePointStr, const std::string &format, const std::locale &locale)
Convert from string of specified format into time point.
system_clock::time_point fromIsoExtendedString(const std::string &isoString)
Convert from the ISO 8601 extended string format (YYYY-MM-DDTHH:MM:SS,fffffffff) to the internal time...
::boost::chrono::nanoseconds nanoseconds