blob: e9af51fc07dfbfc0fce93d885b8a9b8badf3a8bc [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 Afanasyev258ec2b2014-05-14 16:15:37 -0700143system_clock::TimePoint
144fromIsoString(const std::string& isoString)
145{
146 namespace bpt = boost::posix_time;
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500147 static bpt::ptime epoch = bpt::from_time_t(0);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700148
149 bpt::ptime ptime = bpt::from_iso_string(isoString);
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500150 auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
151 point += microseconds((ptime - epoch).total_microseconds() % 1000000);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700152
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700153 return point;
154}
155
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700156std::string
157toString(const system_clock::TimePoint& timePoint,
158 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
159 const std::locale& locale/* = std::locale("C")*/)
160{
161 namespace bpt = boost::posix_time;
162 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
163
164 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
165 ptime += bpt::microseconds(micro);
166
167 bpt::time_facet* facet = new bpt::time_facet(format.c_str());
168 std::ostringstream formattedTimePoint;
169 formattedTimePoint.imbue(std::locale(locale, facet));
170 formattedTimePoint << ptime;
171
172 return formattedTimePoint.str();
173}
174
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700175system_clock::TimePoint
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500176fromString(const std::string& timePointStr,
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700177 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
178 const std::locale& locale/* = std::locale("C")*/)
179{
180 namespace bpt = boost::posix_time;
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500181 static bpt::ptime epoch = bpt::from_time_t(0);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700182
183 bpt::time_input_facet* facet = new bpt::time_input_facet(format);
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500184 std::istringstream is(timePointStr);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700185 is.imbue(std::locale(locale, facet));
186 bpt::ptime ptime;
187 is >> ptime;
188
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500189 auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
190 point += microseconds((ptime - epoch).total_microseconds() % 1000000);
191
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700192 return point;
193}
194
195} // namespace time
196} // namespace ndn
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800197
198namespace boost {
199namespace chrono {
200
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800201template<class CharT>
202std::basic_string<CharT>
203clock_string<ndn::time::system_clock, CharT>::since()
204{
205 if (ndn::time::g_systemClock == nullptr) {
206 // optimized default version
207 return clock_string<system_clock, CharT>::since();
208 }
209 else {
210 return ndn::time::g_systemClock->getSince();
211 }
212}
213
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800214template<class CharT>
215std::basic_string<CharT>
216clock_string<ndn::time::steady_clock, CharT>::since()
217{
218 if (ndn::time::g_steadyClock == nullptr) {
219 // optimized default version
220 return clock_string<ndn::time::base_steady_clock, CharT>::since();
221 }
222 else {
223 return ndn::time::g_steadyClock->getSince();
224 }
225}
226
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500227template struct clock_string<ndn::time::system_clock, char>;
228template struct clock_string<ndn::time::steady_clock, char>;
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800229
230} // namespace chrono
231} // namespace boost