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/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_TIME
6#define SC_EXPORT_LIBRARY_TIME 0
7#endif
8#define SC_TIME_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_TIME)
9
10#include "../Foundation/PrimitiveTypes.h"
11
12namespace SC
13{
15namespace Time
16{
17struct Absolute;
18struct Monotonic;
19struct Realtime;
20struct Relative;
22
23struct Milliseconds;
24struct Nanoseconds;
25struct Seconds;
26} // namespace Time
27
29
32
34
37{
38 constexpr Nanoseconds() : ns(0) {}
39 constexpr explicit Nanoseconds(int64_t ns) : ns(ns) {};
40 int64_t ns;
41
42 [[nodiscard]] bool operator>(const Nanoseconds other) const { return ns > other.ns; }
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};
46
49{
50 constexpr Milliseconds() : ms(0) {}
51 constexpr explicit Milliseconds(int64_t ms) : ms(ms) {};
52 int64_t ms;
53
54 [[nodiscard]] operator TimeMs() const { return {ms}; }
55 [[nodiscard]] bool operator>(const Milliseconds other) const { return ms > other.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};
59
62{
63 constexpr Seconds() : sec(0) {}
64 constexpr explicit Seconds(int64_t sec) : sec(sec) {};
65 int64_t sec;
66
67 [[nodiscard]] bool operator>(const Seconds other) const { return sec > other.sec; }
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
71 constexpr operator Milliseconds() { return Milliseconds(sec * 1000); }
72};
73
76{
78 Relative() : seconds(0.0) {}
79
81 Relative(Milliseconds time) : seconds(static_cast<double>(time.ms / 1e3)) {}
82
84 Relative(Nanoseconds time) : seconds(static_cast<double>(time.ns / 1e9)) {}
85
87 Relative(Seconds time) : seconds(static_cast<double>(time.sec)) {}
88
89 static Relative fromSeconds(double seconds) { return Relative(seconds); }
90
91 [[nodiscard]] bool operator>(const Relative other) const { return seconds > other.seconds; }
92 [[nodiscard]] bool operator<(const Relative other) const { return seconds < other.seconds; }
93 [[nodiscard]] bool operator==(const Relative other) const { return toNanoseconds() == other.toNanoseconds(); }
94
95 Seconds toSeconds() const { return Seconds(static_cast<int64_t>(seconds + 0.5)); }
96 Nanoseconds toNanoseconds() const { return Nanoseconds(static_cast<int64_t>(seconds * 1e9 + 0.5)); }
97 Milliseconds toMilliseconds() const { return Milliseconds(static_cast<int64_t>(seconds * 1e3 + 0.5)); }
98
99 private:
100 Relative(double seconds) : seconds(seconds) {}
101 double seconds = 0;
102};
103
104// User defined literals
105// Using "unsigned long long int" instead of int64_t because it's mandated by the standard.
106// clang-format off
107inline Time::Nanoseconds operator""_ns(unsigned long long int ns) { return Time::Nanoseconds(static_cast<int64_t>(ns)); }
108inline Time::Milliseconds operator""_ms(unsigned long long int ms) { return Time::Milliseconds(static_cast<int64_t>(ms)); }
109inline Time::Seconds operator""_sec(unsigned long long int sec) { return Time::Seconds(static_cast<int64_t>(sec)); }
110// clang-format on
111
115struct Time::Absolute : public TimeMs
116{
117 protected:
118 struct Internal;
119
120 public:
122 Absolute() = default;
123
126 Absolute(int64_t milliseconds) : TimeMs{milliseconds} {}
127
128 Absolute(TimeMs time) : TimeMs{time} {}
129
132 {
133 uint16_t year = 0;
134 uint8_t month = 0;
135 uint8_t dayOfMonth = 0;
136 uint8_t dayOfWeek = 0;
137 uint8_t dayOfYear = 0;
138 uint8_t hour = 0;
139 uint8_t minutes = 0;
140 uint8_t seconds = 0;
141
142 const char* getMonth() const;
143 const char* getDay() const;
144 bool isDaylightSaving = false;
145
146 private:
147 friend struct Internal;
148 char monthName[16];
149 char dayName[16];
150 };
151
158 [[nodiscard]] bool parseLocal(ParseResult& result) const;
159
163 [[nodiscard]] bool parseUTC(ParseResult& result) const;
164
167 [[nodiscard]] bool isLaterThanOrEqualTo(Absolute other) const;
168
171 [[nodiscard]] bool isLaterThan(Absolute other) const;
172
175 [[nodiscard]] Milliseconds subtractExact(Absolute other) const;
176
178 [[nodiscard]] Absolute offsetBy(Milliseconds other) const;
179};
180
183{
184 using Absolute::Absolute;
185
187 [[nodiscard]] static Monotonic now();
188
190 [[nodiscard]] int64_t getMonotonicMilliseconds() const { return milliseconds; }
191};
192
195{
196 using Absolute::Absolute;
197
199 [[nodiscard]] static Realtime now();
200
202 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return milliseconds; }
203};
204
207{
209
215
223
230 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
231
239
244
247
250
253
256
257 private:
258 int64_t part1;
259 int64_t part2;
260
261 struct Internal;
262};
263
265} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:28
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:27
long long int64_t
Platform independent (8) bytes signed int.
Definition PrimitiveTypes.h:41
A vocabulary type representing a time interval in milliseconds since epoch.
Definition PrimitiveTypes.h:50
Holds information on a parsed absolute time from Absolute::parseLocal.
Definition Time.h:132
Absolute time as realtime or monotonically increasing clock.
Definition Time.h:116
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:126
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:207
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:49
Represent monotonically increasing time (use Monotonic::now for current time)
Definition Time.h:183
int64_t getMonotonicMilliseconds() const
Return given time as monotonically incrementing milliseconds.
Definition Time.h:190
static Monotonic now()
Obtain time according to monotonic clock.
Type-safe wrapper of uint64 used to represent nanoseconds.
Definition Time.h:37
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition Time.h:195
static Realtime now()
Obtain time according to realtime clock.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition Time.h:202
Interval of time represented with 64 bit double precision float.
Definition Time.h:76
Relative(Milliseconds time)
Construct a Relative from milliseconds.
Definition Time.h:81
Relative(Seconds time)
Construct a Relative from seconds.
Definition Time.h:87
Relative()
how many seconds have elapsed in
Definition Time.h:78
Relative(Nanoseconds time)
Construct a Relative from nanoseconds.
Definition Time.h:84
Type-safe wrapper of uint64 used to represent seconds.
Definition Time.h:62