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