time.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #include "time.hpp"
23 #include "time-custom-clock.hpp"
24 
25 #include <boost/date_time/posix_time/posix_time.hpp>
26 #include <sstream>
27 
28 namespace ndn {
29 namespace time {
30 
31 static shared_ptr<CustomSystemClock> g_systemClock;
32 static shared_ptr<CustomSteadyClock> g_steadyClock;
33 
34 // this function is declared in time-custom-clock.hpp
35 void
36 setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock,
37  shared_ptr<CustomSystemClock> systemClock)
38 {
39  g_systemClock = std::move(systemClock);
40  g_steadyClock = std::move(steadyClock);
41 }
42 
44 
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 
57 std::time_t
59 {
60  return duration_cast<seconds>(t.time_since_epoch()).count();
61 }
62 
64 system_clock::from_time_t(std::time_t t) noexcept
65 {
66  return time_point(seconds(t));
67 }
68 
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 
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 
92 steady_clock::to_wait_duration(steady_clock::duration d)
93 {
94  if (g_steadyClock == nullptr) {
95  // optimized default version
96  return d;
97  }
98  else {
99  return g_steadyClock->toWaitDuration(d);
100  }
101 }
102 
104 
107 {
108  static constexpr system_clock::TimePoint epoch(seconds::zero());
109  return epoch;
110 }
111 
112 milliseconds
114 {
115  return duration_cast<milliseconds>(point - getUnixEpoch());
116 }
117 
120 {
121  return getUnixEpoch() + duration;
122 }
123 
124 static boost::posix_time::ptime
126 {
127  namespace bpt = boost::posix_time;
128  static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
129 
130  using BptResolution =
131 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
132  nanoseconds;
133 #elif defined(BOOST_DATE_TIME_HAS_MICROSECONDS)
134  microseconds;
135 #else
136  milliseconds;
137 #endif
138  constexpr auto unitsPerHour = duration_cast<BptResolution>(1_h).count();
139 
140  auto sinceEpoch = duration_cast<BptResolution>(timePoint - getUnixEpoch()).count();
141  return epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
142 }
143 
144 std::string
146 {
147  return boost::posix_time::to_iso_string(convertToPosixTime(timePoint));
148 }
149 
151 convertToTimePoint(const boost::posix_time::ptime& ptime)
152 {
153  namespace bpt = boost::posix_time;
154  static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
155 
156  // .total_seconds() has an issue with large dates until Boost 1.66, see #4478.
157  // time_t overflows for large dates on 32-bit platforms (Y2038 problem).
158  auto sinceEpoch = ptime - epoch;
159  auto point = system_clock::TimePoint(seconds(sinceEpoch.ticks() / bpt::time_duration::ticks_per_second()));
160  return point + microseconds(sinceEpoch.total_microseconds() % 1000000);
161 }
162 
164 fromIsoString(const std::string& isoString)
165 {
166  return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
167 }
168 
169 std::string
171  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
172  const std::locale& locale/* = std::locale("C")*/)
173 {
174  namespace bpt = boost::posix_time;
175 
176  std::ostringstream os;
177  auto* facet = new bpt::time_facet(format.data());
178  os.imbue(std::locale(locale, facet));
179  os << convertToPosixTime(timePoint);
180 
181  return os.str();
182 }
183 
185 fromString(const std::string& timePointStr,
186  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
187  const std::locale& locale/* = std::locale("C")*/)
188 {
189  namespace bpt = boost::posix_time;
190 
191  std::istringstream is(timePointStr);
192  auto* facet = new bpt::time_input_facet(format);
193  is.imbue(std::locale(locale, facet));
194  bpt::ptime ptime;
195  is >> ptime;
196 
197  return convertToTimePoint(ptime);
198 }
199 
200 } // namespace time
201 } // namespace ndn
202 
203 namespace boost {
204 namespace chrono {
205 
206 template<class CharT>
207 std::basic_string<CharT>
208 clock_string<ndn::time::system_clock, CharT>::since()
209 {
210  if (ndn::time::g_systemClock == nullptr) {
211  // optimized default version
212  return clock_string<system_clock, CharT>::since();
213  }
214  else {
215  return ndn::time::g_systemClock->getSince();
216  }
217 }
218 
219 template<class CharT>
220 std::basic_string<CharT>
221 clock_string<ndn::time::steady_clock, CharT>::since()
222 {
223  if (ndn::time::g_steadyClock == nullptr) {
224  // optimized default version
225  return clock_string<ndn::time::base_steady_clock, CharT>::since();
226  }
227  else {
228  return ndn::time::g_steadyClock->getSince();
229  }
230 }
231 
232 template struct clock_string<ndn::time::system_clock, char>;
233 template struct clock_string<ndn::time::steady_clock, char>;
234 
235 } // namespace chrono
236 } // namespace boost
boost::chrono::system_clock::duration duration
Definition: time.hpp:190
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
system_clock::TimePoint fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.cpp:119
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:164
Definition: block.hpp:31
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:193
static time_point now() noexcept
Definition: time.cpp:80
static std::time_t to_time_t(const time_point &t) noexcept
Definition: time.cpp:58
boost::chrono::steady_clock::duration duration
Definition: time.hpp:220
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:223
static time_point now() noexcept
Definition: time.cpp:46
system_clock::TimePoint fromString(const std::string &timePointStr, const std::string &format, const std::locale &locale)
Convert from string of specified format into time point.
Definition: time.cpp:185
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format, const std::locale &locale)
Convert time point to string with specified format.
Definition: time.cpp:170
boost::chrono::steady_clock base_steady_clock
Definition: time.cpp:76
static system_clock::TimePoint convertToTimePoint(const boost::posix_time::ptime &ptime)
Definition: time.cpp:151
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:145
void setCustomClocks(shared_ptr< CustomSteadyClock > steadyClock=nullptr, shared_ptr< CustomSystemClock > systemClock=nullptr)
Set custom system and steady clocks.
Definition: time.cpp:36
time_point TimePoint
Definition: time.hpp:196
static shared_ptr< CustomSteadyClock > g_steadyClock
Definition: time.cpp:32
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:113
static boost::posix_time::ptime convertToPosixTime(const system_clock::TimePoint &timePoint)
Definition: time.cpp:125
static shared_ptr< CustomSystemClock > g_systemClock
Definition: time.cpp:31
static time_point from_time_t(std::time_t t) noexcept
Definition: time.cpp:64
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Definition: time.cpp:106