Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Time.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Common/CompilerMacrosExport.h"
5#include "../Common/PlatformMacrosType.h"
6#ifndef SC_EXPORT_LIBRARY_TIME
7#define SC_EXPORT_LIBRARY_TIME 0
8#endif
9#define SC_TIME_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_TIME)
10
11#include "../Common/PrimitiveDefinitions.h"
12
13namespace SC
14{
16namespace Time
17{
18struct Absolute;
19struct Monotonic;
20struct Realtime;
21struct Relative;
23
24struct Milliseconds;
25struct Nanoseconds;
26struct Seconds;
27} // namespace Time
28
30
33
35
38{
39 constexpr Nanoseconds() : ns(0) {}
40 constexpr explicit Nanoseconds(int64_t ns) : ns(ns) {};
41 int64_t ns;
42
43 [[nodiscard]] bool operator>(const Nanoseconds other) const { return ns > other.ns; }
44 [[nodiscard]] bool operator<(const Nanoseconds other) const { return ns < other.ns; }
45 [[nodiscard]] bool operator==(const Nanoseconds other) const { return ns == other.ns; }
46};
47
50{
51 constexpr Milliseconds() : ms(0) {}
52 constexpr explicit Milliseconds(int64_t ms) : ms(ms) {};
53 int64_t ms;
54
55 [[nodiscard]] operator TimeMs() const { return {ms}; }
56 [[nodiscard]] bool operator>(const Milliseconds other) const { return ms > other.ms; }
57 [[nodiscard]] bool operator<(const Milliseconds other) const { return ms < other.ms; }
58 [[nodiscard]] bool operator==(const Milliseconds other) const { return ms == other.ms; }
59};
60
63{
64 constexpr Seconds() : sec(0) {}
65 constexpr explicit Seconds(int64_t sec) : sec(sec) {};
66 int64_t sec;
67
68 [[nodiscard]] bool operator>(const Seconds other) const { return sec > other.sec; }
69 [[nodiscard]] bool operator<(const Seconds other) const { return sec < other.sec; }
70 [[nodiscard]] bool operator==(const Seconds other) const { return sec == other.sec; }
71
72 constexpr operator Milliseconds() { return Milliseconds(sec * 1000); }
73};
74
77{
79 Relative() : seconds(0.0) {}
80
82 Relative(Milliseconds time) : seconds(static_cast<double>(time.ms / 1e3)) {}
83
85 Relative(Nanoseconds time) : seconds(static_cast<double>(time.ns / 1e9)) {}
86
88 Relative(Seconds time) : seconds(static_cast<double>(time.sec)) {}
89
90 static Relative fromSeconds(double seconds) { return Relative(seconds); }
91
92 [[nodiscard]] bool operator>(const Relative other) const { return seconds > other.seconds; }
93 [[nodiscard]] bool operator<(const Relative other) const { return seconds < other.seconds; }
94 [[nodiscard]] bool operator==(const Relative other) const { return toNanoseconds() == other.toNanoseconds(); }
95
96 Seconds toSeconds() const { return Seconds(static_cast<int64_t>(seconds + 0.5)); }
97 Nanoseconds toNanoseconds() const { return Nanoseconds(static_cast<int64_t>(seconds * 1e9 + 0.5)); }
98 Milliseconds toMilliseconds() const { return Milliseconds(static_cast<int64_t>(seconds * 1e3 + 0.5)); }
99
100 private:
101 Relative(double seconds) : seconds(seconds) {}
102 double seconds = 0;
103};
104
105// User defined literals
106// Using "unsigned long long int" instead of int64_t because it's mandated by the standard.
107// clang-format off
108inline Time::Nanoseconds operator""_ns(unsigned long long int ns) { return Time::Nanoseconds(static_cast<int64_t>(ns)); }
109inline Time::Milliseconds operator""_ms(unsigned long long int ms) { return Time::Milliseconds(static_cast<int64_t>(ms)); }
110inline Time::Seconds operator""_sec(unsigned long long int sec) { return Time::Seconds(static_cast<int64_t>(sec)); }
111// clang-format on
112
116struct Time::Absolute : public TimeMs
117{
118 protected:
119 struct Internal;
120
121 public:
123 Absolute() = default;
124
127 Absolute(int64_t milliseconds) : TimeMs{milliseconds} {}
128
129 Absolute(TimeMs time) : TimeMs{time} {}
130
133 {
134 uint16_t year = 0;
135 uint8_t month = 0;
136 uint8_t dayOfMonth = 0;
137 uint8_t dayOfWeek = 0;
138 uint8_t dayOfYear = 0;
139 uint8_t hour = 0;
140 uint8_t minutes = 0;
141 uint8_t seconds = 0;
142
143 const char* getMonth() const;
144 const char* getDay() const;
145 bool isDaylightSaving = false;
146
147 private:
148 friend struct Internal;
149 char monthName[16];
150 char dayName[16];
151 };
152
159 [[nodiscard]] bool parseLocal(ParseResult& result) const;
160
164 [[nodiscard]] bool parseUTC(ParseResult& result) const;
165
168 [[nodiscard]] bool isLaterThanOrEqualTo(Absolute other) const;
169
172 [[nodiscard]] bool isLaterThan(Absolute other) const;
173
176 [[nodiscard]] Milliseconds subtractExact(Absolute other) const;
177
179 [[nodiscard]] Absolute offsetBy(Milliseconds other) const;
180};
181
184{
185 using Absolute::Absolute;
186
188 [[nodiscard]] static Monotonic now();
189
191 [[nodiscard]] int64_t getMonotonicMilliseconds() const { return milliseconds; }
192};
193
196{
197 using Absolute::Absolute;
198
200 [[nodiscard]] static Realtime now();
201
203 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return milliseconds; }
204};
205
208{
210
216
224
231 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
232
240
245
248
251
254
257
258 private:
259 int64_t part1;
260 int64_t part2;
261
262 struct Internal;
263};
264
266} // namespace SC
Holds information on a parsed absolute time from Absolute::parseLocal.
Definition Time.h:133
Absolute time as realtime or monotonically increasing clock.
Definition Time.h:117
Absolute offsetBy(Milliseconds other) const
Offset this absolute time with a relative time in milliseconds.
bool isLaterThanOrEqualTo(Absolute other) const
Check if this Absolute time is later or equal to another Absolute time.
bool isLaterThan(Absolute other) const
Check if this Absolute time is lather than another Absolute time.
Absolute(int64_t milliseconds)
Construct an Absolute from milliseconds since epoch.
Definition Time.h:127
bool parseUTC(ParseResult &result) const
Parses UTC time to a Parsed structure.
Absolute()=default
Construct an Absolute time equal to epoch.
Milliseconds subtractExact(Absolute other) const
Obtains the difference between this time and the other time.
bool parseLocal(ParseResult &result) const
Parses local time to a Parsed structure.
An high resolution time counter.
Definition Time.h:208
Milliseconds toMilliseconds() const
Converts to Milliseconds.
Relative subtractApproximate(HighResolutionCounter other) const
Subtracts another HighResolutionCounter from this one, returning an approximate Relative.
Relative getRelative() const
Converts to a Relative struct.
Nanoseconds toNanoseconds() const
Converts to Nanoseconds.
HighResolutionCounter offsetBy(Milliseconds ms) const
Returns a HighResolutionCounter offset by a given number of Milliseconds.
Seconds toSeconds() const
Converts to Seconds.
bool isLaterThanOrEqualTo(HighResolutionCounter other) const
Check if this HighResolutionCounter is later or equal to another HighResolutionCounter.
HighResolutionCounter & snap()
Sets HighResolutionCounter to current instant Example:
HighResolutionCounter subtractExact(HighResolutionCounter other) const
Subtracts another HighResolutionCounter from this one, returning a precise HighResolutionCounter.
Type-safe wrapper of uint64 used to represent milliseconds.
Definition Time.h:50
Represent monotonically increasing time (use Monotonic::now for current time)
Definition Time.h:184
int64_t getMonotonicMilliseconds() const
Return given time as monotonically incrementing milliseconds.
Definition Time.h:191
static Monotonic now()
Obtain time according to monotonic clock.
Type-safe wrapper of uint64 used to represent nanoseconds.
Definition Time.h:38
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition Time.h:196
static Realtime now()
Obtain time according to realtime clock.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition Time.h:203
Interval of time represented with 64 bit double precision float.
Definition Time.h:77
Relative(Milliseconds time)
Construct a Relative from milliseconds.
Definition Time.h:82
Relative(Seconds time)
Construct a Relative from seconds.
Definition Time.h:88
Relative()
how many seconds have elapsed in
Definition Time.h:79
Relative(Nanoseconds time)
Construct a Relative from nanoseconds.
Definition Time.h:85
Type-safe wrapper of uint64 used to represent seconds.
Definition Time.h:63