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-2020 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 "ndn-cxx/util/time.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 macOS 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 #else
134  microseconds;
135 #endif
136  constexpr auto unitsPerHour = duration_cast<BptResolution>(1_h).count();
137 
138  auto sinceEpoch = duration_cast<BptResolution>(timePoint - getUnixEpoch()).count();
139  return epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour);
140 }
141 
142 std::string
144 {
145  return boost::posix_time::to_iso_string(convertToPosixTime(timePoint));
146 }
147 
149 convertToTimePoint(const boost::posix_time::ptime& ptime)
150 {
151  namespace bpt = boost::posix_time;
152  static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
153 
154  // .total_seconds() has an issue with large dates until Boost 1.66, see #4478.
155  // time_t overflows for large dates on 32-bit platforms (Y2038 problem).
156  auto sinceEpoch = ptime - epoch;
157  auto point = system_clock::TimePoint(seconds(sinceEpoch.ticks() / bpt::time_duration::ticks_per_second()));
158  return point + microseconds(sinceEpoch.total_microseconds() % 1000000);
159 }
160 
162 fromIsoString(const std::string& isoString)
163 {
164  return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
165 }
166 
167 std::string
169  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
170  const std::locale& locale/* = std::locale("C")*/)
171 {
172  namespace bpt = boost::posix_time;
173 
174  std::ostringstream os;
175  auto* facet = new bpt::time_facet(format.data());
176  os.imbue(std::locale(locale, facet));
177  os << convertToPosixTime(timePoint);
178 
179  return os.str();
180 }
181 
183 fromString(const std::string& timePointStr,
184  const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
185  const std::locale& locale/* = std::locale("C")*/)
186 {
187  namespace bpt = boost::posix_time;
188 
189  std::istringstream is(timePointStr);
190  auto* facet = new bpt::time_input_facet(format);
191  is.imbue(std::locale(locale, facet));
192  bpt::ptime ptime;
193  is >> ptime;
194 
195  return convertToTimePoint(ptime);
196 }
197 
198 } // namespace time
199 } // namespace ndn
200 
201 namespace boost {
202 namespace chrono {
203 
204 template<class CharT>
205 std::basic_string<CharT>
206 clock_string<ndn::time::system_clock, CharT>::since()
207 {
208  if (ndn::time::g_systemClock == nullptr) {
209  // optimized default version
210  return clock_string<system_clock, CharT>::since();
211  }
212  else {
213  return ndn::time::g_systemClock->getSince();
214  }
215 }
216 
217 template<class CharT>
218 std::basic_string<CharT>
219 clock_string<ndn::time::steady_clock, CharT>::since()
220 {
221  if (ndn::time::g_steadyClock == nullptr) {
222  // optimized default version
223  return clock_string<ndn::time::base_steady_clock, CharT>::since();
224  }
225  else {
226  return ndn::time::g_steadyClock->getSince();
227  }
228 }
229 
230 template struct clock_string<ndn::time::system_clock, char>;
231 template struct clock_string<ndn::time::steady_clock, char>;
232 
233 } // namespace chrono
234 } // namespace boost
boost::chrono::system_clock::duration duration
Definition: time.hpp:189
Definition: data.cpp:26
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:162
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:192
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:219
boost::chrono::time_point< steady_clock > time_point
Definition: time.hpp:222
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:183
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:168
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:149
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:143
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:195
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