blob: d17596495ccf0cb9e9eb2a8e98ec2228c19a96b7 [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
163/**
164 * \brief Get current time
165 * \return{ the current time in monotonic clock }
166 */
167Point
168now();
169
170/**
171 * \brief Get time::Duration for the specified number of seconds
172 */
173template<class T>
174inline Duration
175seconds(T value)
176{
177 return Duration(value * static_cast<int64_t>(1000000000));
178}
179
180/**
181 * \brief Get time::Duration for the specified number of milliseconds
182 */
183template<class T>
184inline Duration
185milliseconds(T value)
186{
187 return Duration(value * static_cast<int64_t>(1000000));
188}
189
190/**
191 * \brief Get time::Duration for the specified number of microseconds
192 */
193template<class T>
194inline Duration
195microseconds(T value)
196{
197 return Duration(value * static_cast<int64_t>(1000));
198}
199
200/**
201 * \brief Get time::Duration for the specified number of nanoseconds
202 */
203inline Duration
204nanoseconds(int64_t value)
205{
206 return Duration(value);
207}
208
209
210} // namespace time
211
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800212} // namespace ndn
213
214#endif // NDN_TIME_HPP