blob: b652a7bbc35fc8b02567c409d3a767a1961de2ef [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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>
26
27namespace ndn {
28namespace time {
29
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080030static shared_ptr<CustomSystemClock> g_systemClock;
31static shared_ptr<CustomSteadyClock> g_steadyClock;
32
33// this method is defined in time-custom-clock.hpp
34void
35setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock,
36 shared_ptr<CustomSystemClock> systemClock)
37{
38 g_systemClock = systemClock;
39 g_steadyClock = steadyClock;
40}
41
42/////////////////////////////////////////////////////////////////////////////////////////////
43
44system_clock::time_point
45system_clock::now() noexcept
46{
47 if (g_systemClock == nullptr) {
48 // optimized default version
49 return time_point(boost::chrono::system_clock::now().time_since_epoch());
50 }
51 else {
52 return g_systemClock->getNow();
53 }
54}
55
56std::time_t
57system_clock::to_time_t(const time_point& t) noexcept
58{
59 return duration_cast<seconds>(t.time_since_epoch()).count();
60}
61
62system_clock::time_point
63system_clock::from_time_t(std::time_t t) noexcept
64{
65 return time_point(seconds(t));
66}
67
68/////////////////////////////////////////////////////////////////////////////////////////////
69
70#ifdef __APPLE__
71 // Note that on OS X platform boost::steady_clock is not truly monotonic, so we use
72 // system_clock instead. Refer to https://svn.boost.org/trac/boost/ticket/7719)
73 typedef boost::chrono::system_clock base_steady_clock;
74#else
75 typedef boost::chrono::steady_clock base_steady_clock;
76#endif
77
78steady_clock::time_point
79steady_clock::now() noexcept
80{
81 if (g_steadyClock == nullptr) {
82 // optimized default version
83 return time_point(base_steady_clock::now().time_since_epoch());
84 }
85 else {
86 return g_steadyClock->getNow();
87 }
88}
89
90boost::posix_time::time_duration
91steady_clock::to_posix_duration(const duration& duration)
92{
93 if (g_steadyClock == nullptr) {
94 // optimized default version
95 return
96#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
97 boost::posix_time::nanoseconds(duration_cast<nanoseconds>(duration).count())
98#else
99 boost::posix_time::microseconds(duration_cast<microseconds>(duration).count())
100#endif
101 ;
102 }
103 else {
104 return g_steadyClock->toPosixDuration(duration);
105 }
106}
107
108/////////////////////////////////////////////////////////////////////////////////////////////
109
110const system_clock::TimePoint&
111getUnixEpoch()
112{
113 static system_clock::TimePoint epoch = system_clock::from_time_t(0);
114 return epoch;
115}
116
117milliseconds
118toUnixTimestamp(const system_clock::TimePoint& point)
119{
120 return duration_cast<milliseconds>(point - getUnixEpoch());
121}
122
123system_clock::TimePoint
124fromUnixTimestamp(const milliseconds& duration)
125{
126 return getUnixEpoch() + duration;
127}
128
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700129std::string
130toIsoString(const system_clock::TimePoint& timePoint)
131{
132 namespace bpt = boost::posix_time;
133 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
134
135 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
136 if (micro > 0)
137 {
138 ptime += bpt::microseconds(micro);
139 return bpt::to_iso_string(ptime);
140 }
141 else
142 return bpt::to_iso_string(ptime);
143}
144
145
146system_clock::TimePoint
147fromIsoString(const std::string& isoString)
148{
149 namespace bpt = boost::posix_time;
150 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
151
152 bpt::ptime ptime = bpt::from_iso_string(isoString);
153
154 system_clock::TimePoint point =
155 system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
156 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
157 return point;
158}
159
160
161std::string
162toString(const system_clock::TimePoint& timePoint,
163 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
164 const std::locale& locale/* = std::locale("C")*/)
165{
166 namespace bpt = boost::posix_time;
167 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
168
169 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
170 ptime += bpt::microseconds(micro);
171
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;
176
177 return formattedTimePoint.str();
178}
179
180
181system_clock::TimePoint
182fromString(const std::string& formattedTimePoint,
183 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;
187 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
188
189 bpt::time_input_facet* facet = new bpt::time_input_facet(format);
190 std::istringstream is(formattedTimePoint);
191
192 is.imbue(std::locale(locale, facet));
193 bpt::ptime ptime;
194 is >> ptime;
195
196 system_clock::TimePoint point =
197 system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
198 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
199 return point;
200}
201
202} // namespace time
203} // namespace ndn
Alexander Afanasyev85b17b82014-11-10 16:22:05 -0800204
205namespace boost {
206namespace chrono {
207
208/////////////////////////////////////////////////////////////////////////////////////////////
209
210template<class CharT>
211std::basic_string<CharT>
212clock_string<ndn::time::system_clock, CharT>::since()
213{
214 if (ndn::time::g_systemClock == nullptr) {
215 // optimized default version
216 return clock_string<system_clock, CharT>::since();
217 }
218 else {
219 return ndn::time::g_systemClock->getSince();
220 }
221}
222
223template
224struct clock_string<ndn::time::system_clock, char>;
225
226/////////////////////////////////////////////////////////////////////////////////////////////
227
228template<class CharT>
229std::basic_string<CharT>
230clock_string<ndn::time::steady_clock, CharT>::since()
231{
232 if (ndn::time::g_steadyClock == nullptr) {
233 // optimized default version
234 return clock_string<ndn::time::base_steady_clock, CharT>::since();
235 }
236 else {
237 return ndn::time::g_steadyClock->getSince();
238 }
239}
240
241template
242struct clock_string<ndn::time::steady_clock, char>;
243
244} // namespace chrono
245} // namespace boost