Sane C++ Libraries
C++ Platform Abstraction Libraries
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} // namespace SC
22
24
27
29
32{
33 constexpr Nanoseconds() : ns(0) {}
34 constexpr explicit Nanoseconds(int64_t ns) : ns(ns){};
35 int64_t ns;
36
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 [[nodiscard]] bool operator==(const Nanoseconds other) const { return ns == other.ns; }
40};
41
44{
45 constexpr Milliseconds() : ms(0) {}
46 constexpr explicit Milliseconds(int64_t ms) : ms(ms){};
47 int64_t ms;
48
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 [[nodiscard]] bool operator==(const Milliseconds other) const { return ms == other.ms; }
52};
53
56{
57 constexpr Seconds() : sec(0) {}
58 constexpr explicit Seconds(int64_t sec) : sec(sec){};
59 int64_t sec;
60
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 [[nodiscard]] bool operator==(const Seconds other) const { return sec == other.sec; }
64
65 constexpr operator Milliseconds() { return Milliseconds(sec * 1000); }
66};
67
70{
72 Relative() : seconds(0.0) {}
73
75 Relative(Milliseconds time) : seconds(static_cast<double>(time.ms / 1e3)) {}
76
78 Relative(Nanoseconds time) : seconds(static_cast<double>(time.ns / 1e9)) {}
79
81 Relative(Seconds time) : seconds(static_cast<double>(time.sec)) {}
82
83 static Relative fromSeconds(double seconds) { return Relative(seconds); }
84
85 [[nodiscard]] bool operator>(const Relative other) const { return seconds > other.seconds; }
86 [[nodiscard]] bool operator<(const Relative other) const { return seconds < other.seconds; }
87 [[nodiscard]] bool operator==(const Relative other) const { return toNanoseconds() == other.toNanoseconds(); }
88
89 Seconds toSeconds() const { return Seconds(static_cast<int64_t>(seconds + 0.5)); }
90 Nanoseconds toNanoseconds() const { return Nanoseconds(static_cast<int64_t>(seconds * 1e9 + 0.5)); }
91 Milliseconds toMilliseconds() const { return Milliseconds(static_cast<int64_t>(seconds * 1e3 + 0.5)); }
92
93 private:
94 Relative(double seconds) : seconds(seconds) {}
95 double seconds = 0;
96};
97
98// User defined literals
99// Using "unsigned long long int" instead of int64_t because it's mandated by the standard.
100namespace SC
101{
102// clang-format off
103inline Time::Nanoseconds operator""_ns(unsigned long long int ns) { return Time::Nanoseconds(static_cast<int64_t>(ns)); }
104inline Time::Milliseconds operator""_ms(unsigned long long int ms) { return Time::Milliseconds(static_cast<int64_t>(ms)); }
105inline Time::Seconds operator""_sec(unsigned long long int sec) { return Time::Seconds(static_cast<int64_t>(sec)); }
106// clang-format on
107
108} // namespace SC
109
114{
115 protected:
116 struct Internal;
117 int64_t milliseconds;
118
119 public:
121 Absolute() : milliseconds(0) {}
122
125 Absolute(int64_t milliseconds) : milliseconds(milliseconds) {}
126
129 {
130 uint16_t year = 0;
131 uint8_t month = 0;
132 uint8_t dayOfMonth = 0;
133 uint8_t dayOfWeek = 0;
134 uint8_t dayOfYear = 0;
135 uint8_t hour = 0;
136 uint8_t minutes = 0;
137 uint8_t seconds = 0;
138
139 const char* getMonth() const;
140 const char* getDay() const;
141 bool isDaylightSaving = false;
142
143 private:
144 friend struct Internal;
145 char monthName[16];
146 char dayName[16];
147 };
148
155 [[nodiscard]] bool parseLocal(ParseResult& result) const;
156
160 [[nodiscard]] bool parseUTC(ParseResult& result) const;
161
164 [[nodiscard]] bool isLaterThanOrEqualTo(Absolute other) const;
165
168 [[nodiscard]] bool isLaterThan(Absolute other) const;
169
172 [[nodiscard]] Milliseconds subtractExact(Absolute other) const;
173
175 [[nodiscard]] Absolute offsetBy(Milliseconds other) const;
176};
177
180{
181 using Absolute::Absolute;
182
184 [[nodiscard]] static Monotonic now();
185
187 [[nodiscard]] int64_t getMonotonicMilliseconds() const { return milliseconds; }
188};
189
192{
193 using Absolute::Absolute;
194
196 [[nodiscard]] static Realtime now();
197
199 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return milliseconds; }
200};
201
204{
206
212
220
227 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
228
236
241
244
247
250
253
254 private:
255 int64_t part1;
256 int64_t part2;
257
258 struct Internal;
259};
260
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
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
Holds information on a parsed absolute time from Absolute::parseLocal.
Definition: Time.h:129
Absolute time as realtime or monotonically increasing clock.
Definition: Time.h:114
Absolute offsetBy(Milliseconds other) const
Offset this absolute time with a relative time in milliseconds.
Absolute()
Construct an Absolute time equal to epoch.
Definition: Time.h:121
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:125
bool parseUTC(ParseResult &result) const
Parses UTC time to a Parsed structure.
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:204
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:44
Represent monotonically increasing time (use Monotonic::now for current time)
Definition: Time.h:180
int64_t getMonotonicMilliseconds() const
Return given time as monotonically incrementing milliseconds.
Definition: Time.h:187
static Monotonic now()
Obtain time according to monotonic clock.
Type-safe wrapper of uint64 used to represent nanoseconds.
Definition: Time.h:32
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition: Time.h:192
static Realtime now()
Obtain time according to realtime clock.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition: Time.h:199
Interval of time represented with 64 bit double precision float.
Definition: Time.h:70
Relative(Milliseconds time)
Construct a Relative from milliseconds.
Definition: Time.h:75
Relative(Seconds time)
Construct a Relative from seconds.
Definition: Time.h:81
Relative()
how many seconds have elapsed in
Definition: Time.h:72
Relative(Nanoseconds time)
Construct a Relative from nanoseconds.
Definition: Time.h:78
Type-safe wrapper of uint64 used to represent seconds.
Definition: Time.h:56