Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 1 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_TIME_HPP |
| 7 | #define NDN_TIME_HPP |
| 8 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 9 | #include "../common.hpp" |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 10 | #include <boost/chrono.hpp> |
| 11 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 14 | namespace time { |
| 15 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 16 | using boost::chrono::duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 18 | typedef duration<boost::int_least32_t, boost::ratio<86400> > days; |
| 19 | using boost::chrono::hours; |
| 20 | using boost::chrono::minutes; |
| 21 | using boost::chrono::seconds; |
| 22 | |
| 23 | using boost::chrono::milliseconds; |
| 24 | using boost::chrono::microseconds; |
| 25 | using boost::chrono::nanoseconds; |
| 26 | |
| 27 | using boost::chrono::duration_cast; |
| 28 | |
| 29 | /** |
| 30 | * \brief System clock |
| 31 | * |
| 32 | * System clock represents the system-wide real time wall clock. |
| 33 | * |
| 34 | * It may not be monotonic: on most systems, the system time can be |
| 35 | * adjusted at any moment. It is the only clock that has the ability |
| 36 | * to be displayed and converted to/from UNIX timestamp. |
| 37 | * |
| 38 | * To get current TimePoint: |
| 39 | * |
| 40 | * <code> |
| 41 | * system_clock::TimePoint now = system_clock::now(); |
| 42 | * </code> |
| 43 | * |
| 44 | * To convert TimePoint to/from UNIX timestamp: |
| 45 | * |
| 46 | * <code> |
| 47 | * system_clock::TimePoint time = ...; |
| 48 | * uint64_t timestampInMilliseconds = toUnixTimestamp(time).count(); |
| 49 | * system_clock::TimePoint time2 = fromUnixTimestamp(time::milliseconds(timestampInMilliseconds)); |
| 50 | * </code> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 52 | class system_clock : public boost::chrono::system_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 53 | { |
| 54 | public: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 55 | typedef time_point TimePoint; |
| 56 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | // /// \brief Get current TimePoint |
| 59 | // TimePoint |
| 60 | // now(); |
| 61 | }; // class system_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | /** |
| 64 | * \brief Steady clock |
| 65 | * |
| 66 | * Steady clock represents a monotonic clock. The time points of this |
| 67 | * clock cannot decrease as physical time moves forward. This clock is |
| 68 | * not related to wall clock time, and is best suitable for measuring |
| 69 | * intervals. |
| 70 | * |
| 71 | * Note that on OS X platform this defaults to system clock and is not |
| 72 | * truly monotonic. Refer to https://svn.boost.org/trac/boost/ticket/7719) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 73 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | class steady_clock : public |
| 75 | #ifdef __APPLE__ |
| 76 | // steady_clock may go backwards on OS X platforms, so use system_clock |
| 77 | // instead |
| 78 | boost::chrono::system_clock |
| 79 | #else |
| 80 | boost::chrono::steady_clock |
| 81 | #endif |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 82 | { |
| 83 | public: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 84 | typedef time_point TimePoint; |
| 85 | typedef duration Duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 87 | // /// \brief Get current TimePoint |
| 88 | // TimePoint |
| 89 | // now(); |
| 90 | }; // class steady_clock |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 93 | /** |
| 94 | * \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) |
| 95 | */ |
| 96 | inline const system_clock::TimePoint& |
| 97 | getUnixEpoch() |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | static system_clock::TimePoint epoch = system_clock::from_time_t(0); |
| 100 | return epoch; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 103 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 104 | * \brief Convert system_clock::TimePoint to UNIX timestamp |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 105 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 106 | inline milliseconds |
| 107 | toUnixTimestamp(const system_clock::TimePoint& point) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | return duration_cast<milliseconds>(point - getUnixEpoch()); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 113 | * \brief Convert UNIX timestamp to system_clock::TimePoint |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 114 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 115 | inline system_clock::TimePoint |
| 116 | fromUnixTimestamp(const milliseconds& duration) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 118 | return getUnixEpoch() + duration; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 122 | * \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff) |
| 123 | * |
| 124 | * If timePoint contains doesn't contain fractional seconds the |
| 125 | * output format is YYYYMMDDTHHMMSS |
| 126 | * |
| 127 | * Examples: |
| 128 | * |
| 129 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 130 | * - with fractional microseconds: 20020131T100001,123456 |
| 131 | * - with fractional milliseconds: 20020131T100001,123 |
| 132 | * - without fractional seconds: 20020131T100001 |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 133 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 134 | inline std::string |
| 135 | toIsoString(const system_clock::TimePoint& timePoint) |
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 | boost::posix_time::ptime ptime = boost::posix_time::from_time_t( |
| 138 | system_clock::TimePoint::clock::to_time_t(timePoint)); |
| 139 | |
| 140 | uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000; |
| 141 | if (micro > 0) |
| 142 | { |
| 143 | ptime += boost::posix_time::microseconds(micro); |
| 144 | return boost::posix_time::to_iso_string(ptime); |
| 145 | } |
| 146 | else |
| 147 | return boost::posix_time::to_iso_string(ptime); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 151 | * \brief Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation |
| 152 | * to the internal time format |
| 153 | * |
| 154 | * Examples of accepted ISO strings: |
| 155 | * |
| 156 | * - with fractional nanoseconds: 20020131T100001,123456789 |
| 157 | * - with fractional microseconds: 20020131T100001,123456 |
| 158 | * - with fractional milliseconds: 20020131T100001,123 |
| 159 | * - without fractional seconds: 20020131T100001 |
| 160 | * |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 161 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 162 | inline system_clock::TimePoint |
| 163 | fromIsoString(const std::string& isoString) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 164 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 165 | static boost::posix_time::ptime posixTimeEpoch = boost::posix_time::from_time_t(0); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 167 | boost::posix_time::ptime ptime = boost::posix_time::from_iso_string(isoString); |
| 168 | |
| 169 | system_clock::TimePoint point = system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds()); |
| 170 | point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000); |
| 171 | return point; |
| 172 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 173 | |
| 174 | } // namespace time |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 175 | } // namespace ndn |
| 176 | |
| 177 | #endif // NDN_TIME_HPP |