🟨 Time handling (relative, absolute, high resolution)
SaneCppTime.h contains classes to measure time and compute or measure time intervals.
Dependencies

Features
Status
🟨 MVP
This library is in MVP state but it doesn't have a clear roadmap.
Description
SC::Time::Absolute
Absolute time as realtime or monotonically increasing clock.
- See also
- Monotonic
-
Realtime
SC::Time::Absolute::parseLocal
Parses local time to a Parsed structure.
- Parameters
-
[out] | result | The Parsed structure holding current date / time |
- Returns
true
if time has been parsed successfully
Example: Time::Absolute::ParseResult local;
String result;
(void)StringBuilder::format(result, "{} {:02}/{:02}/{} {:02}:{:02}:{:02} {}", local.getDay(), local.dayOfMonth,
local.getMonth(), local.year, local.hour, local.minutes, local.seconds,
local.isDaylightSaving ? "DAYLIGHT SAVING" : "NO DAYLIGHT SAVING");
report.console.printLine(result.view());
SC::Time::Monotonic
Represent monotonically increasing time (use Monotonic::now for current time)
SC::Time::Monotonic::now
Obtain time according to monotonic clock.
SC::Time::Realtime
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
SC::Time::Realtime::now
Obtain time according to realtime clock.
SC::Time::Relative
Interval of time represented with 64 bit double precision float.
SC::Time::HighResolutionCounter
An high resolution time counter.
SC::Time::HighResolutionCounter::snap
Sets HighResolutionCounter to current instant
Example:
Time::HighResolutionCounter start, end;
start.snap();
Thread::Sleep(100);
end.snap();
Time::Milliseconds elapsedMs = end.subtractExact(start).toMilliseconds();
Time::Nanoseconds elapsedNs = end.toNanoseconds();
Time::Seconds elapsedSec = end.toSeconds();
Time::Relative relativeTime = end.getRelative();
Time::Relative approxTime = end.subtractApproximate(start);
SC::Time::HighResolutionCounter::subtractApproximate
Subtracts another HighResolutionCounter from this one, returning an approximate Relative.
- Parameters
-
other | The HighResolutionCounter to be subtracted |
- Returns
- A Relative holding the time interval between the two HighResolutionCounter
Example: Time::HighResolutionCounter start, end;
end = start.offsetBy(Time::Milliseconds(321));
Time::Milliseconds elapsed = end.subtractExact(start).toMilliseconds();
Time::Absolute maxTime(
static_cast<int64_t>(INT64_MAX - 1000));
Time::Absolute overflowed = maxTime.offsetBy(Time::Milliseconds(2000));
Time::HighResolutionCounter counter;
counter = counter.offsetBy(999_ms);
counter = counter.offsetBy(2001_ms);
Time::HighResolutionCounter base;
Time::HighResolutionCounter normalized = counter.subtractExact(base);
Time::Milliseconds totalMs = normalized.toMilliseconds();
Time::HighResolutionCounter smaller = base;
smaller = smaller.offsetBy(100_ms);
Time::HighResolutionCounter larger = base;
larger = larger.offsetBy(500_ms);
Time::HighResolutionCounter negDiff = smaller.subtractExact(larger);
Time::Milliseconds negDiffMs = negDiff.toMilliseconds();
Time::HighResolutionCounter posDiff = larger.subtractExact(smaller);
Time::Milliseconds posDiffMs = posDiff.toMilliseconds();
SC::Time::HighResolutionCounter::isLaterThanOrEqualTo
Check if this HighResolutionCounter is later or equal to another HighResolutionCounter.
- Parameters
-
other | The HighResolutionCounter to be used in the comparison |
- Returns
true
if this HighResolutionCounter is later or equal to another HighResolutionCounter
Example: Time::HighResolutionCounter start;
start.snap();
const Time::HighResolutionCounter end = start.offsetBy(123_ms);
Blog
Some relevant blog posts are:
Roadmap
🟩 Usable
🟦 Complete Features:
💡 Unplanned Features:
Statistics
Type | Lines Of Code | Comments | Sum |
Headers | 124 | 136 | 260 |
Sources | 219 | 37 | 256 |
Sum | 343 | 173 | 516 |