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 Relative;
14
15struct Milliseconds;
16struct Seconds;
17} // namespace Time
18} // namespace SC
19
21
24
26
29{
30 constexpr Milliseconds() : ms(0) {}
31 constexpr explicit Milliseconds(int64_t ms) : ms(ms){};
32 int64_t ms;
33
34 bool operator>(const Milliseconds other) const { return ms > other.ms; }
35 bool operator<(const Milliseconds other) const { return ms < other.ms; }
36};
37
40{
41 constexpr Seconds() : sec(0) {}
42 constexpr explicit Seconds(int64_t sec) : sec(sec){};
43
44 constexpr operator Milliseconds() { return Milliseconds(sec * 1000); }
45 int64_t sec;
46};
47
50{
52 Relative() : floatingSeconds(0.0) {}
53
57 static Relative fromSeconds(double seconds) { return Relative(seconds); }
58
62 {
63 return Milliseconds(static_cast<int64_t>(floatingSeconds * 1000.0 + 0.5f));
64 }
65 Seconds inSeconds() const { return Seconds(static_cast<int64_t>(floatingSeconds)); }
66
67 private:
68 Relative(double floatingSeconds) : floatingSeconds(floatingSeconds) {}
69 double floatingSeconds = 0;
70};
71
74{
75 private:
76 struct Internal;
77 int64_t millisecondsSinceEpoch;
78
79 public:
82 Absolute(int64_t millisecondsSinceEpoch) : millisecondsSinceEpoch(millisecondsSinceEpoch) {}
83
86 [[nodiscard]] static Absolute now();
87
90 {
91 uint16_t year = 0;
92 uint8_t month = 0;
93 uint8_t dayOfMonth = 0;
94 uint8_t dayOfWeek = 0;
95 uint8_t dayOfYear = 0;
96 uint8_t hour = 0;
97 uint8_t minutes = 0;
98 uint8_t seconds = 0;
99
100 const char* getMonth() const;
101 const char* getDay() const;
102 bool isDaylightSaving = false;
103
104 private:
105 friend struct Internal;
106 char monthName[16];
107 char dayName[16];
108 };
109
116 [[nodiscard]] bool parseLocal(ParseResult& result) const;
117
121 [[nodiscard]] bool parseUTC(ParseResult& result) const;
122
126 [[nodiscard]] Relative subtract(Absolute other);
127
130 [[nodiscard]] int64_t getMillisecondsSinceEpoch() const { return millisecondsSinceEpoch; }
131};
132
135{
137
143
151
158 [[nodiscard]] bool isLaterThanOrEqualTo(HighResolutionCounter other) const;
159
167
172
173 Relative getRelative() const;
174
175 int64_t part1;
176 int64_t part2;
177
178 private:
179 struct Internal;
180};
181
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:90
Absolute time represented with milliseconds since epoch.
Definition: Time.h:74
Relative subtract(Absolute other)
Obtain the Relative by subtracting this Absolute with another one.
static Absolute now()
Obtain Absolute representing current time.
Absolute(int64_t millisecondsSinceEpoch)
Construct an Absolute from milliseconds since epoch.
Definition: Time.h:82
bool parseUTC(ParseResult &result) const
Parses UTC time to a Parsed structure.
int64_t getMillisecondsSinceEpoch() const
Return given time as milliseconds since epoch.
Definition: Time.h:130
bool parseLocal(ParseResult &result) const
Parses local time to a Parsed structure.
An high resolution time counter.
Definition: Time.h:135
Relative subtractApproximate(HighResolutionCounter other) const
Subtracts another HighResolutionCounter from this one, returning an approximate Relative.
HighResolutionCounter offsetBy(Milliseconds ms) const
Returns a HighResolutionCounter offset by a given number of Milliseconds.
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:29
Interval of time represented with 64 bit double precision float.
Definition: Time.h:50
Relative()
how many seconds have elapsed in
Definition: Time.h:52
static Relative fromSeconds(double seconds)
Construct a Relative from seconds.
Definition: Time.h:57
Milliseconds inRoundedUpperMilliseconds() const
Converts current time to Milliseconds, rounding to upper integer.
Definition: Time.h:61
Type-safe wrapper of uint64 used to represent seconds.
Definition: Time.h:40