Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "time.hpp" |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 23 | #include "time-custom-clock.hpp" |
| 24 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #include <boost/date_time/posix_time/posix_time.hpp> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 26 | #include <sstream> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace time { |
| 30 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 31 | static shared_ptr<CustomSystemClock> g_systemClock; |
| 32 | static shared_ptr<CustomSteadyClock> g_steadyClock; |
| 33 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 34 | // this function is declared in time-custom-clock.hpp |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 35 | void |
| 36 | setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock, |
| 37 | shared_ptr<CustomSystemClock> systemClock) |
| 38 | { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 39 | g_systemClock = std::move(systemClock); |
| 40 | g_steadyClock = std::move(steadyClock); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | system_clock::time_point |
| 46 | system_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 | |
| 57 | std::time_t |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 58 | system_clock::to_time_t(const system_clock::time_point& t) noexcept |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 59 | { |
| 60 | return duration_cast<seconds>(t.time_since_epoch()).count(); |
| 61 | } |
| 62 | |
| 63 | system_clock::time_point |
| 64 | system_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 | |
| 79 | steady_clock::time_point |
| 80 | steady_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 Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 91 | steady_clock::duration |
| 92 | steady_clock::to_wait_duration(steady_clock::duration d) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 93 | { |
| 94 | if (g_steadyClock == nullptr) { |
| 95 | // optimized default version |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 96 | return d; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 97 | } |
| 98 | else { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 99 | return g_steadyClock->toWaitDuration(d); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | const system_clock::TimePoint& |
| 106 | getUnixEpoch() |
| 107 | { |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 108 | static constexpr system_clock::TimePoint epoch(seconds::zero()); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 109 | return epoch; |
| 110 | } |
| 111 | |
| 112 | milliseconds |
| 113 | toUnixTimestamp(const system_clock::TimePoint& point) |
| 114 | { |
| 115 | return duration_cast<milliseconds>(point - getUnixEpoch()); |
| 116 | } |
| 117 | |
| 118 | system_clock::TimePoint |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 119 | fromUnixTimestamp(milliseconds duration) |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 120 | { |
| 121 | return getUnixEpoch() + duration; |
| 122 | } |
| 123 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 124 | static boost::posix_time::ptime |
| 125 | convertToPosixTime(const system_clock::TimePoint& timePoint) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 126 | { |
| 127 | namespace bpt = boost::posix_time; |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 128 | static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 130 | using BptResolution = |
| 131 | #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS) |
| 132 | nanoseconds; |
| 133 | #elif defined(BOOST_DATE_TIME_HAS_MICROSECONDS) |
| 134 | microseconds; |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 135 | #else |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 136 | milliseconds; |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 137 | #endif |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 138 | constexpr auto unitsPerHour = duration_cast<BptResolution>(1_h).count(); |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 139 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 140 | auto sinceEpoch = duration_cast<BptResolution>(timePoint - getUnixEpoch()).count(); |
| 141 | return epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour); |
| 142 | } |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 143 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 144 | std::string |
| 145 | toIsoString(const system_clock::TimePoint& timePoint) |
| 146 | { |
| 147 | return boost::posix_time::to_iso_string(convertToPosixTime(timePoint)); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 150 | static system_clock::TimePoint |
| 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 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 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); |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 163 | system_clock::TimePoint |
| 164 | fromIsoString(const std::string& isoString) |
| 165 | { |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 166 | return convertToTimePoint(boost::posix_time::from_iso_string(isoString)); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 169 | std::string |
| 170 | toString(const system_clock::TimePoint& timePoint, |
| 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; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 176 | std::ostringstream os; |
| 177 | auto* facet = new bpt::time_facet(format.data()); |
| 178 | os.imbue(std::locale(locale, facet)); |
| 179 | os << convertToPosixTime(timePoint); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 180 | |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 181 | return os.str(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 184 | system_clock::TimePoint |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 185 | fromString(const std::string& timePointStr, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 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; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 190 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 191 | std::istringstream is(timePointStr); |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 192 | auto* facet = new bpt::time_input_facet(format); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 193 | is.imbue(std::locale(locale, facet)); |
| 194 | bpt::ptime ptime; |
| 195 | is >> ptime; |
| 196 | |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 197 | return convertToTimePoint(ptime); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | } // namespace time |
| 201 | } // namespace ndn |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 202 | |
| 203 | namespace boost { |
| 204 | namespace chrono { |
| 205 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 232 | template struct clock_string<ndn::time::system_clock, char>; |
| 233 | template struct clock_string<ndn::time::steady_clock, char>; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 234 | |
| 235 | } // namespace chrono |
| 236 | } // namespace boost |