roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "roo_flags.h"
6#include "roo_logging.h"
7
8/// For convenience conversion from roo_time::Duration.
9#include "roo_time.h"
10
11#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
12#include <string>
13#endif
14
15#if defined(ARDUINO)
16#include <Arduino.h>
17#endif
18
19namespace roo_quantity {
20
21/// Representation of elapsed, internally stored as floating-point seconds.
22class Time {
23 public:
24 /// Creates a time object representing an 'unknown' time.
25 Time() : time_(std::nanf("")) {}
26
27 Time(const roo_time::Duration& duration) : time_(duration.inSecondsFloat()) {}
28
29 /// Returns the time in seconds.
30 float inSeconds() const { return time_; }
31
32 /// Returns the time in milliseconds.
33 float inMilliseconds() const { return time_ * 1000.0f; }
34
35 /// Returns the time in microseconds.
36 float inMicroseconds() const { return time_ * 1000000.0f; }
37
38 /// Returns the time in nanoseconds.
39 float inNanoseconds() const { return time_ * 1000000000.0f; }
40
41 /// Returns whether the object represents an unknown time.
42 bool isUnknown() const { return std::isnan(time_); }
43
44 bool operator<(const Time& other) const { return time_ < other.time_; }
45
46 bool operator==(const Time& other) const { return time_ == other.time_; }
47
48 bool operator>(const Time& other) const { return other.time_ < time_; }
49
50 bool operator<=(const Time& other) const { return !(other.time_ < time_); }
51
52 bool operator>=(const Time& other) const { return !(time_ < other.time_); }
53
54 bool operator!=(const Time& other) const { return !(time_ == other.time_); }
55
56 inline Time& operator+=(const Time& other) {
57 time_ += other.inSeconds();
58 return *this;
59 }
60
61 inline Time& operator-=(const Time& other) {
62 time_ -= other.inSeconds();
63 return *this;
64 }
65
66 inline Time& operator*=(float multi) {
67 time_ *= multi;
68 return *this;
69 }
70
71 inline Time& operator/=(float div) {
72 time_ /= div;
73 return *this;
74 }
75
76#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
77 /// Returns the string representation of the time.
78 std::string asString() const;
79#endif
80
81#if defined(ARDUINO)
82 String asArduinoString() const;
83#endif
84
85 private:
86 friend Time UnknownTime();
87
88 friend Time TimeInSeconds(float);
89
90 explicit Time(float time) : time_(time) {}
91
92 /// Stored in seconds.
93 float time_;
94};
95
96inline Time TimeInSeconds(float time);
97
98/// Returns a time object representing an unknown time.
99inline Time UnknownTime() { return Time(); }
100
101/// Returns a time object equivalent to the specified time
102/// expressed in minutes (60 seconds).
103inline Time TimeInMinutes(float time) { return TimeInSeconds(time * 60.0f); }
104
105/// Returns a time object equivalent to the specified time
106/// expressed in hours (3600 seconds).
107inline Time TimeInHours(float time) { return TimeInSeconds(time * 3600.0f); }
108
109/// Returns a time object equivalent to the specified time
110/// expressed in seconds.
111inline Time TimeInSeconds(float time) { return Time(time); }
112
113/// Returns a time object equivalent to the specified time
114/// expressed in milliseconds.
115inline Time TimeInMilliseconds(float time) {
116 return TimeInSeconds(time * 0.001f);
117}
118
119/// Returns a time object equivalent to the specified time
120/// expressed in microseconds.
121inline Time TimeInMicroseconds(float time) {
122 return TimeInSeconds(time * 0.000001f);
123}
124
125/// Returns a time object equivalent to the specified time
126/// expressed in nanoseconds.
127inline Time TimeInNanoseconds(float time) {
128 return TimeInSeconds(time * 0.000000001f);
129}
130
131inline Time operator+(Time a, Time b) {
132 return TimeInSeconds(a.inSeconds() + b.inSeconds());
133}
134
135inline Time operator-(Time a, Time b) {
136 return TimeInSeconds(a.inSeconds() - b.inSeconds());
137}
138
139inline Time operator-(Time a) { return TimeInSeconds(-a.inSeconds()); }
140
141inline Time operator*(Time a, float b) {
142 return TimeInSeconds(a.inSeconds() * b);
143}
144
145inline Time operator*(float a, Time b) {
146 return TimeInSeconds(a * b.inSeconds());
147}
148
149inline Time operator/(Time a, float b) {
150 return TimeInSeconds(a.inSeconds() / b);
151}
152
153inline float operator/(Time a, Time b) { return a.inSeconds() / b.inSeconds(); }
154
155roo_logging::Stream& operator<<(roo_logging::Stream& os, const Time& val);
156
157} // namespace roo_quantity
Representation of elapsed, internally stored as floating-point seconds.
Definition time.h:22
Time & operator/=(float div)
Definition time.h:71
bool operator<=(const Time &other) const
Definition time.h:50
float inMicroseconds() const
Returns the time in microseconds.
Definition time.h:36
Time()
Creates a time object representing an 'unknown' time.
Definition time.h:25
bool operator==(const Time &other) const
Definition time.h:46
bool isUnknown() const
Returns whether the object represents an unknown time.
Definition time.h:42
Time(const roo_time::Duration &duration)
Definition time.h:27
Time & operator*=(float multi)
Definition time.h:66
float inMilliseconds() const
Returns the time in milliseconds.
Definition time.h:33
float inSeconds() const
Returns the time in seconds.
Definition time.h:30
bool operator>=(const Time &other) const
Definition time.h:52
friend Time TimeInSeconds(float)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111
Time & operator-=(const Time &other)
Definition time.h:61
bool operator!=(const Time &other) const
Definition time.h:54
friend Time UnknownTime()
Returns a time object representing an unknown time.
Definition time.h:99
float inNanoseconds() const
Returns the time in nanoseconds.
Definition time.h:39
Time & operator+=(const Time &other)
Definition time.h:56
bool operator>(const Time &other) const
Definition time.h:48
bool operator<(const Time &other) const
Definition time.h:44
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Time TimeInHours(float time)
Returns a time object equivalent to the specified time expressed in hours (3600 seconds).
Definition time.h:107
Time TimeInNanoseconds(float time)
Returns a time object equivalent to the specified time expressed in nanoseconds.
Definition time.h:127
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Time TimeInMilliseconds(float time)
Returns a time object equivalent to the specified time expressed in milliseconds.
Definition time.h:115
Time TimeInMicroseconds(float time)
Returns a time object equivalent to the specified time expressed in microseconds.
Definition time.h:121
Area operator+(Area a, Area b)
Definition area.h:171
Time TimeInSeconds(float time)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111
Area operator-(Area a, Area b)
Definition area.h:175
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183
Time UnknownTime()
Returns a time object representing an unknown time.
Definition time.h:99
Time TimeInMinutes(float time)
Returns a time object equivalent to the specified time expressed in minutes (60 seconds).
Definition time.h:103