blob: e0b3727584a25196f3de45c7c8622223f792207e [file] [log] [blame]
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -08001/**
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 Afanasyev19508852014-01-29 01:01:51 -080011#include "../common.hpp"
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080012
13namespace ndn {
14
Alexander Afanasyev809805d2014-02-17 17:20:33 -080015/**
16 * A time interval represented as the number of milliseconds.
17 */
18typedef int64_t Milliseconds;
19
20/**
21 * The calendar time represented as the number of milliseconds since 1/1/1970.
22 */
23typedef int64_t MillisecondsSince1970;
24
25
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080026const boost::posix_time::ptime UNIX_EPOCH_TIME =
27 boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
Alexander Afanasyevd409d592014-01-28 18:36:38 -080028
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080029/**
30 * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
31 */
32inline MillisecondsSince1970
33getNowMilliseconds()
34{
35 return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
36}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080037
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080038inline MillisecondsSince1970
39ndn_getNowMilliseconds()
40{
41 return getNowMilliseconds();
42}
43
Alexander Afanasyev809805d2014-02-17 17:20:33 -080044inline MillisecondsSince1970
45getNow()
46{
47 return getNowMilliseconds();
48}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080049
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080050/**
51 * Convert to the ISO string representation of the time.
52 * @param time Milliseconds since 1/1/1970.
53 * @return The ISO string.
54 */
55inline std::string
56toIsoString(const MillisecondsSince1970& time)
57{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080058 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 Afanasyeve2e3ca52014-01-03 13:59:07 -080065}
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 */
72inline MillisecondsSince1970
73fromIsoString(const std::string& isoString)
74{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080075 boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080076
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080077 return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080078}
79
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080namespace time {
81
82class monotonic_clock;
83
84/** \class Duration
85 * \brief represents a time interval
86 * Time unit is nanosecond.
87 */
88class Duration
89{
90public:
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
124private:
125 int64_t m_value;
126};
127
128/** \class Point
129 * \brief represents a point in time
130 * This uses monotonic clock.
131 */
132class Point
133{
134public:
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
174private:
175 int64_t m_value;
176};
177
Yingdi Yuf2a82092014-02-03 16:49:15 -0800178inline std::ostream&
179operator<<(std::ostream &os, const Duration& duration)
180{
181 os << static_cast<int64_t>(duration) / 1000000000.0 << " s";
182 return os;
183}
184
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800185/**
186 * \brief Get current time
187 * \return{ the current time in monotonic clock }
188 */
189Point
190now();
191
192/**
193 * \brief Get time::Duration for the specified number of seconds
194 */
195template<class T>
196inline Duration
197seconds(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 */
205template<class T>
206inline Duration
207milliseconds(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 */
215template<class T>
216inline Duration
217microseconds(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 */
225inline Duration
226nanoseconds(int64_t value)
227{
228 return Duration(value);
229}
230
231
232} // namespace time
233
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800234} // namespace ndn
235
236#endif // NDN_TIME_HPP