blob: 276cf1cabc2233706603ac550e673fbfd2619b81 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_TIME_H_
2#define AOS_COMMON_TIME_H_
3
4#include <stdint.h>
5#include <time.h>
6
7#ifndef __VXWORKS__
8#include <type_traits>
brians57dd5822013-02-27 21:44:15 +00009#include <sys/time.h>
brians343bc112013-02-10 01:53:46 +000010#else
11#include <sysLib.h>
brians57dd5822013-02-27 21:44:15 +000012#include <sys/times.h>
brians343bc112013-02-10 01:53:46 +000013#endif
14#include <ostream>
15
16#include "aos/aos_stdint.h"
17#include "aos/common/type_traits.h"
18
19namespace aos {
20namespace time {
21
22// A nice structure for representing times.
23// 0 <= nsec_ < kNSecInSec should always be true. All functions here will make
24// sure that that is true if it was on all inputs (including *this).
25//
26// The arithmetic and comparison operators are overloaded because they make
27// complete sense and are very useful. The default copy and assignment stuff is
28// left because it works fine. Multiplication and division of Times by Times are
29// not implemented because I can't think of any uses for them and there are
30// multiple ways to do it.
31struct Time {
32 public:
33 static const int32_t kNSecInSec = 1000000000;
34 static const int32_t kNSecInMSec = 1000000;
35 static const int32_t kNSecInUSec = 1000;
36 static const int32_t kMSecInSec = 1000;
37 static const int32_t kUSecInSec = 1000000;
38 Time(int32_t sec, int32_t nsec) : sec_(sec), nsec_(nsec) {
39 Check();
40 }
41 #ifndef SWIG
42 explicit Time(const struct timespec &value)
43 : sec_(value.tv_sec), nsec_(value.tv_nsec) {
44 Check();
45 }
46 struct timespec ToTimespec() const {
47 struct timespec ans;
48 ans.tv_sec = sec_;
49 ans.tv_nsec = nsec_;
50 return ans;
51 }
brians57dd5822013-02-27 21:44:15 +000052 explicit Time(const struct timeval &value)
53 : sec_(value.tv_sec), nsec_(value.tv_usec * kNSecInUSec) {
54 Check();
55 }
56 struct timeval ToTimeval() const {
57 struct timeval ans;
58 ans.tv_sec = sec_;
59 ans.tv_usec = nsec_ / kNSecInUSec;
60 return ans;
61 }
brians343bc112013-02-10 01:53:46 +000062 #endif // SWIG
Brian Silverman3204dd82013-03-12 18:42:01 -070063
brians343bc112013-02-10 01:53:46 +000064 // CLOCK_MONOTONIC on the fitpc and CLOCK_REALTIME on the cRIO because the
65 // cRIO doesn't have any others.
66 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
67 // change when somebody changes the wall clock (like the ntp deamon or
68 // whatever). See clock_gettime(2) for details.
69 //
70 // This is the clock that code that just wants to sleep for a certain amount
71 // of time or measure how long something takes should use.
72 #ifndef __VXWORKS__
73 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
74 #else
75 static const clockid_t kDefaultClock = CLOCK_REALTIME;
76 #endif
77 // Creates a Time representing the current value of the specified clock or
78 // dies.
79 static Time Now(clockid_t clock = kDefaultClock);
80
81 // Constructs a Time representing seconds.
Brian Silverman7645d2f2013-03-30 18:53:56 -070082 // TODO(brians): fix and test the negative cases for all of these
brians343bc112013-02-10 01:53:46 +000083 static Time InSeconds(double seconds) {
Brian Silverman7645d2f2013-03-30 18:53:56 -070084 if (seconds < 0.0) {
85 return Time(static_cast<int32_t>(seconds) - 1,
86 (seconds - static_cast<int32_t>(seconds) + 1.0) * kNSecInSec);
87 } else {
88 return Time(static_cast<int32_t>(seconds),
89 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
90 }
brians343bc112013-02-10 01:53:46 +000091 }
92
93 // Constructs a time representing microseconds.
94 static Time InNS(int64_t nseconds) {
95 return Time(nseconds / static_cast<int64_t>(kNSecInSec),
96 nseconds % kNSecInSec);
97 }
98
99 // Constructs a time representing microseconds.
100 static Time InUS(int useconds) {
Brian Silverman7645d2f2013-03-30 18:53:56 -0700101 if (useconds < 0) {
102 return Time(useconds / kUSecInSec - 1,
103 (useconds % kUSecInSec) * kNSecInUSec + kNSecInSec);
104 } else {
105 return Time(useconds / kUSecInSec, (useconds % kUSecInSec) * kNSecInUSec);
106 }
brians343bc112013-02-10 01:53:46 +0000107 }
108
109 // Constructs a time representing mseconds.
110 static Time InMS(int mseconds) {
111 return Time(mseconds / kMSecInSec, (mseconds % kMSecInSec) * kNSecInMSec);
112 }
113
114 // Checks whether or not this time is within amount nanoseconds of other.
115 bool IsWithin(const Time &other, int64_t amount) const;
116
117 // Returns the time represented all in nanoseconds.
118 int64_t ToNSec() const {
119 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
120 static_cast<int64_t>(nsec_);
121 }
122#ifdef __VXWORKS__
123 // Returns the time represented all in system clock ticks. The system clock
124 // rate is retrieved using sysClkRateGet().
125 int ToTicks() const {
126 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
127 }
Brian Silverman3204dd82013-03-12 18:42:01 -0700128 // Constructs a Time representing ticks.
129 static Time InTicks(int ticks) {
Brian Silvermanffe3d712013-03-15 21:35:59 -0700130 return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet());
Brian Silverman3204dd82013-03-12 18:42:01 -0700131 }
brians343bc112013-02-10 01:53:46 +0000132#endif
133
134 // Returns the time represented in milliseconds.
135 int64_t ToMSec() const {
136 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
137 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
138 }
139
Brian Silverman7645d2f2013-03-30 18:53:56 -0700140 // Returns the time represent in microseconds.
141 // TODO(brians): test this
142 int64_t ToUSec() const {
143 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) +
144 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec));
145 }
146
briansf0165ca2013-03-02 06:17:47 +0000147 // Returns the time represented in fractional seconds.
148 double ToSeconds() const {
149 return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec;
150 }
151
brians343bc112013-02-10 01:53:46 +0000152 #ifndef SWIG
153 Time &operator+=(const Time &rhs);
154 Time &operator-=(const Time &rhs);
155 Time &operator*=(int32_t rhs);
156 Time &operator/=(int32_t rhs);
157 Time &operator%=(int32_t rhs);
158 #endif
159 const Time operator+(const Time &rhs) const;
160 const Time operator-(const Time &rhs) const;
161 const Time operator*(int32_t rhs) const;
162 const Time operator/(int32_t rhs) const;
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700163 // TODO(brians) test this
164 double operator/(const Time &rhs) const;
brians343bc112013-02-10 01:53:46 +0000165 const Time operator%(int32_t rhs) const;
166
167 bool operator==(const Time &rhs) const;
168 bool operator!=(const Time &rhs) const;
169 bool operator<(const Time &rhs) const;
170 bool operator>(const Time &rhs) const;
171 bool operator<=(const Time &rhs) const;
172 bool operator>=(const Time &rhs) const;
173
174 #ifndef SWIG
175 // For gtest etc.
176 friend std::ostream &operator<<(std::ostream &os, const Time &time);
177 #endif // SWIG
178
179 int32_t sec() const { return sec_; }
180 void set_sec(int32_t sec) { sec_ = sec; }
181 int32_t nsec() const { return nsec_; }
182 void set_nsec(int32_t nsec) {
183 nsec_ = nsec;
184 Check();
185 }
186
Brian Silverman3204dd82013-03-12 18:42:01 -0700187 // Absolute value.
188 Time abs() const {
189 if (*this > Time(0, 0)) return *this;
Brian Silvermanf3cbebd2013-03-19 16:53:18 -0700190 if (nsec_ == 0) return Time(-sec_, 0);
Brian Silverman3204dd82013-03-12 18:42:01 -0700191 return Time(-sec_ - 1, kNSecInSec - nsec_);
192 }
193
194 // Enables returning the mock time value for Now instead of checking the
195 // system clock. This should only be used when testing things depending on
196 // time, or many things may/will break.
197 static void EnableMockTime(const Time now);
198 // Sets now when time is being mocked.
199 static void SetMockTime(const Time now);
200 // Convenience function to just increment the mock time by a certain amount.
201 static void IncrementMockTime(const Time amount) {
Brian Silvermanf3cbebd2013-03-19 16:53:18 -0700202 // TODO(brians) make this thread safe so it's more useful?
Brian Silverman3204dd82013-03-12 18:42:01 -0700203 SetMockTime(Now() + amount);
204 }
205 // Disables mocking time.
206 static void DisableMockTime();
207
brians343bc112013-02-10 01:53:46 +0000208 private:
209 int32_t sec_, nsec_;
210 // LOG(FATAL)s if nsec_ is >= kNSecInSec.
211 void Check();
212};
213
214// Sleeps for the amount of time represented by time counted by clock.
215void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
216// Sleeps until clock is at the time represented by time.
217void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
218
219} // namespace time
220} // namespace aos
221
222#endif // AOS_COMMON_TIME_H_