25 #include <boost/date_time/posix_time/posix_time.hpp>
31 static shared_ptr<CustomSystemClock> g_systemClock;
32 static shared_ptr<CustomSteadyClock> g_steadyClock;
37 shared_ptr<CustomSystemClock> systemClock)
39 g_systemClock = std::move(systemClock);
40 g_steadyClock = std::move(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();
94 if (g_steadyClock ==
nullptr) {
99 return g_steadyClock->toWaitDuration(d);
115 return duration_cast<milliseconds>(point -
getUnixEpoch());
124 static boost::posix_time::ptime
127 namespace bpt = boost::posix_time;
128 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
130 using BptResolution =
131 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
136 constexpr
auto unitsPerHour = duration_cast<BptResolution>(1_h).count();
138 auto sinceEpoch = duration_cast<BptResolution>(timePoint -
getUnixEpoch()).count();
139 return epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
145 return boost::posix_time::to_iso_string(convertToPosixTime(timePoint));
151 return boost::posix_time::to_iso_extended_string(convertToPosixTime(timePoint));
155 convertToTimePoint(
const boost::posix_time::ptime& ptime)
157 namespace bpt = boost::posix_time;
158 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
162 auto sinceEpoch = ptime - epoch;
164 return point +
microseconds(sinceEpoch.total_microseconds() % 1000000);
170 return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
176 return convertToTimePoint(boost::posix_time::from_iso_extended_string(isoString));
181 const std::string& format,
182 const std::locale& locale)
184 namespace bpt = boost::posix_time;
186 std::ostringstream os;
187 auto* facet =
new bpt::time_facet(format.data());
188 os.imbue(std::locale(locale, facet));
189 os << convertToPosixTime(timePoint);
196 const std::string& format,
197 const std::locale& locale)
199 namespace bpt = boost::posix_time;
201 std::istringstream is(timePointStr);
202 auto* facet =
new bpt::time_input_facet(format);
203 is.imbue(std::locale(locale, facet));
207 return convertToTimePoint(ptime);
216 template<
class CharT>
217 std::basic_string<CharT>
220 if (ndn::time::g_systemClock ==
nullptr) {
222 return clock_string<system_clock, CharT>::since();
225 return ndn::time::g_systemClock->getSince();
229 template<
class CharT>
230 std::basic_string<CharT>
233 if (ndn::time::g_steadyClock ==
nullptr) {
235 return clock_string<ndn::time::base_steady_clock, CharT>::since();
238 return ndn::time::g_steadyClock->getSince();
242 template struct clock_string<ndn::time::system_clock, char>;
243 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,...
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
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.
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::duration< Rep, Period > duration
boost::chrono::milliseconds milliseconds
void setCustomClocks(shared_ptr< CustomSteadyClock > steadyClock=nullptr, shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
boost::chrono::nanoseconds nanoseconds
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...
system_clock::time_point fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::time_point.
boost::chrono::seconds seconds
boost::chrono::steady_clock base_steady_clock
static std::basic_string< CharT > since()
static std::basic_string< CharT > since()