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