blob: 1d7ccae71fa654ee5539b8b6a41f123b576bea4a [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
63 // CLOCK_MONOTONIC on the fitpc and CLOCK_REALTIME on the cRIO because the
64 // cRIO doesn't have any others.
65 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
66 // change when somebody changes the wall clock (like the ntp deamon or
67 // whatever). See clock_gettime(2) for details.
68 //
69 // This is the clock that code that just wants to sleep for a certain amount
70 // of time or measure how long something takes should use.
71 #ifndef __VXWORKS__
72 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
73 #else
74 static const clockid_t kDefaultClock = CLOCK_REALTIME;
75 #endif
76 // Creates a Time representing the current value of the specified clock or
77 // dies.
78 static Time Now(clockid_t clock = kDefaultClock);
79
80 // Constructs a Time representing seconds.
81 static Time InSeconds(double seconds) {
82 return Time(static_cast<int32_t>(seconds),
83 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
84 }
85
86 // Constructs a time representing microseconds.
87 static Time InNS(int64_t nseconds) {
88 return Time(nseconds / static_cast<int64_t>(kNSecInSec),
89 nseconds % kNSecInSec);
90 }
91
92 // Constructs a time representing microseconds.
93 static Time InUS(int useconds) {
94 return Time(useconds / kUSecInSec, (useconds % kUSecInSec) * kNSecInUSec);
95 }
96
97 // Constructs a time representing mseconds.
98 static Time InMS(int mseconds) {
99 return Time(mseconds / kMSecInSec, (mseconds % kMSecInSec) * kNSecInMSec);
100 }
101
102 // Checks whether or not this time is within amount nanoseconds of other.
103 bool IsWithin(const Time &other, int64_t amount) const;
104
105 // Returns the time represented all in nanoseconds.
106 int64_t ToNSec() const {
107 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
108 static_cast<int64_t>(nsec_);
109 }
110#ifdef __VXWORKS__
111 // Returns the time represented all in system clock ticks. The system clock
112 // rate is retrieved using sysClkRateGet().
113 int ToTicks() const {
114 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
115 }
116#endif
117
118 // Returns the time represented in milliseconds.
119 int64_t ToMSec() const {
120 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
121 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
122 }
123
briansf0165ca2013-03-02 06:17:47 +0000124 // Returns the time represented in fractional seconds.
125 double ToSeconds() const {
126 return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec;
127 }
128
brians343bc112013-02-10 01:53:46 +0000129 #ifndef SWIG
130 Time &operator+=(const Time &rhs);
131 Time &operator-=(const Time &rhs);
132 Time &operator*=(int32_t rhs);
133 Time &operator/=(int32_t rhs);
134 Time &operator%=(int32_t rhs);
135 #endif
136 const Time operator+(const Time &rhs) const;
137 const Time operator-(const Time &rhs) const;
138 const Time operator*(int32_t rhs) const;
139 const Time operator/(int32_t rhs) const;
140 const Time operator%(int32_t rhs) const;
141
142 bool operator==(const Time &rhs) const;
143 bool operator!=(const Time &rhs) const;
144 bool operator<(const Time &rhs) const;
145 bool operator>(const Time &rhs) const;
146 bool operator<=(const Time &rhs) const;
147 bool operator>=(const Time &rhs) const;
148
149 #ifndef SWIG
150 // For gtest etc.
151 friend std::ostream &operator<<(std::ostream &os, const Time &time);
152 #endif // SWIG
153
154 int32_t sec() const { return sec_; }
155 void set_sec(int32_t sec) { sec_ = sec; }
156 int32_t nsec() const { return nsec_; }
157 void set_nsec(int32_t nsec) {
158 nsec_ = nsec;
159 Check();
160 }
161
162 private:
163 int32_t sec_, nsec_;
164 // LOG(FATAL)s if nsec_ is >= kNSecInSec.
165 void Check();
166};
167
168// Sleeps for the amount of time represented by time counted by clock.
169void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
170// Sleeps until clock is at the time represented by time.
171void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
172
173} // namespace time
174} // namespace aos
175
176#endif // AOS_COMMON_TIME_H_