TinyRobotics
Loading...
Searching...
No Matches
Time.h
1#pragma once
2
3namespace tinyrobotics {
4
5/**
6 * @enum TimeUnit
7 * @ingroup units
8 * @brief Supported time units for conversion and representation.
9 */
10enum class TimeUnit { S, MS, US, MIN, HOUR };
11
12/**
13 * @class Time
14 * @ingroup units
15 * @brief Represents a time duration with a specific unit (seconds,
16 * milliseconds, microseconds, minutes, or hours).
17 *
18 * Provides methods to set and get the time in different units, as well as
19 * handle unit conversion when retrieving the time in a desired unit. The
20 * internal representation of the time is stored in the unit specified at
21 * construction, and all operations ensure that the time can be accurately
22 * converted between supported units.
23 *
24 * This class can be used for various applications such as navigation, robotics,
25 * or any scenario that requires time representation and manipulation. The
26 * getValue method allows for easy retrieval of the time in the desired unit,
27 * while the setValue method allows for updating the time measurement with a new
28 * value and unit. The class is designed to be simple and efficient for use in
29 * embedded systems, with basic unit conversion logic that covers common time
30 * units.
31 */
32class Time {
33 public:
34 Time() = default;
35 Time(float time, TimeUnit unit) { setValue(time, unit); }
36
37 void setValue(float newTime, TimeUnit newUnit) {
38 time = newTime;
39 unit = newUnit;
40 }
41
42 float getValue(TimeUnit desiredUnit) const {
43 if (unit == desiredUnit) return time;
44 switch (unit) {
45 case TimeUnit::S:
46 if (desiredUnit == TimeUnit::MS) return time * 1000;
47 if (desiredUnit == TimeUnit::US) return time * 1000000;
48 if (desiredUnit == TimeUnit::MIN) return time / 60;
49 if (desiredUnit == TimeUnit::HOUR) return time / 3600;
50 break;
51 case TimeUnit::MS:
52 if (desiredUnit == TimeUnit::S) return time / 1000;
53 if (desiredUnit == TimeUnit::US) return time * 1000;
54 if (desiredUnit == TimeUnit::MIN) return time / 60000;
55 if (desiredUnit == TimeUnit::HOUR) return time / 3600000;
56 break;
57 case TimeUnit::US:
58 if (desiredUnit == TimeUnit::S) return time / 1000000;
59 if (desiredUnit == TimeUnit::MS) return time / 1000;
60 if (desiredUnit == TimeUnit::MIN) return time / 60000000;
61 if (desiredUnit == TimeUnit::HOUR) return time / 3600000000;
62 break;
63 case TimeUnit::MIN:
64 if (desiredUnit == TimeUnit::S) return time * 60;
65 if (desiredUnit == TimeUnit::MS) return time * 60000;
66 if (desiredUnit == TimeUnit::US) return time * 60000000;
67 if (desiredUnit == TimeUnit::HOUR) return time / 60;
68 break;
69 case TimeUnit::HOUR:
70 if (desiredUnit == TimeUnit::S) return time * 3600;
71 if (desiredUnit == TimeUnit::MS) return time * 3600000;
72 if (desiredUnit == TimeUnit::US) return time * 3600000000;
73 if (desiredUnit == TimeUnit::MIN) return time * 60;
74 break;
75 }
76 return -1; // Invalid conversion
77 }
78
79 Time operator+(const Time& other) const {
80 float otherTime = other.getValue(unit);
81 return Time(time + otherTime, unit);
82 }
83
84 Time operator-(const Time& other) const {
85 float otherTime = other.getValue(unit);
86 return Time(time - otherTime, unit);
87 }
88
89 Time operator*(float scalar) const { return Time(time * scalar, unit); }
90
91 Time operator/(float scalar) const {
92 if (scalar == 0) return Time(0, unit);
93 return Time(time / scalar, unit);
94 }
95
96 bool operator==(const Time& other) const {
97 return time == other.getValue(unit);
98 }
99
100 bool operator!=(const Time& other) const { return !(*this == other); }
101
102 bool operator<(const Time& other) const {
103 return time < other.getValue(unit);
104 }
105
106 bool operator<=(const Time& other) const {
107 return time <= other.getValue(unit);
108 }
109
110 bool operator>(const Time& other) const {
111 return time > other.getValue(unit);
112 }
113
114 bool operator>=(const Time& other) const {
115 return time >= other.getValue(unit);
116 }
117
118 Time& operator+=(const Time& other) {
119 time += other.getValue(unit);
120 return *this;
121 }
122
123 Time& operator-=(const Time& other) {
124 time -= other.getValue(unit);
125 return *this;
126 }
127
128 Time& operator*=(float scalar) {
129 time *= scalar;
130 return *this;
131 }
132
133 Time& operator/=(float scalar) {
134 if (scalar == 0) {
135 time = 0;
136 } else {
137 time /= scalar;
138 }
139 return *this;
140 }
141
142 protected:
143 float time = 0.0f;
144 TimeUnit unit = TimeUnit::MS;
145};
146
147} // namespace tinyrobotics
Represents a time duration with a specific unit (seconds, milliseconds, microseconds,...
Definition: Time.h:32
TimeUnit
Supported time units for conversion and representation.
Definition: Time.h:10