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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -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 | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 22 | #ifndef NDN_UTIL_TIME_HPP |
| 23 | #define NDN_UTIL_TIME_HPP |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 25 | #include "../common.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 26 | |
| 27 | #include <boost/asio/wait_traits.hpp> |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 28 | #include <boost/chrono.hpp> |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 31 | namespace time { |
| 32 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 33 | using boost::chrono::duration; |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 34 | using boost::chrono::duration_cast; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 36 | using days = duration<int_fast32_t, boost::ratio<86400>>; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 37 | using boost::chrono::hours; |
| 38 | using boost::chrono::minutes; |
| 39 | using boost::chrono::seconds; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 40 | using boost::chrono::milliseconds; |
| 41 | using boost::chrono::microseconds; |
| 42 | using boost::chrono::nanoseconds; |
| 43 | |
Junxiao Shi | dc2d6d2 | 2016-08-04 14:30:23 +0000 | [diff] [blame] | 44 | /** \return the absolute value of the duration d |
| 45 | * \note The function does not participate in the overload resolution |
| 46 | * unless std::numeric_limits<Rep>::is_signed is true. |
| 47 | */ |
| 48 | template<typename Rep, typename Period, |
Junxiao Shi | 5537d13 | 2016-08-05 03:48:01 +0000 | [diff] [blame] | 49 | typename = typename std::enable_if<std::numeric_limits<Rep>::is_signed>::type> |
Junxiao Shi | dc2d6d2 | 2016-08-04 14:30:23 +0000 | [diff] [blame] | 50 | constexpr duration<Rep, Period> |
| 51 | abs(duration<Rep, Period> d) |
| 52 | { |
| 53 | return d >= d.zero() ? d : -d; |
| 54 | } |
| 55 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 56 | /** |
| 57 | * \brief System clock |
| 58 | * |
| 59 | * System clock represents the system-wide real time wall clock. |
| 60 | * |
| 61 | * It may not be monotonic: on most systems, the system time can be |
| 62 | * adjusted at any moment. It is the only clock that has the ability |
| 63 | * to be displayed and converted to/from UNIX timestamp. |
| 64 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 65 | * To get the current time: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 66 | * |
| 67 | * <code> |
| 68 | * system_clock::TimePoint now = system_clock::now(); |
| 69 | * </code> |
| 70 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 71 | * To convert a TimePoint to/from UNIX timestamp: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | * |
| 73 | * <code> |
| 74 | * system_clock::TimePoint time = ...; |
| 75 | * uint64_t timestampInMilliseconds = toUnixTimestamp(time).count(); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 76 | * system_clock::TimePoint time2 = fromUnixTimestamp(milliseconds(timestampInMilliseconds)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | * </code> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 79 | class system_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | { |
| 81 | public: |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 82 | using duration = boost::chrono::system_clock::duration; |
| 83 | using rep = duration::rep; |
| 84 | using period = duration::period; |
| 85 | using time_point = boost::chrono::time_point<system_clock>; |
| 86 | static constexpr bool is_steady = boost::chrono::system_clock::is_steady; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | typedef time_point TimePoint; |
| 89 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 91 | static time_point |
| 92 | now() noexcept; |
| 93 | |
| 94 | static std::time_t |
| 95 | to_time_t(const time_point& t) noexcept; |
| 96 | |
| 97 | static time_point |
| 98 | from_time_t(std::time_t t) noexcept; |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 99 | }; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 101 | /** |
| 102 | * \brief Steady clock |
| 103 | * |
| 104 | * Steady clock represents a monotonic clock. The time points of this |
| 105 | * clock cannot decrease as physical time moves forward. This clock is |
| 106 | * not related to wall clock time, and is best suitable for measuring |
| 107 | * intervals. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 108 | */ |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 109 | class steady_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 110 | { |
| 111 | public: |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 112 | using duration = boost::chrono::steady_clock::duration; |
| 113 | using rep = duration::rep; |
| 114 | using period = duration::period; |
| 115 | using time_point = boost::chrono::time_point<steady_clock>; |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 116 | static constexpr bool is_steady = true; |
| 117 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 118 | typedef time_point TimePoint; |
| 119 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 121 | static time_point |
| 122 | now() noexcept; |
| 123 | |
| 124 | private: |
| 125 | /** |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 126 | * \brief Trait function used in detail::SteadyTimer to select proper waiting time |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 127 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 128 | * Mock time implementations should return the minimum value to ensure |
| 129 | * that Boost.Asio doesn't perform any waiting on mock timers. |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 130 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 131 | * @sa http://blog.think-async.com/2007/08/time-travel.html |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 132 | */ |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 133 | static duration |
| 134 | to_wait_duration(duration d); |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 135 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 136 | friend struct boost::asio::wait_traits<steady_clock>; // see steady-timer.hpp |
| 137 | }; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 139 | /** |
| 140 | * \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) |
| 141 | */ |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 142 | const system_clock::TimePoint& |
| 143 | getUnixEpoch(); |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 145 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 146 | * \brief Convert system_clock::TimePoint to UNIX timestamp |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 147 | */ |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 148 | milliseconds |
| 149 | toUnixTimestamp(const system_clock::TimePoint& point); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 152 | * \brief Convert UNIX timestamp to system_clock::TimePoint |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 153 | */ |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 154 | system_clock::TimePoint |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 155 | fromUnixTimestamp(milliseconds duration); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 158 | * \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff) |
| 159 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 160 | * If \p timePoint contains doesn't contain fractional seconds, |
| 161 | * the output format is YYYYMMDDTHHMMSS |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 162 | * |
| 163 | * Examples: |
| 164 | * |
| 165 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 166 | * - with fractional microseconds: 20020131T100001,123456 |
| 167 | * - with fractional milliseconds: 20020131T100001,123 |
| 168 | * - without fractional seconds: 20020131T100001 |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 169 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 170 | std::string |
| 171 | toIsoString(const system_clock::TimePoint& timePoint); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 172 | |
| 173 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 174 | * \brief Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation |
| 175 | * to the internal time format |
| 176 | * |
| 177 | * Examples of accepted ISO strings: |
| 178 | * |
| 179 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 180 | * - with fractional microseconds: 20020131T100001,123456 |
| 181 | * - with fractional milliseconds: 20020131T100001,123 |
| 182 | * - without fractional seconds: 20020131T100001 |
| 183 | * |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 184 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 185 | system_clock::TimePoint |
| 186 | fromIsoString(const std::string& isoString); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * \brief Convert time point to string with specified format |
| 190 | * |
| 191 | * By default, `%Y-%m-%d %H:%M:%S` is used, producing dates like |
| 192 | * `2014-04-10 22:51:00` |
| 193 | * |
| 194 | * \param timePoint time point of system_clock |
| 195 | * \param format desired output format (default: `%Y-%m-%d %H:%M:%S`) |
| 196 | * \param locale desired locale (default: "C" locale) |
| 197 | * |
Davide Pesavento | e6e6fde | 2016-04-16 14:44:45 +0200 | [diff] [blame] | 198 | * \sa http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/date_time_io.html#date_time.format_flags |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 199 | * describes possible formatting flags |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 200 | **/ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 201 | std::string |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 202 | toString(const system_clock::TimePoint& timePoint, |
| 203 | const std::string& format = "%Y-%m-%d %H:%M:%S", |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 204 | const std::locale& locale = std::locale("C")); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * \brief Convert from string of specified format into time point |
| 208 | * |
| 209 | * By default, `%Y-%m-%d %H:%M:%S` is used, accepting dates like |
| 210 | * `2014-04-10 22:51:00` |
| 211 | * |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 212 | * \param timePointStr string representing time point |
| 213 | * \param format input output format (default: `%Y-%m-%d %H:%M:%S`) |
| 214 | * \param locale input locale (default: "C" locale) |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 215 | * |
Davide Pesavento | e6e6fde | 2016-04-16 14:44:45 +0200 | [diff] [blame] | 216 | * \sa http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/date_time_io.html#date_time.format_flags |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 217 | * describes possible formatting flags |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 218 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 219 | system_clock::TimePoint |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 220 | fromString(const std::string& timePointStr, |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 221 | const std::string& format = "%Y-%m-%d %H:%M:%S", |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 222 | const std::locale& locale = std::locale("C")); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 223 | |
| 224 | } // namespace time |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 225 | } // namespace ndn |
| 226 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 227 | namespace boost { |
| 228 | namespace chrono { |
| 229 | |
| 230 | template<class CharT> |
| 231 | struct clock_string<ndn::time::system_clock, CharT> |
| 232 | { |
| 233 | static std::basic_string<CharT> |
| 234 | since(); |
| 235 | }; |
| 236 | |
| 237 | template<class CharT> |
| 238 | struct clock_string<ndn::time::steady_clock, CharT> |
| 239 | { |
| 240 | static std::basic_string<CharT> |
| 241 | since(); |
| 242 | }; |
| 243 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 244 | extern template struct clock_string<ndn::time::system_clock, char>; |
| 245 | extern template struct clock_string<ndn::time::steady_clock, char>; |
| 246 | |
Alexander Afanasyev | 85b17b8 | 2014-11-10 16:22:05 -0800 | [diff] [blame] | 247 | } // namespace chrono |
| 248 | } // namespace boost |
| 249 | |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 250 | #endif // NDN_UTIL_TIME_HPP |