blob: 9af95fcf211c3a98073b02755ee9b6a68cff6b48 [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 Afanasyev1e0a0772014-01-28 20:07:07 -080012#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080013
14namespace ndn {
15
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080016const boost::posix_time::ptime UNIX_EPOCH_TIME =
17 boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
Alexander Afanasyevd409d592014-01-28 18:36:38 -080018
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080019/**
20 * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
21 */
22inline MillisecondsSince1970
23getNowMilliseconds()
24{
25 return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
26}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080027
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080028inline MillisecondsSince1970
29ndn_getNowMilliseconds()
30{
31 return getNowMilliseconds();
32}
33
Alexander Afanasyevd409d592014-01-28 18:36:38 -080034
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080035/**
36 * Convert to the ISO string representation of the time.
37 * @param time Milliseconds since 1/1/1970.
38 * @return The ISO string.
39 */
40inline std::string
41toIsoString(const MillisecondsSince1970& time)
42{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080043 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 Afanasyeve2e3ca52014-01-03 13:59:07 -080050}
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 */
57inline MillisecondsSince1970
58fromIsoString(const std::string& isoString)
59{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080060 boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080061
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080062 return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080063}
64
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065namespace time {
66
67class monotonic_clock;
68
69/** \class Duration
70 * \brief represents a time interval
71 * Time unit is nanosecond.
72 */
73class Duration
74{
75public:
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
109private:
110 int64_t m_value;
111};
112
113/** \class Point
114 * \brief represents a point in time
115 * This uses monotonic clock.
116 */
117class Point
118{
119public:
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
159private:
160 int64_t m_value;
161};
162
Yingdi Yuf2a82092014-02-03 16:49:15 -0800163inline std::ostream&
164operator<<(std::ostream &os, const Duration& duration)
165{
166 os << static_cast<int64_t>(duration) / 1000000000.0 << " s";
167 return os;
168}
169
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800170/**
171 * \brief Get current time
172 * \return{ the current time in monotonic clock }
173 */
174Point
175now();
176
177/**
178 * \brief Get time::Duration for the specified number of seconds
179 */
180template<class T>
181inline Duration
182seconds(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 */
190template<class T>
191inline Duration
192milliseconds(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 */
200template<class T>
201inline Duration
202microseconds(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 */
210inline Duration
211nanoseconds(int64_t value)
212{
213 return Duration(value);
214}
215
216
217} // namespace time
218
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800219} // namespace ndn
220
221#endif // NDN_TIME_HPP