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 | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 29 | namespace time { |
| 30 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 31 | using boost::chrono::duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 33 | typedef duration<boost::int_least32_t, boost::ratio<86400> > days; |
| 34 | using boost::chrono::hours; |
| 35 | using boost::chrono::minutes; |
| 36 | using boost::chrono::seconds; |
| 37 | |
| 38 | using boost::chrono::milliseconds; |
| 39 | using boost::chrono::microseconds; |
| 40 | using boost::chrono::nanoseconds; |
| 41 | |
| 42 | using boost::chrono::duration_cast; |
| 43 | |
| 44 | /** |
| 45 | * \brief System clock |
| 46 | * |
| 47 | * System clock represents the system-wide real time wall clock. |
| 48 | * |
| 49 | * It may not be monotonic: on most systems, the system time can be |
| 50 | * adjusted at any moment. It is the only clock that has the ability |
| 51 | * to be displayed and converted to/from UNIX timestamp. |
| 52 | * |
| 53 | * To get current TimePoint: |
| 54 | * |
| 55 | * <code> |
| 56 | * system_clock::TimePoint now = system_clock::now(); |
| 57 | * </code> |
| 58 | * |
| 59 | * To convert TimePoint to/from UNIX timestamp: |
| 60 | * |
| 61 | * <code> |
| 62 | * system_clock::TimePoint time = ...; |
| 63 | * uint64_t timestampInMilliseconds = toUnixTimestamp(time).count(); |
| 64 | * system_clock::TimePoint time2 = fromUnixTimestamp(time::milliseconds(timestampInMilliseconds)); |
| 65 | * </code> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 67 | class system_clock : public boost::chrono::system_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 68 | { |
| 69 | public: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 70 | typedef time_point TimePoint; |
| 71 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 73 | // /// \brief Get current TimePoint |
| 74 | // TimePoint |
| 75 | // now(); |
| 76 | }; // class system_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 78 | /** |
| 79 | * \brief Steady clock |
| 80 | * |
| 81 | * Steady clock represents a monotonic clock. The time points of this |
| 82 | * clock cannot decrease as physical time moves forward. This clock is |
| 83 | * not related to wall clock time, and is best suitable for measuring |
| 84 | * intervals. |
| 85 | * |
| 86 | * Note that on OS X platform this defaults to system clock and is not |
| 87 | * truly monotonic. Refer to https://svn.boost.org/trac/boost/ticket/7719) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 88 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 89 | class steady_clock : public |
| 90 | #ifdef __APPLE__ |
| 91 | // steady_clock may go backwards on OS X platforms, so use system_clock |
| 92 | // instead |
| 93 | boost::chrono::system_clock |
| 94 | #else |
| 95 | boost::chrono::steady_clock |
| 96 | #endif |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 97 | { |
| 98 | public: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | typedef time_point TimePoint; |
| 100 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 102 | // /// \brief Get current TimePoint |
| 103 | // TimePoint |
| 104 | // now(); |
| 105 | }; // class steady_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 108 | /** |
| 109 | * \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) |
| 110 | */ |
| 111 | inline const system_clock::TimePoint& |
| 112 | getUnixEpoch() |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 113 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 114 | static system_clock::TimePoint epoch = system_clock::from_time_t(0); |
| 115 | return epoch; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 118 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 119 | * \brief Convert system_clock::TimePoint to UNIX timestamp |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 120 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 121 | inline milliseconds |
| 122 | toUnixTimestamp(const system_clock::TimePoint& point) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 124 | return duration_cast<milliseconds>(point - getUnixEpoch()); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 128 | * \brief Convert UNIX timestamp to system_clock::TimePoint |
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 | inline system_clock::TimePoint |
| 131 | fromUnixTimestamp(const milliseconds& duration) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 132 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 133 | return getUnixEpoch() + duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 137 | * \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff) |
| 138 | * |
| 139 | * If timePoint contains doesn't contain fractional seconds the |
| 140 | * output format is YYYYMMDDTHHMMSS |
| 141 | * |
| 142 | * Examples: |
| 143 | * |
| 144 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 145 | * - with fractional microseconds: 20020131T100001,123456 |
| 146 | * - with fractional milliseconds: 20020131T100001,123 |
| 147 | * - without fractional seconds: 20020131T100001 |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 148 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 149 | std::string |
| 150 | toIsoString(const system_clock::TimePoint& timePoint); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 151 | |
| 152 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 153 | * \brief Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation |
| 154 | * to the internal time format |
| 155 | * |
| 156 | * Examples of accepted ISO strings: |
| 157 | * |
| 158 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 159 | * - with fractional microseconds: 20020131T100001,123456 |
| 160 | * - with fractional milliseconds: 20020131T100001,123 |
| 161 | * - without fractional seconds: 20020131T100001 |
| 162 | * |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 163 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 164 | system_clock::TimePoint |
| 165 | fromIsoString(const std::string& isoString); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * \brief Convert time point to string with specified format |
| 169 | * |
| 170 | * By default, `%Y-%m-%d %H:%M:%S` is used, producing dates like |
| 171 | * `2014-04-10 22:51:00` |
| 172 | * |
| 173 | * \param timePoint time point of system_clock |
| 174 | * \param format desired output format (default: `%Y-%m-%d %H:%M:%S`) |
| 175 | * \param locale desired locale (default: "C" locale) |
| 176 | * |
| 177 | * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags |
| 178 | * described possible formatting flags |
| 179 | **/ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 180 | std::string |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 181 | toString(const system_clock::TimePoint& timePoint, |
| 182 | const std::string& format = "%Y-%m-%d %H:%M:%S", |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 183 | const std::locale& locale = std::locale("C")); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * \brief Convert from string of specified format into time point |
| 187 | * |
| 188 | * By default, `%Y-%m-%d %H:%M:%S` is used, accepting dates like |
| 189 | * `2014-04-10 22:51:00` |
| 190 | * |
| 191 | * \param formattedTimePoint string representing time point |
| 192 | * \param format input output format (default: `%Y-%m-%d %H:%M:%S`) |
| 193 | * \param locale input locale (default: "C" locale) |
| 194 | * |
| 195 | * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags |
| 196 | * described possible formatting flags |
| 197 | */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 198 | system_clock::TimePoint |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 199 | fromString(const std::string& formattedTimePoint, |
| 200 | const std::string& format = "%Y-%m-%d %H:%M:%S", |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 201 | const std::locale& locale = std::locale("C")); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 202 | |
| 203 | } // namespace time |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 204 | } // namespace ndn |
| 205 | |
| 206 | #endif // NDN_TIME_HPP |