Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
DateTime.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
4#define _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
5
8
9#include "config.hpp"
10#include "constants.hpp"
11#include "date_conversions.hpp"
13#include "date_time_struct.hpp"
15#include "time_formatting.hpp"
16#include "time_parser.hpp"
17#include "time_struct.hpp"
18#include "time_utils.hpp"
19#include "time_zone_struct.hpp"
20#include "types.hpp"
21#include "validation.hpp"
22
23#include <cstdint>
24#include <cstring>
25#include <stdexcept>
26#include <string>
27#ifdef TIME_SHIELD_CPP17
28#include <string_view>
29#endif
30
31namespace time_shield {
32
36 class DateTime {
37 public:
39 DateTime() noexcept
40 : m_utc_ms(0)
41 , m_offset(0) {}
42
47 static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset = 0) noexcept {
48 return DateTime(utc_ms, offset);
49 }
50
55 static DateTime from_unix_s(ts_t utc_s, tz_t offset = 0) noexcept {
56 return DateTime(sec_to_ms(utc_s), offset);
57 }
58
62 static DateTime now_utc(tz_t offset = 0) noexcept {
63 return DateTime(ts_ms(), offset);
64 }
65
69 int month,
70 int day,
71 int hour = 0,
72 int min = 0,
73 int sec = 0,
74 int ms = 0,
75 tz_t offset = 0) {
76 const ts_ms_t local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
77 const ts_ms_t utc_ms = local_ms - offset_to_ms(offset);
78 return DateTime(utc_ms, offset);
79 }
80
94 int month,
95 int day,
96 int hour,
97 int min,
98 int sec,
99 int ms,
100 tz_t offset,
101 DateTime& out) noexcept {
102 if (!is_valid_date_time(year, month, day, hour, min, sec, ms)) {
103 return false;
104 }
105 const TimeZoneStruct tz = to_time_zone_struct(offset);
106 if (!is_valid_time_zone_offset(tz)) {
107 return false;
108 }
109 const ts_ms_t local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
110 out = DateTime(local_ms - offset_to_ms(offset), offset);
111 return true;
112 }
113
115 static DateTime from_date_time_struct(const DateTimeStruct& local_dt, tz_t offset = 0) {
116 const ts_ms_t local_ms = dt_to_timestamp_ms(local_dt);
117 const ts_ms_t utc_ms = local_ms - offset_to_ms(offset);
118 return DateTime(utc_ms, offset);
119 }
120
127 const DateTimeStruct& local_dt,
128 tz_t offset,
129 DateTime& out) noexcept {
130 if (!is_valid_date_time(local_dt)) {
131 return false;
132 }
133 const TimeZoneStruct tz = to_time_zone_struct(offset);
134 if (!is_valid_time_zone_offset(tz)) {
135 return false;
136 }
137 const ts_ms_t local_ms = dt_to_timestamp_ms(local_dt);
138 out = DateTime(local_ms - offset_to_ms(offset), offset);
139 return true;
140 }
141
146
151
154 const IsoWeekDateStruct& iso,
155 int hour = 0,
156 int min = 0,
157 int sec = 0,
158 int ms = 0,
159 tz_t offset = 0) {
161 return from_components(date.year, date.mon, date.day, hour, min, sec, ms, offset);
162 }
163
168 static bool try_parse_iso8601(const std::string& str, DateTime& out) noexcept {
169 return try_parse_iso8601_buffer(str.data(), str.size(), out);
170 }
171
172 #ifdef TIME_SHIELD_CPP17
177 static bool try_parse_iso8601(std::string_view str, DateTime& out) noexcept {
178 return try_parse_iso8601_buffer(str.data(), str.size(), out);
179 }
180 #endif
181
186 static bool try_parse_iso8601(const char* str, DateTime& out) noexcept {
187 if (str == nullptr) {
188 return false;
189 }
190 return try_parse_iso8601_buffer(str, std::strlen(str), out);
191 }
192
196 static DateTime parse_iso8601(const std::string& str) {
197 return parse_iso8601_buffer(str.data(), str.size());
198 }
199
200 #ifdef TIME_SHIELD_CPP17
204 static DateTime parse_iso8601(std::string_view str) {
205 return parse_iso8601_buffer(str.data(), str.size());
206 }
207 #endif
208
212 static DateTime parse_iso8601(const char* str) {
213 if (str == nullptr) {
214 throw std::invalid_argument("Invalid ISO8601 datetime");
215 }
216 return parse_iso8601_buffer(str, std::strlen(str));
217 }
218
223 static bool try_parse_iso_week_date(const std::string& str, IsoWeekDateStruct& iso) noexcept {
224 return parse_iso_week_date(str.data(), str.size(), iso);
225 }
226
227 #ifdef TIME_SHIELD_CPP17
229 static bool try_parse_iso_week_date(std::string_view str, IsoWeekDateStruct& iso) noexcept {
230 return parse_iso_week_date(str.data(), str.size(), iso);
231 }
232 #endif
233
235 static bool try_parse_iso_week_date(const char* str, IsoWeekDateStruct& iso) noexcept {
236 if (str == nullptr) {
237 return false;
238 }
239 return parse_iso_week_date(str, std::strlen(str), iso);
240 }
241
243 std::string to_iso8601() const {
245 }
246
248 std::string to_iso8601_utc() const {
250 }
251
253 std::string format(const std::string& fmt) const {
254 return to_string_ms(fmt, local_ms(), m_offset);
255 }
256
257 #ifdef TIME_SHIELD_CPP17
259 std::string format(std::string_view fmt) const {
260 return to_string_ms(std::string(fmt), local_ms(), m_offset);
261 }
262 #endif
263
265 std::string format(const char* fmt) const {
266 if (fmt == nullptr) {
267 return std::string();
268 }
269 return to_string_ms(std::string(fmt), local_ms(), m_offset);
270 }
271
276
278 ts_ms_t unix_ms() const noexcept {
279 return m_utc_ms;
280 }
281
283 ts_t unix_s() const noexcept {
285 }
286
288 tz_t utc_offset() const noexcept {
289 return m_offset;
290 }
291
296
298 year_t year() const {
300 }
301
303 int month() const {
305 }
306
308 int day() const {
310 }
311
313 int hour() const {
315 }
316
318 int minute() const {
320 }
321
323 int second() const {
325 }
326
328 int millisecond() const {
330 }
331
333 DateStruct date() const {
334 const DateTimeStruct local_dt = to_date_time_struct_local();
335 return create_date_struct(local_dt.year, local_dt.mon, local_dt.day);
336 }
337
340 const DateTimeStruct local_dt = to_date_time_struct_local();
341 return create_time_struct(
342 static_cast<int16_t>(local_dt.hour),
343 static_cast<int16_t>(local_dt.min),
344 static_cast<int16_t>(local_dt.sec),
345 static_cast<int16_t>(local_dt.ms));
346 }
347
351 return create_date_struct(utc_dt.year, utc_dt.mon, utc_dt.day);
352 }
353
357 return create_time_struct(
358 static_cast<int16_t>(utc_dt.hour),
359 static_cast<int16_t>(utc_dt.min),
360 static_cast<int16_t>(utc_dt.sec),
361 static_cast<int16_t>(utc_dt.ms));
362 }
363
365 Weekday weekday() const {
366 const DateStruct local_date = date();
367 return weekday_of_date<Weekday>(local_date);
368 }
369
371 int iso_weekday() const {
372 const DateStruct local_date = date();
373 return iso_weekday_of_date(local_date.year, local_date.mon, local_date.day);
374 }
375
378 const DateStruct local_date = date();
379 return to_iso_week_date(local_date.year, local_date.mon, local_date.day);
380 }
381
383 bool is_workday() const noexcept {
384 return is_workday_ms(local_ms());
385 }
386
388 bool is_weekend() const noexcept {
390 }
391
393 bool operator==(const DateTime& other) const noexcept {
394 return m_utc_ms == other.m_utc_ms;
395 }
396
398 bool operator!=(const DateTime& other) const noexcept {
399 return !(*this == other);
400 }
401
403 bool operator<(const DateTime& other) const noexcept {
404 return m_utc_ms < other.m_utc_ms;
405 }
406
408 bool operator<=(const DateTime& other) const noexcept {
409 return m_utc_ms <= other.m_utc_ms;
410 }
411
413 bool operator>(const DateTime& other) const noexcept {
414 return m_utc_ms > other.m_utc_ms;
415 }
416
418 bool operator>=(const DateTime& other) const noexcept {
419 return m_utc_ms >= other.m_utc_ms;
420 }
421
423 bool same_local(const DateTime& other) const noexcept {
424 return local_ms() == other.local_ms() && m_offset == other.m_offset;
425 }
426
428 DateTime add_ms(int64_t delta_ms) const noexcept {
429 return DateTime(m_utc_ms + delta_ms, m_offset);
430 }
431
433 DateTime add_seconds(int64_t seconds) const noexcept {
434 return add_ms(sec_to_ms(seconds));
435 }
436
438 DateTime add_minutes(int64_t minutes) const noexcept {
439 return add_ms(sec_to_ms(minutes * SEC_PER_MIN));
440 }
441
443 DateTime add_hours(int64_t hours) const noexcept {
444 return add_ms(sec_to_ms(hours * SEC_PER_HOUR));
445 }
446
448 DateTime add_days(int64_t days) const noexcept {
449 return add_ms(days * MS_PER_DAY);
450 }
451
453 int64_t diff_ms(const DateTime& other) const noexcept {
454 return m_utc_ms - other.m_utc_ms;
455 }
456
458 double diff_seconds(const DateTime& other) const noexcept {
459 return static_cast<double>(diff_ms(other)) / static_cast<double>(MS_PER_SEC);
460 }
461
463 DateTime with_offset(tz_t new_offset) const noexcept {
464 return DateTime(m_utc_ms, new_offset);
465 }
466
468 DateTime to_utc() const noexcept {
469 return with_offset(0);
470 }
471
474 const DateTimeStruct local_dt = to_date_time_struct_local();
475 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, local_dt.day);
476 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
477 }
478
481 const DateTimeStruct local_dt = to_date_time_struct_local();
482 const ts_ms_t local_end_ms = to_timestamp_ms(
483 local_dt.year,
484 local_dt.mon,
485 local_dt.day,
486 23,
487 59,
488 59,
489 static_cast<int>(MS_PER_SEC - 1));
490 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
491 }
492
495 const DateTimeStruct local_dt = to_date_time_struct_local();
496 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, 1);
497 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
498 }
499
502 const DateTimeStruct local_dt = to_date_time_struct_local();
503 const int days = num_days_in_month(local_dt.year, local_dt.mon);
504 const ts_ms_t local_end_ms = to_timestamp_ms(
505 local_dt.year,
506 local_dt.mon,
507 days,
508 23,
509 59,
510 59,
511 static_cast<int>(MS_PER_SEC - 1));
512 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
513 }
514
517 const year_t local_year = year();
518 const ts_ms_t local_start_ms = to_timestamp_ms(local_year, 1, 1);
519 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
520 }
521
524 const year_t local_year = year();
525 const ts_ms_t local_end_ms = to_timestamp_ms(
526 local_year,
527 12,
528 31,
529 23,
530 59,
531 59,
532 static_cast<int>(MS_PER_SEC - 1));
533 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
534 }
535
536 private:
537 static bool try_parse_iso8601_buffer(const char* data, std::size_t size, DateTime& out) noexcept {
538 if (data == nullptr) {
539 return false;
540 }
543 if (!time_shield::parse_iso8601(data, size, dt, tz)) {
544 return false;
545 }
546 const tz_t offset = time_zone_struct_to_offset(tz);
547 const ts_ms_t utc_ms = dt_to_timestamp_ms(dt) - offset_to_ms(offset);
548 out = from_unix_ms(utc_ms, offset);
549 return true;
550 }
551
552 static DateTime parse_iso8601_buffer(const char* data, std::size_t size) {
553 DateTime result;
554 if (!try_parse_iso8601_buffer(data, size, result)) {
555 throw std::invalid_argument("Invalid ISO8601 datetime");
556 }
557 return result;
558 }
559
560 DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
561 : m_utc_ms(utc_ms)
562 , m_offset(offset) {}
563
564 static constexpr ts_ms_t offset_to_ms(tz_t offset) noexcept {
565 return static_cast<ts_ms_t>(offset) * MS_PER_SEC;
566 }
567
568 ts_ms_t local_ms() const noexcept {
570 }
571
574 };
575
576} // namespace time_shield
577
578#endif // _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
static DateTime now_utc(tz_t offset=0) noexcept
Construct instance for current UTC time.
Definition DateTime.hpp:62
static bool try_parse_iso_week_date(std::string_view str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string_view.
Definition DateTime.hpp:229
static DateTime parse_iso8601(std::string_view str)
Parse ISO8601 string_view, throws on failure.
Definition DateTime.hpp:204
DateTime add_hours(int64_t hours) const noexcept
Add hours to UTC instant.
Definition DateTime.hpp:443
DateStruct date() const
Local date components.
Definition DateTime.hpp:333
DateTime start_of_day() const
Start of local day.
Definition DateTime.hpp:473
static bool try_parse_iso_week_date(const std::string &str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string.
Definition DateTime.hpp:223
tz_t utc_offset() const noexcept
Access stored UTC offset.
Definition DateTime.hpp:288
int day() const
Local day component.
Definition DateTime.hpp:308
static bool try_parse_iso_week_date(const char *str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date C-string.
Definition DateTime.hpp:235
TimeStruct time_of_day() const
Local time-of-day components.
Definition DateTime.hpp:339
static DateTime from_unix_s(ts_t utc_s, tz_t offset=0) noexcept
Create instance from UTC seconds.
Definition DateTime.hpp:55
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
Definition DateTime.hpp:377
static bool try_from_components(year_t year, int month, int day, int hour, int min, int sec, int ms, tz_t offset, DateTime &out) noexcept
Try to build from calendar components interpreted in provided offset.
Definition DateTime.hpp:92
DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
Definition DateTime.hpp:560
bool operator!=(const DateTime &other) const noexcept
Compare inequality by UTC instant.
Definition DateTime.hpp:398
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
Definition DateTime.hpp:168
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
Definition DateTime.hpp:243
DateTime end_of_day() const
End of local day.
Definition DateTime.hpp:480
bool same_local(const DateTime &other) const noexcept
Check if local representations match including offset.
Definition DateTime.hpp:423
static DateTime from_iso_week_date(const IsoWeekDateStruct &iso, int hour=0, int min=0, int sec=0, int ms=0, tz_t offset=0)
Build instance from ISO week date interpreted in provided offset.
Definition DateTime.hpp:153
bool operator>(const DateTime &other) const noexcept
Greater-than comparison by UTC instant.
Definition DateTime.hpp:413
int millisecond() const
Local millisecond component.
Definition DateTime.hpp:328
bool operator==(const DateTime &other) const noexcept
Compare equality by UTC instant.
Definition DateTime.hpp:393
static bool try_from_date_time_struct(const DateTimeStruct &local_dt, tz_t offset, DateTime &out) noexcept
Try to build from DateTimeStruct interpreted in provided offset.
Definition DateTime.hpp:126
ts_t unix_s() const noexcept
Access UTC seconds.
Definition DateTime.hpp:283
DateTime end_of_month() const
End of local month.
Definition DateTime.hpp:501
std::string format(std::string_view fmt) const
Format using custom string_view pattern.
Definition DateTime.hpp:259
double diff_seconds(const DateTime &other) const noexcept
Difference in seconds to another DateTime.
Definition DateTime.hpp:458
static bool try_parse_iso8601(std::string_view str, DateTime &out) noexcept
Try to parse ISO8601 string_view to DateTime.
Definition DateTime.hpp:177
bool operator<=(const DateTime &other) const noexcept
Less-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:408
DateTime with_offset(tz_t new_offset) const noexcept
Return copy with new offset preserving instant.
Definition DateTime.hpp:463
TimeStruct utc_time_of_day() const
UTC time-of-day components.
Definition DateTime.hpp:355
int hour() const
Local hour component.
Definition DateTime.hpp:313
static DateTime parse_iso8601(const std::string &str)
Parse ISO8601 string, throws on failure.
Definition DateTime.hpp:196
static bool try_parse_iso8601_buffer(const char *data, std::size_t size, DateTime &out) noexcept
Definition DateTime.hpp:537
Weekday weekday() const
Local weekday.
Definition DateTime.hpp:365
DateTime() noexcept
Default constructor sets epoch with zero offset.
Definition DateTime.hpp:39
std::string to_mql5_date_time() const
Format to MQL5 date-time string.
Definition DateTime.hpp:273
int month() const
Local month component.
Definition DateTime.hpp:303
int minute() const
Local minute component.
Definition DateTime.hpp:318
DateTimeStruct to_date_time_struct_utc() const
Convert to UTC date-time structure.
Definition DateTime.hpp:148
std::string format(const std::string &fmt) const
Format using custom pattern.
Definition DateTime.hpp:253
int iso_weekday() const
Local ISO weekday number (1..7).
Definition DateTime.hpp:371
DateTime add_minutes(int64_t minutes) const noexcept
Add minutes to UTC instant.
Definition DateTime.hpp:438
DateTime start_of_month() const
Start of local month.
Definition DateTime.hpp:494
int64_t diff_ms(const DateTime &other) const noexcept
Difference in milliseconds to another DateTime.
Definition DateTime.hpp:453
DateStruct utc_date() const
UTC date components.
Definition DateTime.hpp:349
static DateTime from_date_time_struct(const DateTimeStruct &local_dt, tz_t offset=0)
Build from DateTimeStruct interpreted in provided offset.
Definition DateTime.hpp:115
int second() const
Local second component.
Definition DateTime.hpp:323
bool operator<(const DateTime &other) const noexcept
Less-than comparison by UTC instant.
Definition DateTime.hpp:403
year_t year() const
Local year component.
Definition DateTime.hpp:298
static bool try_parse_iso8601(const char *str, DateTime &out) noexcept
Try to parse ISO8601 C-string to DateTime.
Definition DateTime.hpp:186
bool operator>=(const DateTime &other) const noexcept
Greater-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:418
static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset=0) noexcept
Create instance from UTC milliseconds.
Definition DateTime.hpp:47
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
Definition DateTime.hpp:248
static DateTime parse_iso8601_buffer(const char *data, std::size_t size)
Definition DateTime.hpp:552
DateTimeStruct to_date_time_struct_local() const
Convert to date-time structure using stored offset.
Definition DateTime.hpp:143
ts_ms_t local_ms() const noexcept
Definition DateTime.hpp:568
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
Definition DateTime.hpp:278
DateTime add_ms(int64_t delta_ms) const noexcept
Add milliseconds to UTC instant.
Definition DateTime.hpp:428
static DateTime parse_iso8601(const char *str)
Parse ISO8601 C-string, throws on failure.
Definition DateTime.hpp:212
static constexpr ts_ms_t offset_to_ms(tz_t offset) noexcept
Definition DateTime.hpp:564
bool is_workday() const noexcept
Check if local date is a workday.
Definition DateTime.hpp:383
bool is_weekend() const noexcept
Check if local date is a weekend.
Definition DateTime.hpp:388
static DateTime from_components(year_t year, int month, int day, int hour=0, int min=0, int sec=0, int ms=0, tz_t offset=0)
Build from calendar components interpreted in provided offset.
Definition DateTime.hpp:67
DateTime to_utc() const noexcept
Return copy with zero offset.
Definition DateTime.hpp:468
DateTime add_seconds(int64_t seconds) const noexcept
Add seconds to UTC instant.
Definition DateTime.hpp:433
DateTime start_of_year() const
Start of local year.
Definition DateTime.hpp:516
std::string format(const char *fmt) const
Format using C-string pattern.
Definition DateTime.hpp:265
TimeZoneStruct time_zone() const
Get timezone structure from offset.
Definition DateTime.hpp:293
DateTime end_of_year() const
End of local year.
Definition DateTime.hpp:523
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
Definition DateTime.hpp:448
Configuration macros for the library.
Header file with time-related constants.
Conversions related to calendar dates and DateStruct helpers.
Conversions involving DateTimeStruct and day boundary helpers.
Header for date and time structure and related functions.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t MS_PER_DAY
Milliseconds per day.
Definition constants.hpp:97
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:77
constexpr int64_t SEC_PER_MIN
Seconds per minute.
IsoWeekDateStruct to_iso_week_date(Y year, M month, D day)
Convert calendar date to ISO week date.
DateStruct iso_week_date_to_date(const IsoWeekDateStruct &iso_date)
Convert ISO week date to calendar date.
TIME_SHIELD_CONSTEXPR T1 weekday_of_date(const T2 &date)
Get the day of the week from a date structure.
TIME_SHIELD_CONSTEXPR int iso_weekday_of_date(Y year, M month, D day)
Get ISO weekday for a calendar date.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
bool parse_iso_week_date(const char *input, std::size_t length, IsoWeekDateStruct &iso_date) noexcept
Parse ISO week date string buffer.
constexpr T days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
const std::string to_iso8601_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string.
const std::string to_mql5_date_time(ts_t ts)
Converts a timestamp to a string in MQL5 date and time format.
const std::string to_iso8601_utc_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string in UTC format.
const std::string to_string_ms(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp in milliseconds to string with custom format.
bool parse_iso8601(const char *input, std::size_t length, DateTimeStruct &dt, TimeZoneStruct &tz) noexcept
Parse ISO8601 character buffer into DateTimeStruct and TimeZoneStruct.
bool is_workday_ms(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (milliseconds precision).
TIME_SHIELD_CONSTEXPR tz_t time_zone_struct_to_offset(const TimeZoneStruct &tz) noexcept
Convert a TimeZoneStruct to a numeric UTC offset (seconds).
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
T to_date_time_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
const DateStruct create_date_struct(int64_t year, int32_t mon=1, int32_t day=1)
Creates a DateStruct instance.
const TimeStruct create_time_struct(int16_t hour, int16_t min, int16_t sec=0, int16_t ms=0)
Creates a TimeStruct instance.
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone_offset(const T &time_zone) noexcept
Check if the time zone is valid.
const DateTimeStruct create_date_time_struct(int64_t year, int mon=1, int day=1, int hour=0, int min=0, int sec=0, int ms=0)
Creates a DateTimeStruct instance.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
constexpr T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
TIME_SHIELD_CONSTEXPR ts_ms_t to_timestamp_ms(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T2 ms=0)
Converts a date-time structure to a timestamp in milliseconds.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:48
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:60
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:49
int64_t year_t
Year as an integer (e.g., 2024).
Definition types.hpp:41
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR bool is_weekend(ts_t ts) noexcept
Alias for is_day_off function.
TIME_SHIELD_CONSTEXPR bool is_valid_date_time(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0) noexcept
Checks the correctness of a date and time.
Conversions and utilities for ISO week dates (ISO 8601).
Main namespace for the Time Shield library.
Structure to represent a date.
int32_t mon
Month component of the date (1-12).
int32_t day
Day component of the date (1-31).
int64_t year
Year component of the date.
Structure to represent date and time.
int ms
Millisecond component of time (0-999)
int hour
Hour component of time (0-23)
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int min
Minute component of time (0-59)
int mon
Month component of the date (1-12).
int sec
Second component of time (0-59)
Structure to represent an ISO week date.
Structure to represent time.
Structure to represent time zone information.
Header file for time formatting utilities.
Header file with functions for parsing dates and times in ISO8601 format and converting them to vario...
Header for time structure and related functions.
Header file with time-related utility functions.
Header for time zone structure and related functions.
Type definitions for time-related units and formats.
Header file with time-related validation functions.