Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_TIME_HPP |
| 9 | #define NDN_TIME_HPP |
| 10 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 11 | #include "../common.hpp" |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
| 14 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 15 | /** |
| 16 | * A time interval represented as the number of milliseconds. |
| 17 | */ |
| 18 | typedef int64_t Milliseconds; |
| 19 | |
| 20 | /** |
| 21 | * The calendar time represented as the number of milliseconds since 1/1/1970. |
| 22 | */ |
| 23 | typedef int64_t MillisecondsSince1970; |
| 24 | |
| 25 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 26 | const boost::posix_time::ptime UNIX_EPOCH_TIME = |
| 27 | boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 29 | /** |
| 30 | * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond |
| 31 | */ |
| 32 | inline MillisecondsSince1970 |
| 33 | getNowMilliseconds() |
| 34 | { |
| 35 | return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds(); |
| 36 | } |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 38 | inline MillisecondsSince1970 |
| 39 | ndn_getNowMilliseconds() |
| 40 | { |
| 41 | return getNowMilliseconds(); |
| 42 | } |
| 43 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 44 | inline MillisecondsSince1970 |
| 45 | getNow() |
| 46 | { |
| 47 | return getNowMilliseconds(); |
| 48 | } |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Convert to the ISO string representation of the time. |
| 52 | * @param time Milliseconds since 1/1/1970. |
| 53 | * @return The ISO string. |
| 54 | */ |
| 55 | inline std::string |
| 56 | toIsoString(const MillisecondsSince1970& time) |
| 57 | { |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 58 | boost::posix_time::ptime boostTime = UNIX_EPOCH_TIME + boost::posix_time::milliseconds(time); |
| 59 | |
| 60 | /// @todo Determine whether this is necessary at all |
| 61 | if ((time % 1000) == 0) |
| 62 | return boost::posix_time::to_iso_string(boostTime) + ".000000"; |
| 63 | else |
| 64 | return boost::posix_time::to_iso_string(boostTime); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Convert from the ISO string representation to the internal time format. |
| 69 | * @param isoString The ISO time formatted string. |
| 70 | * @return The time in milliseconds since 1/1/1970. |
| 71 | */ |
| 72 | inline MillisecondsSince1970 |
| 73 | fromIsoString(const std::string& isoString) |
| 74 | { |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 75 | boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 77 | return (boostTime-UNIX_EPOCH_TIME).total_milliseconds(); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | namespace time { |
| 81 | |
| 82 | class monotonic_clock; |
| 83 | |
| 84 | /** \class Duration |
| 85 | * \brief represents a time interval |
| 86 | * Time unit is nanosecond. |
| 87 | */ |
| 88 | class Duration |
| 89 | { |
| 90 | public: |
| 91 | Duration() |
| 92 | : m_value(0) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | explicit |
| 97 | Duration(int64_t value) |
| 98 | : m_value(value) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | operator int64_t&() |
| 103 | { |
| 104 | return m_value; |
| 105 | } |
| 106 | |
| 107 | operator const int64_t&() const |
| 108 | { |
| 109 | return m_value; |
| 110 | } |
| 111 | |
| 112 | Duration |
| 113 | operator+(const Duration& other) const |
| 114 | { |
| 115 | return Duration(this->m_value + other.m_value); |
| 116 | } |
| 117 | |
| 118 | Duration |
| 119 | operator-(const Duration& other) const |
| 120 | { |
| 121 | return Duration(this->m_value - other.m_value); |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | int64_t m_value; |
| 126 | }; |
| 127 | |
| 128 | /** \class Point |
| 129 | * \brief represents a point in time |
| 130 | * This uses monotonic clock. |
| 131 | */ |
| 132 | class Point |
| 133 | { |
| 134 | public: |
| 135 | Point() |
| 136 | : m_value(0) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | explicit |
| 141 | Point(int64_t value) |
| 142 | : m_value(value) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | operator int64_t&() |
| 147 | { |
| 148 | return m_value; |
| 149 | } |
| 150 | |
| 151 | operator const int64_t&() const |
| 152 | { |
| 153 | return m_value; |
| 154 | } |
| 155 | |
| 156 | Point |
| 157 | operator+(const Duration& other) const |
| 158 | { |
| 159 | return Point(this->m_value + static_cast<int64_t>(other)); |
| 160 | } |
| 161 | |
| 162 | Duration |
| 163 | operator-(const Point& other) const |
| 164 | { |
| 165 | return Duration(this->m_value - other.m_value); |
| 166 | } |
| 167 | |
| 168 | Point |
| 169 | operator-(const Duration& other) const |
| 170 | { |
| 171 | return Point(this->m_value - static_cast<int64_t>(other)); |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | int64_t m_value; |
| 176 | }; |
| 177 | |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 178 | inline std::ostream& |
| 179 | operator<<(std::ostream &os, const Duration& duration) |
| 180 | { |
| 181 | os << static_cast<int64_t>(duration) / 1000000000.0 << " s"; |
| 182 | return os; |
| 183 | } |
| 184 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 185 | /** |
| 186 | * \brief Get current time |
| 187 | * \return{ the current time in monotonic clock } |
| 188 | */ |
| 189 | Point |
| 190 | now(); |
| 191 | |
| 192 | /** |
| 193 | * \brief Get time::Duration for the specified number of seconds |
| 194 | */ |
| 195 | template<class T> |
| 196 | inline Duration |
| 197 | seconds(T value) |
| 198 | { |
| 199 | return Duration(value * static_cast<int64_t>(1000000000)); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * \brief Get time::Duration for the specified number of milliseconds |
| 204 | */ |
| 205 | template<class T> |
| 206 | inline Duration |
| 207 | milliseconds(T value) |
| 208 | { |
| 209 | return Duration(value * static_cast<int64_t>(1000000)); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * \brief Get time::Duration for the specified number of microseconds |
| 214 | */ |
| 215 | template<class T> |
| 216 | inline Duration |
| 217 | microseconds(T value) |
| 218 | { |
| 219 | return Duration(value * static_cast<int64_t>(1000)); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * \brief Get time::Duration for the specified number of nanoseconds |
| 224 | */ |
| 225 | inline Duration |
| 226 | nanoseconds(int64_t value) |
| 227 | { |
| 228 | return Duration(value); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | } // namespace time |
| 233 | |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 234 | } // namespace ndn |
| 235 | |
| 236 | #endif // NDN_TIME_HPP |