Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace time { |
| 29 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 30 | static shared_ptr<CustomSystemClock> g_systemClock; |
| 31 | static shared_ptr<CustomSteadyClock> g_steadyClock; |
| 32 | |
| 33 | // this method is defined in time-custom-clock.hpp |
| 34 | void |
| 35 | setCustomClocks(shared_ptr<CustomSteadyClock> steadyClock, |
| 36 | shared_ptr<CustomSystemClock> systemClock) |
| 37 | { |
| 38 | g_systemClock = systemClock; |
| 39 | g_steadyClock = steadyClock; |
| 40 | } |
| 41 | |
| 42 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | system_clock::time_point |
| 45 | system_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 | |
| 56 | std::time_t |
| 57 | system_clock::to_time_t(const time_point& t) noexcept |
| 58 | { |
| 59 | return duration_cast<seconds>(t.time_since_epoch()).count(); |
| 60 | } |
| 61 | |
| 62 | system_clock::time_point |
| 63 | system_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 | |
| 78 | steady_clock::time_point |
| 79 | steady_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 | |
| 90 | boost::posix_time::time_duration |
| 91 | steady_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 | |
| 110 | const system_clock::TimePoint& |
| 111 | getUnixEpoch() |
| 112 | { |
| 113 | static system_clock::TimePoint epoch = system_clock::from_time_t(0); |
| 114 | return epoch; |
| 115 | } |
| 116 | |
| 117 | milliseconds |
| 118 | toUnixTimestamp(const system_clock::TimePoint& point) |
| 119 | { |
| 120 | return duration_cast<milliseconds>(point - getUnixEpoch()); |
| 121 | } |
| 122 | |
| 123 | system_clock::TimePoint |
| 124 | fromUnixTimestamp(const milliseconds& duration) |
| 125 | { |
| 126 | return getUnixEpoch() + duration; |
| 127 | } |
| 128 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 129 | std::string |
| 130 | toIsoString(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 | |
| 146 | system_clock::TimePoint |
| 147 | fromIsoString(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 | |
| 161 | std::string |
| 162 | toString(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 | |
| 181 | system_clock::TimePoint |
| 182 | fromString(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 Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 204 | |
| 205 | namespace boost { |
| 206 | namespace chrono { |
| 207 | |
| 208 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 209 | |
| 210 | template<class CharT> |
| 211 | std::basic_string<CharT> |
| 212 | clock_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 | |
| 223 | template |
| 224 | struct clock_string<ndn::time::system_clock, char>; |
| 225 | |
| 226 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | template<class CharT> |
| 229 | std::basic_string<CharT> |
| 230 | clock_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 | |
| 241 | template |
| 242 | struct clock_string<ndn::time::steady_clock, char>; |
| 243 | |
| 244 | } // namespace chrono |
| 245 | } // namespace boost |