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