NDEVR
API Documentation
Time.h
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/BufferBase.h>
36#include <functional>
37struct tm;
38namespace NDEVR
39{
40 class String;
41 class StringView;
43 class Exception;
61 class Time
62 {
63 public:
71 {
72 JANUARY = 1//First month of the year
73 , FEBRUARY//Second month of the year
74 , MARCH//Third month of the year
75 , APRIL//Fourth month of the year
76 , MAY//Fifth month of the year
77 , JUNE//Sixth month of the year
78 , JULY//Seventh month of the year
79 , AUGUST//Eigth month of the year
80 , SEPTEMBER//Ninth month of the year
81 , OCTOBER//Tenth month of the year
82 , NOVEMBER//Eleventh month of the year
83 , DECEMBER//Twelve month of the year
84 };
85 // Static Variables
86 //------------------------
87 public:
88 static constexpr uint08 NANOSECOND = 1;
89 static constexpr uint08 MICROSECOND = 1000 * NANOSECOND;
90 static constexpr uint08 MILLISECOND = 1000000;
91 static constexpr uint08 SECOND = 1000000000;
92 static constexpr uint08 MINUTE = SECOND * 60;
93 static constexpr uint08 HOUR = MINUTE * 60;
94 static constexpr uint08 DAY = HOUR * 24;
95 static constexpr uint08 WEEK = DAY * 7;
96 static constexpr uint08 YEAR = DAY * 365;
97 static constexpr uint08 LEAP_YEAR = DAY * 366;
98 // Member Functions
99 //------------------------
100 public:
104 constexpr Time()
105 : m_time(Constant<uint08>::Invalid)
106 {}
107
113 constexpr explicit Time(const uint08& time)
114 : m_time(time)
115 {}
116
126 NDEVR_BASE_API Time(uint04 year, uint04 month, uint04 day, uint04 hour = 0, uint04 minute = 0, uint04 second = 0, bool is_local_time = false);
134 NDEVR_BASE_API Time(const StringView& time_string, bool is_local_time, const StringView& format_string);
139 NDEVR_BASE_API void setTime(const uint08& time);
147 NDEVR_BASE_API StringAllocatingView getAsString(bool get_local_time, const StringView& format_string) const;
148
149 //NDEVR_BASE_API void appendTo(StringAllocatingView& value, bool get_local_time, const StringView& format_string) const;
150 NDEVR_BASE_API void appendTo(StringAllocatingView& value, bool get_local_time, const StringView& format_string) const;
159 NDEVR_BASE_API void setTimeFromString(const StringView& time_string, bool is_local_time, const StringView& format_string);
165 NDEVR_BASE_API void setHour(const uint01& hour, bool is_local_time);
171 NDEVR_BASE_API void setMinute(const uint01& minute, bool is_local_time);
177 NDEVR_BASE_API void setSecond(const uint01& second, bool is_local_time);
182 NDEVR_BASE_API void setMillisecond(const uint02& millisecond);
188 NDEVR_BASE_API void setYear(const uint02& year, bool is_local_time);
194 NDEVR_BASE_API void setMonth(const Month& month, bool is_local_time);
200 NDEVR_BASE_API void setMonthDay(const uint01& day, bool is_local_time);
206 NDEVR_BASE_API void setDayOfYear(const uint02& day, bool is_local_time);
213 NDEVR_BASE_API fltp08 julianDay(bool is_local_time) const;
219 NDEVR_BASE_API uint02 year(bool is_local_time) const;
225 NDEVR_BASE_API Month month(bool is_local_time) const;
231 NDEVR_BASE_API uint01 monthDay(bool is_local_time) const;
237 NDEVR_BASE_API uint02 yearDay(bool is_local_time) const;
238
244 NDEVR_BASE_API uint01 hour(bool is_local_time) const;
250 NDEVR_BASE_API uint01 minute(bool is_local_time) const;
256 NDEVR_BASE_API uint01 second(bool is_local_time) const;
261 NDEVR_BASE_API uint02 millisecond() const;
267 [[nodiscard]] constexpr uint08 getNanoSeconds() const
268 {
269 return m_time;
270 }
271
275 [[nodiscard]] uint08 getMilliseconds() const
276 {
277 return m_time / MILLISECOND;
278 }
279
283 NDEVR_BASE_API uint08 unixTime() const;
284
285 //Used for TimeSpan calculations
286 [[nodiscard]] NDEVR_BASE_API uint08 leapYearConstant() const;
287 [[nodiscard]] NDEVR_BASE_API uint08 daysConstant() const;
288 [[nodiscard]] NDEVR_BASE_API uint08 monthsConstant() const;
289
290 inline constexpr bool operator==(const Time& time) const
291 {
292 return m_time == time.m_time;
293 }
294 inline constexpr bool operator!=(const Time& time) const
295 {
296 return m_time != time.m_time;
297 }
298 inline constexpr bool operator<(const Time& time) const
299 {
300 return m_time < time.m_time;
301 }
302 inline constexpr bool operator<=(const Time& time) const
303 {
304 return m_time <= time.m_time;
305 }
306 inline constexpr bool operator>(const Time& time) const
307 {
308 return m_time > time.m_time;
309 }
310 inline constexpr bool operator>=(const Time& time) const
311 {
312 return m_time >= time.m_time;
313 }
314 private:
315 uint08 m_time;
316 public:
324 NDEVR_BASE_API static Time SystemTime();
330 static NDEVR_BASE_API void SetSystemTime(const Time& time);
331 NDEVR_BASE_API static void SetMakeUTCFromLocalFunction(const std::function<uint08(const tm& tm)>& function);
332 NDEVR_BASE_API static void SetMakeLocalFromUTCFunction(const std::function<uint08(const tm& tm)>& function);
333 NDEVR_BASE_API static void SetClockErrorCallback(const std::function<void(const Exception& error)>& function);
334 private:
335 static tm getTime(sint08 time, bool use_local_time);
336 static uint08 MakeGMTFromLocal(const tm& time);
337 static uint08 MakeLocalFromGMT(const tm& time);
338 private:
339 static constexpr uint02 s_epoch_year = 1970;
340 static constexpr uint02 s_tm_year_base = 1900;
341 static std::function<uint08(const tm& tm)> s_make_utc_from_local;
342 static std::function<uint08(const tm& tm)> s_make_local_from_utc;
343 static std::function<void(const Exception& error)> s_clock_error_callback;
344 static sint08 s_nano_second_offset;
345 };
346
347
357 template<>
358 struct ObjectInfo<Time, false, false>
359 {
360 static const uint01 Dimensions = 0;
361 static const bool Vector = false;
362 static const bool Primitive = true;
363 static const bool Pointer = false;
364 static const bool Unsigned = false;
365 static const bool Float = true;
366 static const bool Integer = false;
367 static const bool Number = true;
368 static const bool String = false;
369 static const bool Color = false;
370 static const bool Buffer = false;
371 static const bool Boolean = false;
372 static constexpr ObjectInfo<Time, false, false> VectorSub() { return ObjectInfo<Time, false, false>(); }
373 };
374
375 static_assert(sizeof(Time(0)) == 8, "Bad time allocation size");
376
377 template<> constexpr const Time Constant<Time>::Invalid = Time(18446744073709551615ULL);
378 template<> constexpr const Time Constant<Time>::Max = Time(18446744073709551614ULL);
379 template<> constexpr const Time Constant<Time>::Min = Time(0);
380}
Provides consistent interface to handle errors through the throw expression.
Definition Exception.hpp:47
This class is like a string view, but may optionally store the data internally Useful if the return t...
Definition String.h:1278
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
StringAllocatingView getAsString(bool get_local_time, const StringView &format_string) const
Converts the timestamp to a string representation.
static void SetSystemTime(const Time &time)
Sets the application's system clock to a specified Time.
void setMonth(const Month &month, bool is_local_time)
Sets the month (1-12).
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.
void setYear(const uint02 &year, bool is_local_time)
Sets the year (1970-2106).
static Time SystemTime()
Retrieves the current system time which is a combination of std::chrono::steady_clock to ensure smoot...
uint01 hour(bool is_local_time) const
Retrieves the hour of the day, 0-23.
uint08 unixTime() const
Converts the timestamp to a UNIX time representation, or seconds since 1970.
void setMonthDay(const uint01 &day, bool is_local_time)
Sets the day of the month (1-31).
void setTimeFromString(const StringView &time_string, bool is_local_time, const StringView &format_string)
Sets the time by converting the timestamp from a string representation.
uint02 millisecond() const
Retrieves the millisecond part of a second, from 0-999.
constexpr uint08 getNanoSeconds() const
Gets the time in nanoseconds since 1970.
Definition Time.h:267
fltp08 julianDay(bool is_local_time) const
Julian dates (abbreviated JD) are simply a continuous count of days and fractions since noon Universa...
uint01 monthDay(bool is_local_time) const
Retrieves the day of the month, 1-31.
void setDayOfYear(const uint02 &day, bool is_local_time)
Sets the day of the year (1-365 or 366 on leap years).
uint08 getMilliseconds() const
Gets the timestamp in milliseconds since 1970.
Definition Time.h:275
void setTime(const uint08 &time)
Sets the internal timestamp.
uint01 minute(bool is_local_time) const
Retrieves the minute of the hour, from 0-59.
constexpr Time(const uint08 &time)
Constructor initializing the timestamp with a specific value.
Definition Time.h:113
Month
Enumerates the months of the year.
Definition Time.h:71
void setMillisecond(const uint02 &millisecond)
Sets the millisecond of the second, 0-999.
Time(const StringView &time_string, bool is_local_time, const StringView &format_string)
Constructs a Time from a string representation.
void setMinute(const uint01 &minute, bool is_local_time)
Sets the minute of the hour, 0-59.
uint02 year(bool is_local_time) const
Retrieves the year component of the timestamp.
uint01 second(bool is_local_time) const
Retrieves the second of the minute, from 0-59 (60 if ultra-rare leap second).
constexpr Time()
Default constructor initializing an invalid timestamp.
Definition Time.h:104
void setSecond(const uint01 &second, bool is_local_time)
Sets the second of the minute, 0-59.
uint02 yearDay(bool is_local_time) const
Retrieves the day of the year, 1-365(366 on leap year).
Month month(bool is_local_time) const
Retrieves the month component of the timestamp.
void setHour(const uint01 &hour, bool is_local_time)
Sets the hour of the day, 0-23.
The primary namespace for the NDEVR SDK.
constexpr bool operator!=(const Vector< t_dims, t_type > &vec_a, const Vector< t_dims, t_type > &vec_b)
Inequality operator.
Definition Vector.hpp:673
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Information about the object.
Definition ObjectInfo.h:55