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 "../Foundation/PrimitiveTypes.h"
5
6namespace SC
7{
9namespace Time
10{
11struct Absolute;
12struct Monotonic;
13struct Realtime;
14struct Relative;
16
17struct Milliseconds;
18struct Nanoseconds;
19struct Seconds;
20} // namespace Time
21
23
26
28
31{
32 constexpr Nanoseconds() : ns(0) {}
33 constexpr explicit Nanoseconds(int64_t ns) : ns(ns) {};
34 int64_t ns;
35
36 [[nodiscard]] bool operator>(const Nanoseconds other) const { return ns > other.ns; }
37 [[nodiscard]] bool operator<(const Nanoseconds other) const { return ns < other.ns; }
38 [[nodiscard]] bool operator==(const Nanoseconds other) const { return ns == other.ns; }
39};
40
43{
44 constexpr Milliseconds() : ms(0) {}
45 constexpr explicit Milliseconds(int64_t ms) : ms(ms) {};
46 int64_t ms;
47
48 [[nodiscard]] bool operator>(const Milliseconds other) const { return ms > other.ms; }
49 [[nodiscard]] bool operator<(const Milliseconds other) const { return ms < other.ms; }
50 [[nodiscard]] bool operator==(const Milliseconds other) const { return ms == other.ms; }
51};
52
55{
56 constexpr Seconds() : sec(0) {}
57 constexpr explicit Seconds(int64_t sec) : sec(sec) {};
58 int64_t sec;
59
60 [[nodiscard]] bool operator>(const Seconds other) const { return sec > other.sec; }
61 [[nodiscard]] bool operator<(const Seconds other) const { return sec < other.sec; }
62 [[nodiscard]] bool operator==(const Seconds other) const { return sec == other.sec; }
63
64 constexpr operator Milliseconds() { return Milliseconds(sec * 1000); }
65};
66
69{
71 Relative() : seconds(0.0) {}
72
74 Relative(Milliseconds time) : seconds(static_cast<double>(time.ms / 1e3)) {}
75
77 Relative(Nanoseconds time) : seconds(static_cast<double>(time.ns / 1e9)) {}
78
80 Relative(Seconds time) : seconds(static_cast<double>(time.sec)) {}
81
82 static Relative fromSeconds(double seconds) { return Relative(seconds); }
83
84 [[nodiscard]] bool operator>(const Relative other) const { return seconds > other.seconds; }
85 [[nodiscard]] bool operator<(const Relative other) const { return seconds < other.seconds; }
86 [[nodiscard]] bool operator==(const Relative other) const { return toNanoseconds() == other.toNanoseconds(); }
87
88 Seconds toSeconds() const { return Seconds(static_cast<int64_t>(seconds + 0.5)); }
89 Nanoseconds toNanoseconds() const { return Nanoseconds(static_cast<int64_t>(seconds * 1e9 + 0.5)); }
90 Milliseconds toMilliseconds() const { return Milliseconds(static_cast<int64_t>(seconds * 1e3 + 0.5)); }
91
92 private:
93 Relative(double seconds) : seconds(seconds) {}
94 double seconds = 0;
95};
96
97// User defined literals
98// Using "unsigned long long int" instead of int64_t because it's mandated by the standard.
99// clang-format off
100inline Time::Nanoseconds operator""_ns(unsigned long long int ns) { return Time::Nanoseconds(static_cast<int64_t>(ns)); }
101inline Time::Milliseconds operator""_ms(unsigned long long int ms) { return Time::Milliseconds(static_cast<int64_t>(ms)); }
102inline Time::Seconds operator""_sec(unsigned long long int sec) { return Time::Seconds(static_cast<int64_t>(sec)); }
103// clang-format on
104
108struct Time::Absolute : public TimeMs
109{
110 protected:
111 struct Internal;
112
113 public:
115 Absolute() = default;
116
119 Absolute(int64_t milliseconds) : TimeMs{milliseconds} {}
120
121 Absolute(TimeMs time) : TimeMs{time} {}
122
125 {
126 uint16_t year = 0;
127 uint8_t month = 0;
128 uint8_t dayOfMonth = 0;
129 uint8_t dayOfWeek = 0;
130 uint8_t dayOfYear = 0;
131 uint8_t hour = 0;
132 uint8_t minutes = 0;
133 uint8_t seconds = 0;
134
135 const char* getMonth() const;
136 const char* getDay() const;
137 bool isDaylightSaving = false;
138
139 private:
140 friend struct Internal;
141 char monthName[16];
142 char dayName[16];
143 };
144
151 [[nodiscard]] bool parseLocal(ParseResult& result) const;
152
156 [[nodiscard]] bool parseUTC(ParseResult& result) const;
157
160 [[nodiscard]] bool isLaterThanOrEqualTo(Absolute other) const;
161
164 [[nodiscard]] bool isLaterThan(Absolute other) const;
165
168 [[nodiscard]] Milliseconds subtractExact(Absolute other) const;
169
171 [[nodiscard]] Absolute offsetBy(Milliseconds other) const;
172};
173
176{
177 using Absolute::Absolute;
178
180 [[nodiscard]] static Monotonic now();
181
183 [[nodiscard]] int64_t getMonotonicMilliseconds() const { return milliseconds; }
184};
185
188{
189 using Absolute::Absolute;
190
192 [[nodiscard]] static Realtime now();
193
195 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return milliseconds; }
196};
197
200{
202
208
216
223 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
224
232
237
240
243
246
249
250 private:
251 int64_t part1;
252 int64_t part2;
253
254 struct Internal;
255};
256
258} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
long long int64_t
Platform independent (8) bytes signed int.
Definition PrimitiveTypes.h:50
A vocabulary type representing a time interval in milliseconds since epoch.
Definition PrimitiveTypes.h:63
Holds information on a parsed absolute time from Absolute::parseLocal.
Definition Time.h:125
Absolute time as realtime or monotonically increasing clock.
Definition Time.h:109
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:119
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:200
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:43
Represent monotonically increasing time (use Monotonic::now for current time)
Definition Time.h:176
int64_t getMonotonicMilliseconds() const
Return given time as monotonically incrementing milliseconds.
Definition Time.h:183
static Monotonic now()
Obtain time according to monotonic clock.
Type-safe wrapper of uint64 used to represent nanoseconds.
Definition Time.h:31
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition Time.h:188
static Realtime now()
Obtain time according to realtime clock.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition Time.h:195
Interval of time represented with 64 bit double precision float.
Definition Time.h:69
Relative(Milliseconds time)
Construct a Relative from milliseconds.
Definition Time.h:74
Relative(Seconds time)
Construct a Relative from seconds.
Definition Time.h:80
Relative()
how many seconds have elapsed in
Definition Time.h:71
Relative(Nanoseconds time)
Construct a Relative from nanoseconds.
Definition Time.h:77
Type-safe wrapper of uint64 used to represent seconds.
Definition Time.h:55