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"
20#include "time_zone_struct.hpp"
21#include "types.hpp"
22#include "validation.hpp"
23
24#include <cstdint>
25#include <cstring>
26#include <stdexcept>
27#include <string>
28#ifdef TIME_SHIELD_CPP17
29#include <string_view>
30#endif
31
32namespace time_shield {
33
37 class DateTime {
38 public:
40 DateTime() noexcept
41 : m_utc_ms(0)
42 , m_offset(0) {}
43
48 static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset = 0) noexcept {
49 return DateTime(utc_ms, offset);
50 }
51
56 static DateTime from_unix_s(ts_t utc_s, tz_t offset = 0) noexcept {
57 return DateTime(sec_to_ms(utc_s), offset);
58 }
59
63 static DateTime now_utc(tz_t offset = 0) noexcept {
64 return DateTime(ts_ms(), offset);
65 }
66
70 int month,
71 int day,
72 int hour = 0,
73 int min = 0,
74 int sec = 0,
75 int ms = 0,
76 tz_t offset = 0) {
77 const ts_ms_t local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
78 const ts_ms_t utc_ms = local_ms - offset_to_ms(offset);
79 return DateTime(utc_ms, offset);
80 }
81
95 int month,
96 int day,
97 int hour,
98 int min,
99 int sec,
100 int ms,
101 tz_t offset,
102 DateTime& out) noexcept {
103 if (!is_valid_date_time(year, month, day, hour, min, sec, ms)) {
104 return false;
105 }
106 if (!is_valid_tz_offset(offset)) {
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 if (!is_valid_tz_offset(offset)) {
134 return false;
135 }
136 const ts_ms_t local_ms = dt_to_timestamp_ms(local_dt);
137 out = DateTime(local_ms - offset_to_ms(offset), offset);
138 return true;
139 }
140
145
150
153 const IsoWeekDateStruct& iso,
154 int hour = 0,
155 int min = 0,
156 int sec = 0,
157 int ms = 0,
158 tz_t offset = 0) {
160 return from_components(date.year, date.mon, date.day, hour, min, sec, ms, offset);
161 }
162
167 static bool try_parse_iso8601(const std::string& str, DateTime& out) noexcept {
168 return try_parse_iso8601_buffer(str.data(), str.size(), out);
169 }
170
171 #ifdef TIME_SHIELD_CPP17
176 static bool try_parse_iso8601(std::string_view str, DateTime& out) noexcept {
177 return try_parse_iso8601_buffer(str.data(), str.size(), out);
178 }
179 #endif
180
185 static bool try_parse_iso8601(const char* str, DateTime& out) noexcept {
186 if (str == nullptr) {
187 return false;
188 }
189 return try_parse_iso8601_buffer(str, std::strlen(str), out);
190 }
191
195 static DateTime parse_iso8601(const std::string& str) {
196 return parse_iso8601_buffer(str.data(), str.size());
197 }
198
199 #ifdef TIME_SHIELD_CPP17
203 static DateTime parse_iso8601(std::string_view str) {
204 return parse_iso8601_buffer(str.data(), str.size());
205 }
206 #endif
207
211 static DateTime parse_iso8601(const char* str) {
212 if (str == nullptr) {
213 throw std::invalid_argument("Invalid ISO8601 datetime");
214 }
215 return parse_iso8601_buffer(str, std::strlen(str));
216 }
217
224 static bool try_parse_iso_week_date(const std::string& str, IsoWeekDateStruct& iso) noexcept {
225 return parse_iso_week_date(str.data(), str.size(), iso);
226 }
227
228 #ifdef TIME_SHIELD_CPP17
232 static bool try_parse_iso_week_date(std::string_view str, IsoWeekDateStruct& iso) noexcept {
233 return parse_iso_week_date(str.data(), str.size(), iso);
234 }
235 #endif
236
240 static bool try_parse_iso_week_date(const char* str, IsoWeekDateStruct& iso) noexcept {
241 if (str == nullptr) {
242 return false;
243 }
244 return parse_iso_week_date(str, std::strlen(str), iso);
245 }
246
248 std::string to_iso8601() const {
250 }
251
253 std::string to_iso8601_utc() const {
255 }
256
258 std::string format(const std::string& fmt) const {
259 return to_string_ms(fmt, m_utc_ms, m_offset);
260 }
261
262 #ifdef TIME_SHIELD_CPP17
264 std::string format(std::string_view fmt) const {
265 return to_string_ms(std::string(fmt), m_utc_ms, m_offset);
266 }
267 #endif
268
270 std::string format(const char* fmt) const {
271 if (fmt == nullptr) {
272 return std::string();
273 }
274 return to_string_ms(std::string(fmt), m_utc_ms, m_offset);
275 }
276
281
283 ts_ms_t unix_ms() const noexcept {
284 return m_utc_ms;
285 }
286
288 ts_t unix_s() const noexcept {
290 }
291
293 tz_t utc_offset() const noexcept {
294 return m_offset;
295 }
296
301
303 year_t year() const {
305 }
306
308 int month() const {
310 }
311
313 int day() const {
315 }
316
318 int hour() const {
320 }
321
323 int minute() const {
325 }
326
328 int second() const {
330 }
331
333 int millisecond() const {
335 }
336
338 DateStruct date() const {
339 const DateTimeStruct local_dt = to_date_time_struct_local();
340 return create_date_struct(local_dt.year, local_dt.mon, local_dt.day);
341 }
342
345 const DateTimeStruct local_dt = to_date_time_struct_local();
346 return create_time_struct(
347 static_cast<int16_t>(local_dt.hour),
348 static_cast<int16_t>(local_dt.min),
349 static_cast<int16_t>(local_dt.sec),
350 static_cast<int16_t>(local_dt.ms));
351 }
352
356 return create_date_struct(utc_dt.year, utc_dt.mon, utc_dt.day);
357 }
358
362 return create_time_struct(
363 static_cast<int16_t>(utc_dt.hour),
364 static_cast<int16_t>(utc_dt.min),
365 static_cast<int16_t>(utc_dt.sec),
366 static_cast<int16_t>(utc_dt.ms));
367 }
368
370 year_t utc_year() const {
372 }
373
375 int utc_month() const {
377 }
378
380 int utc_day() const {
382 }
383
385 int utc_hour() const {
387 }
388
390 int utc_minute() const {
392 }
393
395 int utc_second() const {
397 }
398
400 int utc_millisecond() const {
402 }
403
405 Weekday weekday() const {
406 const DateStruct local_date = date();
407 return weekday_of_date<Weekday>(local_date);
408 }
409
411 int iso_weekday() const {
412 const DateStruct local_date = date();
413 return iso_weekday_of_date(local_date.year, local_date.mon, local_date.day);
414 }
415
418 const DateStruct local_date = date();
419 return to_iso_week_date(local_date.year, local_date.mon, local_date.day);
420 }
421
424 const DateStruct utc_dt = utc_date();
425 return weekday_of_date<Weekday>(utc_dt);
426 }
427
429 int utc_iso_weekday() const {
430 const DateStruct utc_dt = utc_date();
431 return iso_weekday_of_date(utc_dt.year, utc_dt.mon, utc_dt.day);
432 }
433
436 const DateStruct utc_dt = utc_date();
437 return to_iso_week_date(utc_dt.year, utc_dt.mon, utc_dt.day);
438 }
439
441 bool is_workday() const noexcept {
442 return is_workday_ms(local_ms());
443 }
444
446 bool is_weekend() const noexcept {
448 }
449
451 bool utc_is_workday() const noexcept {
452 const DateStruct utc_dt = utc_date();
453 return time_shield::is_workday(utc_dt.year, utc_dt.mon, utc_dt.day);
454 }
455
457 bool utc_is_weekend() const noexcept {
459 }
460
462 bool operator==(const DateTime& other) const noexcept {
463 return m_utc_ms == other.m_utc_ms;
464 }
465
467 bool operator!=(const DateTime& other) const noexcept {
468 return !(*this == other);
469 }
470
472 bool operator<(const DateTime& other) const noexcept {
473 return m_utc_ms < other.m_utc_ms;
474 }
475
477 bool operator<=(const DateTime& other) const noexcept {
478 return m_utc_ms <= other.m_utc_ms;
479 }
480
482 bool operator>(const DateTime& other) const noexcept {
483 return m_utc_ms > other.m_utc_ms;
484 }
485
487 bool operator>=(const DateTime& other) const noexcept {
488 return m_utc_ms >= other.m_utc_ms;
489 }
490
492 bool same_local(const DateTime& other) const noexcept {
493 return local_ms() == other.local_ms() && m_offset == other.m_offset;
494 }
495
497 DateTime add_ms(int64_t delta_ms) const noexcept {
498 return DateTime(m_utc_ms + delta_ms, m_offset);
499 }
500
502 DateTime add_seconds(int64_t seconds) const noexcept {
503 return add_ms(sec_to_ms(seconds));
504 }
505
507 DateTime add_minutes(int64_t minutes) const noexcept {
508 return add_ms(sec_to_ms(minutes * SEC_PER_MIN));
509 }
510
512 DateTime add_hours(int64_t hours) const noexcept {
513 return add_ms(sec_to_ms(hours * SEC_PER_HOUR));
514 }
515
517 DateTime add_days(int64_t days) const noexcept {
518 return add_ms(days * MS_PER_DAY);
519 }
520
522 int64_t diff_ms(const DateTime& other) const noexcept {
523 return m_utc_ms - other.m_utc_ms;
524 }
525
527 double diff_seconds(const DateTime& other) const noexcept {
528 return static_cast<double>(diff_ms(other)) / static_cast<double>(MS_PER_SEC);
529 }
530
532 DateTime with_offset(tz_t new_offset) const noexcept {
533 return DateTime(m_utc_ms, new_offset);
534 }
535
537 DateTime to_utc() const noexcept {
538 return with_offset(0);
539 }
540
543 const DateTimeStruct local_dt = to_date_time_struct_local();
544 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, local_dt.day);
545 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
546 }
547
550 const DateTimeStruct local_dt = to_date_time_struct_local();
551 const ts_ms_t local_end_ms = to_timestamp_ms(
552 local_dt.year,
553 local_dt.mon,
554 local_dt.day,
555 23,
556 59,
557 59,
558 static_cast<int>(MS_PER_SEC - 1));
559 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
560 }
561
564 const DateTimeStruct local_dt = to_date_time_struct_local();
565 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, 1);
566 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
567 }
568
571 const DateTimeStruct local_dt = to_date_time_struct_local();
572 const int days = num_days_in_month(local_dt.year, local_dt.mon);
573 const ts_ms_t local_end_ms = to_timestamp_ms(
574 local_dt.year,
575 local_dt.mon,
576 days,
577 23,
578 59,
579 59,
580 static_cast<int>(MS_PER_SEC - 1));
581 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
582 }
583
586 const year_t local_year = year();
587 const ts_ms_t local_start_ms = to_timestamp_ms(local_year, 1, 1);
588 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
589 }
590
593 const year_t local_year = year();
594 const ts_ms_t local_end_ms = to_timestamp_ms(
595 local_year,
596 12,
597 31,
598 23,
599 59,
600 59,
601 static_cast<int>(MS_PER_SEC - 1));
602 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
603 }
604
608 return from_unix_ms(to_timestamp_ms(utc_dt.year, utc_dt.mon, utc_dt.day), m_offset);
609 }
610
614 return from_unix_ms(
616 utc_dt.year,
617 utc_dt.mon,
618 utc_dt.day,
619 23,
620 59,
621 59,
622 static_cast<int>(MS_PER_SEC - 1)),
623 m_offset);
624 }
625
629 return from_unix_ms(to_timestamp_ms(utc_dt.year, utc_dt.mon, 1), m_offset);
630 }
631
635 const int days = num_days_in_month(utc_dt.year, utc_dt.mon);
636 return from_unix_ms(
638 utc_dt.year,
639 utc_dt.mon,
640 days,
641 23,
642 59,
643 59,
644 static_cast<int>(MS_PER_SEC - 1)),
645 m_offset);
646 }
647
650 const year_t utc_year_value = utc_year();
651 return from_unix_ms(to_timestamp_ms(utc_year_value, 1, 1), m_offset);
652 }
653
656 const year_t utc_year_value = utc_year();
657 return from_unix_ms(
659 utc_year_value,
660 12,
661 31,
662 23,
663 59,
664 59,
665 static_cast<int>(MS_PER_SEC - 1)),
666 m_offset);
667 }
668
669 private:
670 static bool try_parse_iso8601_buffer(const char* data, std::size_t size, DateTime& out) noexcept {
671 if (data == nullptr) {
672 return false;
673 }
676 if (!time_shield::parse_iso8601(data, size, dt, tz)) {
677 return false;
678 }
679 const tz_t offset = time_zone_struct_to_offset(tz);
680 if (!is_valid_tz_offset(offset)) {
681 return false;
682 }
683 const ts_ms_t utc_ms = dt_to_timestamp_ms(dt) - offset_to_ms(offset);
684 out = from_unix_ms(utc_ms, offset);
685 return true;
686 }
687
688 static DateTime parse_iso8601_buffer(const char* data, std::size_t size) {
689 DateTime result;
690 if (!try_parse_iso8601_buffer(data, size, result)) {
691 throw std::invalid_argument("Invalid ISO8601 datetime");
692 }
693 return result;
694 }
695
696 DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
697 : m_utc_ms(utc_ms)
698 , m_offset(offset) {}
699
700 static TIME_SHIELD_CONSTEXPR ts_ms_t offset_to_ms(tz_t offset) noexcept {
701 return static_cast<ts_ms_t>(offset) * MS_PER_SEC;
702 }
703
704 ts_ms_t local_ms() const noexcept {
706 }
707
710 };
711
712} // namespace time_shield
713
714#endif // _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
DateTime end_of_utc_day() const
End of UTC day.
Definition DateTime.hpp:612
static DateTime now_utc(tz_t offset=0) noexcept
Construct instance for current UTC time.
Definition DateTime.hpp:63
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:232
static DateTime parse_iso8601(std::string_view str)
Parse ISO8601 string_view, throws on failure.
Definition DateTime.hpp:203
DateTime add_hours(int64_t hours) const noexcept
Add hours to UTC instant.
Definition DateTime.hpp:512
DateStruct date() const
Local date components.
Definition DateTime.hpp:338
DateTime start_of_day() const
Start of local day.
Definition DateTime.hpp:542
static bool try_parse_iso_week_date(const std::string &str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string.
Definition DateTime.hpp:224
tz_t utc_offset() const noexcept
Access stored UTC offset.
Definition DateTime.hpp:293
int day() const
Local day component.
Definition DateTime.hpp:313
static bool try_parse_iso_week_date(const char *str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date C-string.
Definition DateTime.hpp:240
int utc_millisecond() const
UTC millisecond component.
Definition DateTime.hpp:400
TimeStruct time_of_day() const
Local time-of-day components.
Definition DateTime.hpp:344
static DateTime from_unix_s(ts_t utc_s, tz_t offset=0) noexcept
Create instance from UTC seconds.
Definition DateTime.hpp:56
IsoWeekDateStruct utc_iso_week_date() const
UTC ISO week date.
Definition DateTime.hpp:435
Weekday utc_weekday() const
UTC weekday.
Definition DateTime.hpp:423
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
Definition DateTime.hpp:417
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:93
DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
Definition DateTime.hpp:696
bool operator!=(const DateTime &other) const noexcept
Compare inequality by UTC instant.
Definition DateTime.hpp:467
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
Definition DateTime.hpp:167
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
Definition DateTime.hpp:248
DateTime end_of_day() const
End of local day.
Definition DateTime.hpp:549
DateTime end_of_utc_year() const
End of UTC year.
Definition DateTime.hpp:655
DateTime start_of_utc_month() const
Start of UTC month.
Definition DateTime.hpp:627
bool same_local(const DateTime &other) const noexcept
Check if local representations match including offset.
Definition DateTime.hpp:492
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:152
bool operator>(const DateTime &other) const noexcept
Greater-than comparison by UTC instant.
Definition DateTime.hpp:482
int millisecond() const
Local millisecond component.
Definition DateTime.hpp:333
bool operator==(const DateTime &other) const noexcept
Compare equality by UTC instant.
Definition DateTime.hpp:462
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:288
bool utc_is_weekend() const noexcept
Check if UTC date is a weekend.
Definition DateTime.hpp:457
DateTime end_of_month() const
End of local month.
Definition DateTime.hpp:570
std::string format(std::string_view fmt) const
Format using custom string_view pattern.
Definition DateTime.hpp:264
double diff_seconds(const DateTime &other) const noexcept
Difference in seconds to another DateTime.
Definition DateTime.hpp:527
static bool try_parse_iso8601(std::string_view str, DateTime &out) noexcept
Try to parse ISO8601 string_view to DateTime.
Definition DateTime.hpp:176
int utc_second() const
UTC second component.
Definition DateTime.hpp:395
DateTime end_of_utc_month() const
End of UTC month.
Definition DateTime.hpp:633
bool utc_is_workday() const noexcept
Check if UTC date is a workday.
Definition DateTime.hpp:451
bool operator<=(const DateTime &other) const noexcept
Less-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:477
DateTime with_offset(tz_t new_offset) const noexcept
Return copy with new offset preserving instant.
Definition DateTime.hpp:532
TimeStruct utc_time_of_day() const
UTC time-of-day components.
Definition DateTime.hpp:360
int hour() const
Local hour component.
Definition DateTime.hpp:318
static DateTime parse_iso8601(const std::string &str)
Parse ISO8601 string, throws on failure.
Definition DateTime.hpp:195
static bool try_parse_iso8601_buffer(const char *data, std::size_t size, DateTime &out) noexcept
Definition DateTime.hpp:670
Weekday weekday() const
Local weekday.
Definition DateTime.hpp:405
DateTime() noexcept
Default constructor sets epoch with zero offset.
Definition DateTime.hpp:40
std::string to_mql5_date_time() const
Format to MQL5 date-time string.
Definition DateTime.hpp:278
int month() const
Local month component.
Definition DateTime.hpp:308
int minute() const
Local minute component.
Definition DateTime.hpp:323
DateTimeStruct to_date_time_struct_utc() const
Convert to UTC date-time structure.
Definition DateTime.hpp:147
std::string format(const std::string &fmt) const
Format using custom pattern.
Definition DateTime.hpp:258
int iso_weekday() const
Local ISO weekday number (1..7).
Definition DateTime.hpp:411
int utc_day() const
UTC day component.
Definition DateTime.hpp:380
DateTime add_minutes(int64_t minutes) const noexcept
Add minutes to UTC instant.
Definition DateTime.hpp:507
DateTime start_of_month() const
Start of local month.
Definition DateTime.hpp:563
int64_t diff_ms(const DateTime &other) const noexcept
Difference in milliseconds to another DateTime.
Definition DateTime.hpp:522
DateStruct utc_date() const
UTC date components.
Definition DateTime.hpp:354
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:328
bool operator<(const DateTime &other) const noexcept
Less-than comparison by UTC instant.
Definition DateTime.hpp:472
int utc_hour() const
UTC hour component.
Definition DateTime.hpp:385
year_t year() const
Local year component.
Definition DateTime.hpp:303
static bool try_parse_iso8601(const char *str, DateTime &out) noexcept
Try to parse ISO8601 C-string to DateTime.
Definition DateTime.hpp:185
bool operator>=(const DateTime &other) const noexcept
Greater-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:487
static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset=0) noexcept
Create instance from UTC milliseconds.
Definition DateTime.hpp:48
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
Definition DateTime.hpp:253
static DateTime parse_iso8601_buffer(const char *data, std::size_t size)
Definition DateTime.hpp:688
year_t utc_year() const
UTC year component.
Definition DateTime.hpp:370
DateTimeStruct to_date_time_struct_local() const
Convert to date-time structure using stored offset.
Definition DateTime.hpp:142
ts_ms_t local_ms() const noexcept
Definition DateTime.hpp:704
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
Definition DateTime.hpp:283
int utc_month() const
UTC month component.
Definition DateTime.hpp:375
DateTime add_ms(int64_t delta_ms) const noexcept
Add milliseconds to UTC instant.
Definition DateTime.hpp:497
static DateTime parse_iso8601(const char *str)
Parse ISO8601 C-string, throws on failure.
Definition DateTime.hpp:211
int utc_minute() const
UTC minute component.
Definition DateTime.hpp:390
bool is_workday() const noexcept
Check if local date is a workday.
Definition DateTime.hpp:441
int utc_iso_weekday() const
UTC ISO weekday number (1..7).
Definition DateTime.hpp:429
bool is_weekend() const noexcept
Check if local date is a weekend.
Definition DateTime.hpp:446
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:68
DateTime to_utc() const noexcept
Return copy with zero offset.
Definition DateTime.hpp:537
DateTime add_seconds(int64_t seconds) const noexcept
Add seconds to UTC instant.
Definition DateTime.hpp:502
DateTime start_of_utc_year() const
Start of UTC year.
Definition DateTime.hpp:649
DateTime start_of_year() const
Start of local year.
Definition DateTime.hpp:585
std::string format(const char *fmt) const
Format using C-string pattern.
Definition DateTime.hpp:270
TimeZoneStruct time_zone() const
Get timezone structure from offset.
Definition DateTime.hpp:298
DateTime start_of_utc_day() const
Start of UTC day.
Definition DateTime.hpp:606
DateTime end_of_year() const
End of local year.
Definition DateTime.hpp:592
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
Definition DateTime.hpp:517
static TIME_SHIELD_CONSTEXPR ts_ms_t offset_to_ms(tz_t offset) noexcept
Definition DateTime.hpp:700
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.
TIME_SHIELD_CONSTEXPR T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR bool is_valid_tz_offset(tz_t off) noexcept
Check if a numeric offset is within supported bounds.
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.
TIME_SHIELD_CONSTEXPR T days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
bool parse_iso_week_date(const char *input, std::size_t length, IsoWeekDateStruct &iso_date) noexcept
Parse ISO week date string buffer.
TIME_SHIELD_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(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (seconds precision).
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 ts_ms_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
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 T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
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:49
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:61
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:50
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.
Conversions between numeric offsets and TimeZoneStruct.
Header for time zone structure and related functions.
Type definitions for time-related units and formats.
Header file with time-related validation functions.