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