3namespace tinyrobotics {
6
7
8
9
10enum class TimeUnit { S, MS, US, MIN, HOUR };
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
35 Time(
float time,
TimeUnit unit) { setValue(time, unit); }
37 void setValue(
float newTime,
TimeUnit newUnit) {
42 float getValue(
TimeUnit desiredUnit)
const {
43 if (unit == desiredUnit)
return time;
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;
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;
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;
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;
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;
79 Time operator+(
const Time& other)
const {
80 float otherTime = other.getValue(unit);
81 return Time(time + otherTime, unit);
84 Time operator-(
const Time& other)
const {
85 float otherTime = other.getValue(unit);
86 return Time(time - otherTime, unit);
89 Time operator*(
float scalar)
const {
return Time(time * scalar, unit); }
91 Time operator/(
float scalar)
const {
92 if (scalar == 0)
return Time(0, unit);
93 return Time(time / scalar, unit);
96 bool operator==(
const Time& other)
const {
97 return time == other.getValue(unit);
100 bool operator!=(
const Time& other)
const {
return !(*
this == other); }
102 bool operator<(
const Time& other)
const {
103 return time < other.getValue(unit);
106 bool operator<=(
const Time& other)
const {
107 return time <= other.getValue(unit);
110 bool operator>(
const Time& other)
const {
111 return time > other.getValue(unit);
114 bool operator>=(
const Time& other)
const {
115 return time >= other.getValue(unit);
118 Time& operator+=(
const Time& other) {
119 time += other.getValue(unit);
123 Time& operator-=(
const Time& other) {
124 time -= other.getValue(unit);
128 Time& operator*=(
float scalar) {
133 Time& operator/=(
float scalar) {
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