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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 108 | static auto epoch = system_clock::from_time_t(0); |
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 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 124 | std::string |
| 125 | toIsoString(const system_clock::TimePoint& timePoint) |
| 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 | |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 130 | #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 Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 138 | bpt::ptime ptime = epoch + bpt::time_duration(sinceEpoch / unitsPerHour, 0, 0, sinceEpoch % unitsPerHour); |
Junxiao Shi | 855eaa5 | 2017-04-04 20:10:43 +0000 | [diff] [blame] | 139 | |
| 140 | return bpt::to_iso_string(ptime); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 143 | system_clock::TimePoint |
| 144 | fromIsoString(const std::string& isoString) |
| 145 | { |
| 146 | namespace bpt = boost::posix_time; |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 147 | static bpt::ptime epoch = bpt::from_time_t(0); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 148 | |
| 149 | bpt::ptime ptime = bpt::from_iso_string(isoString); |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 150 | auto point = system_clock::from_time_t((ptime - epoch).total_seconds()); |
| 151 | point += microseconds((ptime - epoch).total_microseconds() % 1000000); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 152 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 153 | return point; |
| 154 | } |
| 155 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 156 | std::string |
| 157 | toString(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 Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 175 | system_clock::TimePoint |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 176 | fromString(const std::string& timePointStr, |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 177 | 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 Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 181 | static bpt::ptime epoch = bpt::from_time_t(0); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 182 | |
| 183 | bpt::time_input_facet* facet = new bpt::time_input_facet(format); |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 184 | std::istringstream is(timePointStr); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 185 | is.imbue(std::locale(locale, facet)); |
| 186 | bpt::ptime ptime; |
| 187 | is >> ptime; |
| 188 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 189 | auto point = system_clock::from_time_t((ptime - epoch).total_seconds()); |
| 190 | point += microseconds((ptime - epoch).total_microseconds() % 1000000); |
| 191 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 192 | return point; |
| 193 | } |
| 194 | |
| 195 | } // namespace time |
| 196 | } // namespace ndn |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 197 | |
| 198 | namespace boost { |
| 199 | namespace chrono { |
| 200 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 201 | template<class CharT> |
| 202 | std::basic_string<CharT> |
| 203 | clock_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 Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 214 | template<class CharT> |
| 215 | std::basic_string<CharT> |
| 216 | clock_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 Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 227 | template struct clock_string<ndn::time::system_clock, char>; |
| 228 | template struct clock_string<ndn::time::steady_clock, char>; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 229 | |
| 230 | } // namespace chrono |
| 231 | } // namespace boost |