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]] operator TimeMs() const { return {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 [[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.
100// clang-format off
101inline Time::Nanoseconds operator""_ns(unsigned long long int ns) { return Time::Nanoseconds(static_cast<int64_t>(ns)); }
102inline Time::Milliseconds operator""_ms(unsigned long long int ms) { return Time::Milliseconds(static_cast<int64_t>(ms)); }
103inline Time::Seconds operator""_sec(unsigned long long int sec) { return Time::Seconds(static_cast<int64_t>(sec)); }
104// clang-format on
105
109struct Time::Absolute : public TimeMs
110{
111 protected:
112 struct Internal;
113
114 public:
116 Absolute() = default;
117
120 Absolute(int64_t milliseconds) : TimeMs{milliseconds} {}
121
122 Absolute(TimeMs time) : TimeMs{time} {}
123
126 {
127 uint16_t year = 0;
128 uint8_t month = 0;
129 uint8_t dayOfMonth = 0;
130 uint8_t dayOfWeek = 0;
131 uint8_t dayOfYear = 0;
132 uint8_t hour = 0;
133 uint8_t minutes = 0;
134 uint8_t seconds = 0;
135
136 const char* getMonth() const;
137 const char* getDay() const;
138 bool isDaylightSaving = false;
139
140 private:
141 friend struct Internal;
142 char monthName[16];
143 char dayName[16];
144 };
145
152 [[nodiscard]] bool parseLocal(ParseResult& result) const;
153
157 [[nodiscard]] bool parseUTC(ParseResult& result) const;
158
161 [[nodiscard]] bool isLaterThanOrEqualTo(Absolute other) const;
162
165 [[nodiscard]] bool isLaterThan(Absolute other) const;
166
169 [[nodiscard]] Milliseconds subtractExact(Absolute other) const;
170
172 [[nodiscard]] Absolute offsetBy(Milliseconds other) const;
173};
174
177{
178 using Absolute::Absolute;
179
181 [[nodiscard]] static Monotonic now();
182
184 [[nodiscard]] int64_t getMonotonicMilliseconds() const { return milliseconds; }
185};
186
189{
190 using Absolute::Absolute;
191
193 [[nodiscard]] static Realtime now();
194
196 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return milliseconds; }
197};
198
201{
203
209
217
224 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
225
233
238
241
244
247
250
251 private:
252 int64_t part1;
253 int64_t part2;
254
255 struct Internal;
256};
257
259} // 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:126
Absolute time as realtime or monotonically increasing clock.
Definition Time.h:110
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:120
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:201
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:177
int64_t getMonotonicMilliseconds() const
Return given time as monotonically incrementing milliseconds.
Definition Time.h:184
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:189
static Realtime now()
Obtain time according to realtime clock.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition Time.h:196
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