roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1#include "roo_quantity/time.h"
2
3namespace roo_quantity {
4namespace {
5
6void TimeToString(Time val, char* out, int maxlen) {
7 if (val.isUnknown()) {
8 strncpy(out, "? s", maxlen);
9 } else {
10 const char* format;
11 const char* sign = "";
12 float num;
13 if (val.inSeconds() == 0.0f) {
14 snprintf(out, maxlen, "0 s");
15 return;
16 }
17 if (val.inSeconds() < 0) {
18 sign = "-";
19 val = -val;
20 }
21 if (val >= TimeInSeconds(1.0f)) {
22 format = "%s%g s";
23 num = val.inSeconds();
24 } else if (val >= TimeInMilliseconds(1.0f)) {
25 format = "%s%g ms";
26 num = val.inMilliseconds();
27 } else if (val >= TimeInMicroseconds(1.0f)) {
28 format = "%s%g µs";
29 num = val.inMicroseconds();
30 } else {
31 format = "%s%g ns";
32 num = val.inNanoseconds();
33 }
34 snprintf(out, maxlen, format, sign, num);
35 }
36}
37
38} // namespace
39
40#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
41
42std::string Time::asString() const {
43 char out[16];
44 TimeToString(*this, out, 16);
45 return out;
46}
47
48#endif
49
50#if defined(ARDUINO)
51
52String Time::asArduinoString() const {
53 char out[16];
54 TimeToString(*this, out, 16);
55 return out;
56}
57
58#endif
59
60roo_logging::Stream& operator<<(roo_logging::Stream& os, const Time& val) {
61 char out[16];
62 TimeToString(val, out, 16);
63 os << out;
64 return os;
65}
66
67} // namespace roo_quantity
Representation of elapsed, internally stored as floating-point seconds.
Definition time.h:22
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
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
Time TimeInSeconds(float time)
Returns a time object equivalent to the specified time expressed in seconds.
Definition time.h:111