API Documentation
Loading...
Searching...
No Matches
Time.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Time
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <NDEVR/ObjectInfo.h>
36#include <functional>
37struct tm;
38namespace NDEVR
39{
40 class String;
41 class Exception;
42 /**
43 * @class Time
44 * @brief Represents a timestamp with utilities for manipulation and conversion.
45 *
46 * The `Time` class provides a framework for working with timestamps. It supports
47 * various time operations such as getting and setting components (year, month,
48 * day, hour, etc.), formatting, and conversions between time zones.
49 *
50 * This class is designed for flexibility, making it suitable for a variety of
51 * applications, including scheduling, logging, and time-based calculations.
52 */
53 class Time
54 {
55 public:
56 /**
57 * @enum Months
58 * @brief Enumerates the months of the year.
59 *
60 * Values range from `JANUARY` (1) to `DECEMBER` (12) for easy reference
61 * in operations involving months.
62 */
78 // Static Variables
79 //------------------------
80 public:
81 static constexpr uint08 NANOSECOND = 1;
82 static constexpr uint08 MICROSECOND = 1000 * NANOSECOND;
83 static constexpr uint08 MILLISECOND = 1000000;
84 static constexpr uint08 SECOND = 1000000000;
85 static constexpr uint08 MINUTE = SECOND * 60;
86 static constexpr uint08 HOUR = MINUTE * 60;
87 static constexpr uint08 DAY = HOUR * 24;
88 static constexpr uint08 WEEK = DAY * 7;
89 static constexpr uint08 YEAR = DAY * 365;
90 static constexpr uint08 LEAP_YEAR = DAY * 366;
91 // Member Functions
92 //------------------------
93 public:
94 /**
95 * @brief Default constructor initializing an invalid timestamp.
96 */
97 constexpr Time()
98 : m_time(Constant<uint08>::Invalid)
99 {}
100 /**
101 * @brief Constructor initializing the timestamp with a specific value.
102 * @param time Timestamp in nanoseconds.
103 */
104 constexpr explicit Time(const uint08& time)
105 : m_time(time)
106 {}
107 /**
108 * @brief Constructs a `Time` object using date and time components.
109 * @param year Year component.
110 * @param month Month component (1-12).
111 * @param day Day of the month.
112 * @param hour Hour of the day (default is 0).
113 * @param minute Minute of the hour (default is 0).
114 * @param second Second of the minute (default is 0).
115 * @param is_local_time Indicates whether the time is local or UTC.
116 */
117 NDEVR_BASE_API Time(uint04 year, uint04 month, uint04 day, uint04 hour = 0, uint04 minute = 0, uint04 second = 0, bool is_local_time = false);
118 /**
119 * @brief Constructs a Time from a string representation.
120 * https://ndevr.org/time-patterns/
121 * @param time_string The string to convert to a time
122 * @param is_local_time Whether to convert to local time.
123 * @param format_string The format for the string representation.
124 */
125 NDEVR_BASE_API Time(const String& time_string, bool is_local_time, const String& format_string);
126 NDEVR_BASE_API Time(const char* time_string, bool is_local_time, const String& format_string);
127 /**
128 * @brief Sets the internal timestamp.
129 * @param time Timestamp in nanoseconds.
130 */
131 NDEVR_BASE_API void setTime(const uint08& time);
132 /**
133 * @brief Converts the timestamp to a string representation.
134 * https://ndevr.org/time-patterns/
135 * @param get_local_time Whether to convert to local time.
136 * @param format_string The format for the string representation.
137 * @return A formatted string representing the timestamp.
138 */
139 NDEVR_BASE_API String toString(bool get_local_time, const String& format_string) const;
140 /**
141 * @brief Sets the time by converting the timestamp from a string representation.
142 * https://ndevr.org/time-patterns/
143 * @param time_string The string to convert to a time
144 * @param is_local_time Whether to convert to local time.
145 * @param format_string The format for the string representation.
146 */
147 NDEVR_BASE_API void setTimeFromString(const String& time_string, bool is_local_time, const String& format_string);
148 NDEVR_BASE_API void setHour(const uint01& hour, bool is_local_time);
149 NDEVR_BASE_API void setMinute(const uint01& minute, bool is_local_time);
150 NDEVR_BASE_API void setSecond(const uint01& second, bool is_local_time);
152 NDEVR_BASE_API void setYear(const uint02& year, bool is_local_time);
153 NDEVR_BASE_API void setMonth(const uint01& month, bool is_local_time);
154 NDEVR_BASE_API void setMonthDay(const uint01& day, bool is_local_time);
155 NDEVR_BASE_API void setDayOfYear(const uint02& day, bool is_local_time);
156 NDEVR_BASE_API fltp08 julianDay(bool is_local_time) const;
157 /**
158 * @brief Retrieves the year component of the timestamp.
159 * @param is_local_time Indicates whether the time is local or UTC.
160 * @return Year as a 2-byte unsigned integer.
161 */
162 NDEVR_BASE_API uint02 year(bool is_local_time) const;
163 /**
164 * @brief Retrieves the month component of the timestamp.
165 * @param is_local_time Indicates whether the time is local or UTC.
166 * @return Month as a 2-byte unsigned integer.
167 */
168 NDEVR_BASE_API uint02 month(bool is_local_time) const;
169 NDEVR_BASE_API uint02 monthDay(bool is_local_time) const;
170 NDEVR_BASE_API uint02 yearDay(bool is_local_time) const;
171
172 /**
173 * @brief Retrieves the hour component of the timestamp.
174 * @param is_local_time Indicates whether the time is local or UTC.
175 * @return Hour as a 1-byte unsigned integer.
176 */
177 NDEVR_BASE_API uint01 hour(bool is_local_time) const;
178 NDEVR_BASE_API uint01 minute(bool is_local_time) const;
179 NDEVR_BASE_API uint01 second(bool is_local_time) const;
181 /**
182 * @brief Gets the timestamp in nanoseconds.
183 * @return Timestamp in nanoseconds.
184 */
185 [[nodiscard]] constexpr uint08 getNanoSeconds() const
186 {
187 return m_time;
188 }
189 /**
190 * @brief Gets the timestamp in milliseconds.
191 * @return Timestamp in milliseconds.
192 */
193 [[nodiscard]] uint08 getMilliseconds() const
194 {
195 return m_time / MILLISECOND;
196 }
197 /**
198 * @brief Converts the timestamp to a UNIX time representation.
199 * @return UNIX timestamp in nanoseconds.
200 */
202
203 //Used for TimeSpan calculations
205 [[nodiscard]] NDEVR_BASE_API uint08 daysConstant() const;
206 [[nodiscard]] NDEVR_BASE_API uint08 monthsConstant() const;
207
208 inline bool operator==(const Time& time) const
209 {
210 return m_time == time.m_time;
211 }
212 inline bool operator!=(const Time& time) const
213 {
214 return m_time != time.m_time;
215 }
216 inline bool operator<(const Time& time) const
217 {
218 return m_time < time.m_time;
219 }
220 inline bool operator<=(const Time& time) const
221 {
222 return m_time <= time.m_time;
223 }
224 inline bool operator>(const Time& time) const
225 {
226 return m_time > time.m_time;
227 }
228 inline bool operator>=(const Time& time) const
229 {
230 return m_time >= time.m_time;
231 }
232 private:
233 uint08 m_time;///< Internal representation of the timestamp in nanoseconds.
234 public:
235 /**
236 * @brief Retrieves the current system time.
237 * @return The current system time as a `Time` object.
238 */
240 /**
241 * @brief Sets the application's system clock to a specified `Time`.
242 * Note: Does not affect OS Time
243 * @param time The `Time` object to set as the system time.
244 */
245 static NDEVR_BASE_API void SetSystemTime(const Time& time);
246 NDEVR_BASE_API static void SetMakeUTCFromLocalFunction(const std::function<uint08(const tm& tm)>& function);
247 NDEVR_BASE_API static void SetMakeLocalFromUTCFunction(const std::function<uint08(const tm& tm)>& function);
248 NDEVR_BASE_API static void SetClockErrorCallback(const std::function<void(const Exception& error)>& function);
249 private:
250 static tm getTime(sint08 time, bool use_local_time);
251 static uint08 MakeGMTFromLocal(const tm& time);
252 static uint08 MakeLocalFromGMT(const tm& time);
253 private:
254 static constexpr uint02 s_epoch_year = 1970;
255 static constexpr uint02 s_tm_year_base = 1900;
256 static std::function<uint08(const tm& tm)> s_make_utc_from_local;
257 static std::function<uint08(const tm& tm)> s_make_local_from_utc;
258 static std::function<void(const Exception& error)> s_clock_error_callback;
259 static sint08 s_nano_second_offset;
260 };
261
262
263 /**--------------------------------------------------------------------------------------------------
264 Struct: ObjectInfo<Time>
265
266 Information about the object.
267
268 Author: Tyler Parke
269
270 Date: 2020-09-13
271 **/
272 template<>
273 struct ObjectInfo<Time, false, false>
274 {
275 static const uint01 Dimensions = 0;
276 static const bool Vector = false;
277 static const bool Primitive = true;
278 static const bool Pointer = false;
279 static const bool Unsigned = false;
280 static const bool Float = true;
281 static const bool Integer = false;
282 static const bool Number = true;
283 static const bool String = false;
284 static const bool Color = false;
285 static const bool Buffer = false;
286 static const bool Boolean = false;
287 static constexpr ObjectInfo<Time, false, false> VectorSub() { return ObjectInfo<Time, false, false>(); }
288 };
289
290 static_assert(sizeof(Time(0)) == 8, "Bad time allocation size");
291
292 template<> constexpr const Time Constant<Time>::Invalid = Time(18446744073709551615ULL);
293 template<> constexpr const Time Constant<Time>::Max = Time(18446744073709551614ULL);
294 template<> constexpr const Time Constant<Time>::Min = Time(0);
295}
#define NDEVR_BASE_API
Definition DLLInfo.h:57
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
The core Color class in the NDEVR API. Colors can be defined in several ways. The ACIColor is compact...
Definition Color.h:41
Provides consistent interface to handle errors through the throw expression. All exceptions generated...
Definition Exception.hpp:47
Provides shared ownership of a dynamically allocated object.
Definition Pointer.hpp:71
The core String class for the NDEVR API.
Definition String.h:69
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
uint08 unixTime() const
Converts the timestamp to a UNIX time representation.
void setMonth(const uint01 &month, bool is_local_time)
Months
Enumerates the months of the year.
Definition Time.h:64
@ FEBRUARY
Definition Time.h:66
@ NOVEMBER
Definition Time.h:75
@ JANUARY
Definition Time.h:65
@ OCTOBER
Definition Time.h:74
@ DECEMBER
Definition Time.h:76
@ JUNE
Definition Time.h:70
@ MARCH
Definition Time.h:67
@ AUGUST
Definition Time.h:72
@ MAY
Definition Time.h:69
@ SEPTEMBER
Definition Time.h:73
@ JULY
Definition Time.h:71
@ APRIL
Definition Time.h:68
static constexpr uint08 SECOND
Definition Time.h:84
static constexpr uint08 LEAP_YEAR
Definition Time.h:90
Time(const String &time_string, bool is_local_time, const String &format_string)
Constructs a Time from a string representation. https://ndevr.org/time-patterns/.
static void SetClockErrorCallback(const std::function< void(const Exception &error)> &function)
uint08 leapYearConstant() const
uint01 minute(bool is_local_time) const
uint01 second(bool is_local_time) const
static void SetSystemTime(const Time &time)
Sets the application's system clock to a specified Time. Note: Does not affect OS Time.
bool operator!=(const Time &time) const
Definition Time.h:212
uint02 monthDay(bool is_local_time) const
void setTime(const uint08 &time)
Sets the internal timestamp.
uint02 millisecond() const
static void SetMakeLocalFromUTCFunction(const std::function< uint08(const tm &tm)> &function)
void setMinute(const uint01 &minute, bool is_local_time)
void setHour(const uint01 &hour, bool is_local_time)
uint02 yearDay(bool is_local_time) const
static constexpr uint08 WEEK
Definition Time.h:88
uint08 daysConstant() const
fltp08 julianDay(bool is_local_time) const
constexpr Time()
Default constructor initializing an invalid timestamp.
Definition Time.h:97
uint01 hour(bool is_local_time) const
Retrieves the hour component of the timestamp.
constexpr Time(const uint08 &time)
Constructor initializing the timestamp with a specific value.
Definition Time.h:104
uint02 month(bool is_local_time) const
Retrieves the month component of the timestamp.
void setSecond(const uint01 &second, bool is_local_time)
static constexpr uint08 NANOSECOND
Definition Time.h:81
constexpr uint08 getNanoSeconds() const
Gets the timestamp in nanoseconds.
Definition Time.h:185
static constexpr uint08 MINUTE
Definition Time.h:85
void setYear(const uint02 &year, bool is_local_time)
uint08 monthsConstant() const
bool operator>=(const Time &time) const
Definition Time.h:228
static constexpr uint08 MICROSECOND
Definition Time.h:82
Time(uint04 year, uint04 month, uint04 day, uint04 hour=0, uint04 minute=0, uint04 second=0, bool is_local_time=false)
Constructs a Time object using date and time components.
Time(const char *time_string, bool is_local_time, const String &format_string)
bool operator<(const Time &time) const
Definition Time.h:216
static constexpr uint08 DAY
Definition Time.h:87
String toString(bool get_local_time, const String &format_string) const
Converts the timestamp to a string representation. https://ndevr.org/time-patterns/.
bool operator>(const Time &time) const
Definition Time.h:224
static constexpr uint08 HOUR
Definition Time.h:86
void setTimeFromString(const String &time_string, bool is_local_time, const String &format_string)
Sets the time by converting the timestamp from a string representation. https://ndevr....
bool operator==(const Time &time) const
Definition Time.h:208
void setMillisecond(const uint02 &millisecond)
void setDayOfYear(const uint02 &day, bool is_local_time)
uint02 year(bool is_local_time) const
Retrieves the year component of the timestamp.
static void SetMakeUTCFromLocalFunction(const std::function< uint08(const tm &tm)> &function)
static Time SystemTime()
Retrieves the current system time.
static constexpr uint08 MILLISECOND
Definition Time.h:83
static constexpr uint08 YEAR
Definition Time.h:89
void setMonthDay(const uint01 &day, bool is_local_time)
bool operator<=(const Time &time) const
Definition Time.h:220
uint08 getMilliseconds() const
Gets the timestamp in milliseconds.
Definition Time.h:193
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
Definition ACIColor.h:37
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:71
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:88
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Information about the object.
Definition ObjectInfo.h:54