blob: d3e546a682e884619160b24cdf369f4964b747ed [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa84f4642017-08-23 16:14:51 -04002/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070020 */
21
22#include "time.hpp"
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080023#include "time-custom-clock.hpp"
24
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include <boost/date_time/posix_time/posix_time.hpp>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040026#include <sstream>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070027
28namespace ndn {
29namespace time {
30
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080031static shared_ptr<CustomSystemClock> g_systemClock;
32static shared_ptr<CustomSteadyClock> g_steadyClock;
33
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050034// this function is declared in time-custom-clock.hpp
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080035void
36setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock,
37 shared_ptr<CustomSystemClock> systemClock)
38{
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050039 g_systemClock = std::move(systemClock);
40 g_steadyClock = std::move(steadyClock);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080041}
42
43/////////////////////////////////////////////////////////////////////////////////////////////
44
45system_clock::time_point
46system_clock::now() noexcept
47{
48 if (g_systemClock == nullptr) {
49 // optimized default version
50 return time_point(boost::chrono::system_clock::now().time_since_epoch());
51 }
52 else {
53 return g_systemClock->getNow();
54 }
55}
56
57std::time_t
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050058system_clock::to_time_t(const system_clock::time_point& t) noexcept
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080059{
60 return duration_cast<seconds>(t.time_since_epoch()).count();
61}
62
63system_clock::time_point
64system_clock::from_time_t(std::time_t t) noexcept
65{
66 return time_point(seconds(t));
67}
68
69/////////////////////////////////////////////////////////////////////////////////////////////
70
71#ifdef __APPLE__
72 // Note that on OS X platform boost::steady_clock is not truly monotonic, so we use
73 // system_clock instead. Refer to https://svn.boost.org/trac/boost/ticket/7719)
74 typedef boost::chrono::system_clock base_steady_clock;
75#else
76 typedef boost::chrono::steady_clock base_steady_clock;
77#endif
78
79steady_clock::time_point
80steady_clock::now() noexcept
81{
82 if (g_steadyClock == nullptr) {
83 // optimized default version
84 return time_point(base_steady_clock::now().time_since_epoch());
85 }
86 else {
87 return g_steadyClock->getNow();
88 }
89}
90
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050091steady_clock::duration
92steady_clock::to_wait_duration(steady_clock::duration d)
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080093{
94 if (g_steadyClock == nullptr) {
95 // optimized default version
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050096 return d;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080097 }
98 else {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050099 return g_steadyClock->toWaitDuration(d);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800100 }
101}
102
103/////////////////////////////////////////////////////////////////////////////////////////////
104
105const system_clock::TimePoint&
106getUnixEpoch()
107{
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500108 static auto epoch = system_clock::from_time_t(0);
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800109 return epoch;
110}
111
112milliseconds
113toUnixTimestamp(const system_clock::TimePoint& point)
114{
115 return duration_cast<milliseconds>(point - getUnixEpoch());
116}
117
118system_clock::TimePoint
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500119fromUnixTimestamp(milliseconds duration)
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800120{
121 return getUnixEpoch() + duration;
122}
123
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700124std::string
125toIsoString(const system_clock::TimePoint& timePoint)
126{
127 namespace bpt = boost::posix_time;
Junxiao Shi855eaa52017-04-04 20:10:43 +0000128 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700129
Junxiao Shi855eaa52017-04-04 20:10:43 +0000130#ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
131 using BptResolutionUnit = nanoseconds;
132#else
133 using BptResolutionUnit = microseconds;
134#endif
135 constexpr auto unitsPerHour = duration_cast<BptResolutionUnit>(hours(1)).count();
136
137 auto sinceEpoch = duration_cast<BptResolutionUnit>(timePoint - getUnixEpoch()).count();
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500138 bpt::ptime ptime = epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
Junxiao Shi855eaa52017-04-04 20:10:43 +0000139
140 return bpt::to_iso_string(ptime);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700141}
142
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500143static system_clock::TimePoint
144convertToTimePoint(const boost::posix_time::ptime& ptime)
145{
146 namespace bpt = boost::posix_time;
147 static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
148
149 // .total_seconds() has issue with large dates until Boost 1.66. See Issue #4478
150 // from_time_t has issues with large dates on 32-bit platforms
151 auto point = system_clock::time_point(seconds((ptime - epoch).ticks() / bpt::time_duration::ticks_per_second()));
152 point += microseconds((ptime - epoch).total_microseconds() % 1000000);
153 return point;
154}
155
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700156system_clock::TimePoint
157fromIsoString(const std::string& isoString)
158{
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500159 return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700160}
161
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700162std::string
163toString(const system_clock::TimePoint& timePoint,
164 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
165 const std::locale& locale/* = std::locale("C")*/)
166{
167 namespace bpt = boost::posix_time;
168 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
169
170 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
171 ptime += bpt::microseconds(micro);
172
173 bpt::time_facet* facet = new bpt::time_facet(format.c_str());
174 std::ostringstream formattedTimePoint;
175 formattedTimePoint.imbue(std::locale(locale, facet));
176 formattedTimePoint << ptime;
177
178 return formattedTimePoint.str();
179}
180
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700181system_clock::TimePoint
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500182fromString(const std::string& timePointStr,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700183 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
184 const std::locale& locale/* = std::locale("C")*/)
185{
186 namespace bpt = boost::posix_time;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700187
188 bpt::time_input_facet* facet = new bpt::time_input_facet(format);
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500189 std::istringstream is(timePointStr);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700190 is.imbue(std::locale(locale, facet));
191 bpt::ptime ptime;
192 is >> ptime;
193
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500194 return convertToTimePoint(ptime);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700195}
196
197} // namespace time
198} // namespace ndn
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800199
200namespace boost {
201namespace chrono {
202
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800203template<class CharT>
204std::basic_string<CharT>
205clock_string<ndn::time::system_clock, CharT>::since()
206{
207 if (ndn::time::g_systemClock == nullptr) {
208 // optimized default version
209 return clock_string<system_clock, CharT>::since();
210 }
211 else {
212 return ndn::time::g_systemClock->getSince();
213 }
214}
215
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800216template<class CharT>
217std::basic_string<CharT>
218clock_string<ndn::time::steady_clock, CharT>::since()
219{
220 if (ndn::time::g_steadyClock == nullptr) {
221 // optimized default version
222 return clock_string<ndn::time::base_steady_clock, CharT>::since();
223 }
224 else {
225 return ndn::time::g_steadyClock->getSince();
226 }
227}
228
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500229template struct clock_string<ndn::time::system_clock, char>;
230template struct clock_string<ndn::time::steady_clock, char>;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800231
232} // namespace chrono
233} // namespace boost