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