blob: 5d729d915a1a4b29a4ea8a036774cbaeb39541e3 [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 Afanasyev1e0a0772014-01-28 20:07:07 -080015const boost::posix_time::ptime UNIX_EPOCH_TIME =
16 boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
Alexander Afanasyevd409d592014-01-28 18:36:38 -080017
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080018/**
19 * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
20 */
21inline MillisecondsSince1970
22getNowMilliseconds()
23{
24 return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
25}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080026
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080027inline MillisecondsSince1970
28ndn_getNowMilliseconds()
29{
30 return getNowMilliseconds();
31}
32
Alexander Afanasyevd409d592014-01-28 18:36:38 -080033
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080034/**
35 * Convert to the ISO string representation of the time.
36 * @param time Milliseconds since 1/1/1970.
37 * @return The ISO string.
38 */
39inline std::string
40toIsoString(const MillisecondsSince1970& time)
41{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080042 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 Afanasyeve2e3ca52014-01-03 13:59:07 -080049}
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 */
56inline MillisecondsSince1970
57fromIsoString(const std::string& isoString)
58{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080059 boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080060
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080061 return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080062}
63
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064namespace time {
65
66class monotonic_clock;
67
68/** \class Duration
69 * \brief represents a time interval
70 * Time unit is nanosecond.
71 */
72class Duration
73{
74public:
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
108private:
109 int64_t m_value;
110};
111
112/** \class Point
113 * \brief represents a point in time
114 * This uses monotonic clock.
115 */
116class Point
117{
118public:
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
158private:
159 int64_t m_value;
160};
161
Yingdi Yuf2a82092014-02-03 16:49:15 -0800162inline std::ostream&
163operator<<(std::ostream &os, const Duration& duration)
164{
165 os << static_cast<int64_t>(duration) / 1000000000.0 << " s";
166 return os;
167}
168
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800169/**
170 * \brief Get current time
171 * \return{ the current time in monotonic clock }
172 */
173Point
174now();
175
176/**
177 * \brief Get time::Duration for the specified number of seconds
178 */
179template<class T>
180inline Duration
181seconds(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 */
189template<class T>
190inline Duration
191milliseconds(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 */
199template<class T>
200inline Duration
201microseconds(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 */
209inline Duration
210nanoseconds(int64_t value)
211{
212 return Duration(value);
213}
214
215
216} // namespace time
217
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800218} // namespace ndn
219
220#endif // NDN_TIME_HPP