Time Shield Library 2.0.0
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_HEADER_DATETIME_DATETIME_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_DATETIME_DATETIME_HPP_INCLUDED
5
8
11#include <time_shield/text.hpp>
12
13#include <cstdint>
14#include <cstring>
15#include <stdexcept>
16#include <string>
17#ifdef TIME_SHIELD_CPP17
18#include <string_view>
19#endif
20
21namespace time_shield {
22
26 class DateTime {
27 public:
29 DateTime() noexcept
30 : m_utc_ms(0)
31 , m_offset(0) {}
32
37 static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset = 0) noexcept {
38 return DateTime(utc_ms, offset);
39 }
40
45 static DateTime from_unix_s(ts_t utc_s, tz_t offset = 0) noexcept {
46 return DateTime(sec_to_ms(utc_s), offset);
47 }
48
52 static DateTime now_utc(tz_t offset = 0) noexcept {
53 return DateTime(ts_ms(), offset);
54 }
55
59 int month,
60 int day,
61 int hour = 0,
62 int min = 0,
63 int sec = 0,
64 int ms = 0,
65 tz_t offset = 0) {
66 const ts_ms_t local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
67 const ts_ms_t utc_ms = local_ms - offset_to_ms(offset);
68 return DateTime(utc_ms, offset);
69 }
70
84 int month,
85 int day,
86 int hour,
87 int min,
88 int sec,
89 int ms,
90 tz_t offset,
91 DateTime& out) noexcept {
92 if (!is_valid_date_time(year, month, day, hour, min, sec, ms)) {
93 return false;
94 }
95 if (!is_valid_tz_offset(offset)) {
96 return false;
97 }
98 const ts_ms_t local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
99 out = DateTime(local_ms - offset_to_ms(offset), offset);
100 return true;
101 }
102
104 static DateTime from_date_time_struct(const DateTimeStruct& local_dt, tz_t offset = 0) {
105 const ts_ms_t local_ms = dt_to_timestamp_ms(local_dt);
106 const ts_ms_t utc_ms = local_ms - offset_to_ms(offset);
107 return DateTime(utc_ms, offset);
108 }
109
116 const DateTimeStruct& local_dt,
117 tz_t offset,
118 DateTime& out) noexcept {
119 if (!is_valid_date_time(local_dt)) {
120 return false;
121 }
122 if (!is_valid_tz_offset(offset)) {
123 return false;
124 }
125 const ts_ms_t local_ms = dt_to_timestamp_ms(local_dt);
126 out = DateTime(local_ms - offset_to_ms(offset), offset);
127 return true;
128 }
129
134
139
142 const IsoWeekDateStruct& iso,
143 int hour = 0,
144 int min = 0,
145 int sec = 0,
146 int ms = 0,
147 tz_t offset = 0) {
149 return from_components(date.year, date.mon, date.day, hour, min, sec, ms, offset);
150 }
151
156 static bool try_parse_iso8601(const std::string& str, DateTime& out) noexcept {
157 return try_parse_iso8601_buffer(str.data(), str.size(), out);
158 }
159
160 #ifdef TIME_SHIELD_CPP17
165 static bool try_parse_iso8601(std::string_view str, DateTime& out) noexcept {
166 return try_parse_iso8601_buffer(str.data(), str.size(), out);
167 }
168 #endif
169
174 static bool try_parse_iso8601(const char* str, DateTime& out) noexcept {
175 if (str == nullptr) {
176 return false;
177 }
178 return try_parse_iso8601_buffer(str, std::strlen(str), out);
179 }
180
184 static DateTime parse_iso8601(const std::string& str) {
185 return parse_iso8601_buffer(str.data(), str.size());
186 }
187
188 #ifdef TIME_SHIELD_CPP17
192 static DateTime parse_iso8601(std::string_view str) {
193 return parse_iso8601_buffer(str.data(), str.size());
194 }
195 #endif
196
200 static DateTime parse_iso8601(const char* str) {
201 if (str == nullptr) {
202 throw std::invalid_argument("Invalid ISO8601 datetime");
203 }
204 return parse_iso8601_buffer(str, std::strlen(str));
205 }
206
213 static bool try_parse_iso_week_date(const std::string& str, IsoWeekDateStruct& iso) noexcept {
214 return parse_iso_week_date(str.data(), str.size(), iso);
215 }
216
217 #ifdef TIME_SHIELD_CPP17
221 static bool try_parse_iso_week_date(std::string_view str, IsoWeekDateStruct& iso) noexcept {
222 return parse_iso_week_date(str.data(), str.size(), iso);
223 }
224 #endif
225
229 static bool try_parse_iso_week_date(const char* str, IsoWeekDateStruct& iso) noexcept {
230 if (str == nullptr) {
231 return false;
232 }
233 return parse_iso_week_date(str, std::strlen(str), iso);
234 }
235
237 std::string to_iso8601() const {
239 }
240
242 std::string to_iso8601_utc() const {
244 }
245
247 std::string format(const std::string& fmt) const {
248 return to_string_ms(fmt, m_utc_ms, m_offset);
249 }
250
251 #ifdef TIME_SHIELD_CPP17
253 std::string format(std::string_view fmt) const {
254 return to_string_ms(std::string(fmt), m_utc_ms, m_offset);
255 }
256 #endif
257
259 std::string format(const char* fmt) const {
260 if (fmt == nullptr) {
261 return std::string();
262 }
263 return to_string_ms(std::string(fmt), m_utc_ms, m_offset);
264 }
265
270
272 ts_ms_t unix_ms() const noexcept {
273 return m_utc_ms;
274 }
275
277 ts_t unix_s() const noexcept {
279 }
280
282 tz_t utc_offset() const noexcept {
283 return m_offset;
284 }
285
290
292 year_t year() const {
294 }
295
297 int month() const {
299 }
300
302 int day() const {
304 }
305
307 int hour() const {
309 }
310
312 int minute() const {
314 }
315
317 int second() const {
319 }
320
322 int millisecond() const {
324 }
325
327 DateStruct date() const {
328 const DateTimeStruct local_dt = to_date_time_struct_local();
329 return create_date_struct(local_dt.year, local_dt.mon, local_dt.day);
330 }
331
334 const DateTimeStruct local_dt = to_date_time_struct_local();
335 return create_time_struct(
336 static_cast<int16_t>(local_dt.hour),
337 static_cast<int16_t>(local_dt.min),
338 static_cast<int16_t>(local_dt.sec),
339 static_cast<int16_t>(local_dt.ms));
340 }
341
345 return create_date_struct(utc_dt.year, utc_dt.mon, utc_dt.day);
346 }
347
351 return create_time_struct(
352 static_cast<int16_t>(utc_dt.hour),
353 static_cast<int16_t>(utc_dt.min),
354 static_cast<int16_t>(utc_dt.sec),
355 static_cast<int16_t>(utc_dt.ms));
356 }
357
359 year_t utc_year() const {
361 }
362
364 int utc_month() const {
366 }
367
369 int utc_day() const {
371 }
372
374 int utc_hour() const {
376 }
377
379 int utc_minute() const {
381 }
382
384 int utc_second() const {
386 }
387
389 int utc_millisecond() const {
391 }
392
394 Weekday weekday() const {
395 const DateStruct local_date = date();
396 return weekday_of_date<Weekday>(local_date);
397 }
398
400 int iso_weekday() const {
401 const DateStruct local_date = date();
402 return iso_weekday_of_date(local_date.year, local_date.mon, local_date.day);
403 }
404
407 const DateStruct local_date = date();
408 return to_iso_week_date(local_date.year, local_date.mon, local_date.day);
409 }
410
413 const DateStruct utc_dt = utc_date();
414 return weekday_of_date<Weekday>(utc_dt);
415 }
416
418 int utc_iso_weekday() const {
419 const DateStruct utc_dt = utc_date();
420 return iso_weekday_of_date(utc_dt.year, utc_dt.mon, utc_dt.day);
421 }
422
425 const DateStruct utc_dt = utc_date();
426 return to_iso_week_date(utc_dt.year, utc_dt.mon, utc_dt.day);
427 }
428
430 bool is_workday() const noexcept {
431 return is_workday_ms(local_ms());
432 }
433
435 bool is_weekend() const noexcept {
437 }
438
440 bool utc_is_workday() const noexcept {
441 const DateStruct utc_dt = utc_date();
442 return time_shield::is_workday(utc_dt.year, utc_dt.mon, utc_dt.day);
443 }
444
446 bool utc_is_weekend() const noexcept {
448 }
449
451 bool operator==(const DateTime& other) const noexcept {
452 return m_utc_ms == other.m_utc_ms;
453 }
454
456 bool operator!=(const DateTime& other) const noexcept {
457 return !(*this == other);
458 }
459
461 bool operator<(const DateTime& other) const noexcept {
462 return m_utc_ms < other.m_utc_ms;
463 }
464
466 bool operator<=(const DateTime& other) const noexcept {
467 return m_utc_ms <= other.m_utc_ms;
468 }
469
471 bool operator>(const DateTime& other) const noexcept {
472 return m_utc_ms > other.m_utc_ms;
473 }
474
476 bool operator>=(const DateTime& other) const noexcept {
477 return m_utc_ms >= other.m_utc_ms;
478 }
479
481 bool same_local(const DateTime& other) const noexcept {
482 return local_ms() == other.local_ms() && m_offset == other.m_offset;
483 }
484
486 DateTime add_ms(int64_t delta_ms) const noexcept {
487 return DateTime(m_utc_ms + delta_ms, m_offset);
488 }
489
491 DateTime add_seconds(int64_t seconds) const noexcept {
492 return add_ms(sec_to_ms(seconds));
493 }
494
496 DateTime add_minutes(int64_t minutes) const noexcept {
497 return add_ms(sec_to_ms(minutes * SEC_PER_MIN));
498 }
499
501 DateTime add_hours(int64_t hours) const noexcept {
502 return add_ms(sec_to_ms(hours * SEC_PER_HOUR));
503 }
504
506 DateTime add_days(int64_t days) const noexcept {
507 return add_ms(days * MS_PER_DAY);
508 }
509
511 int64_t diff_ms(const DateTime& other) const noexcept {
512 return m_utc_ms - other.m_utc_ms;
513 }
514
516 double diff_seconds(const DateTime& other) const noexcept {
517 return static_cast<double>(diff_ms(other)) / static_cast<double>(MS_PER_SEC);
518 }
519
521 DateTime with_offset(tz_t new_offset) const noexcept {
522 return DateTime(m_utc_ms, new_offset);
523 }
524
526 DateTime to_utc() const noexcept {
527 return with_offset(0);
528 }
529
532 const DateTimeStruct local_dt = to_date_time_struct_local();
533 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, local_dt.day);
534 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
535 }
536
539 const DateTimeStruct local_dt = to_date_time_struct_local();
540 const ts_ms_t local_end_ms = to_timestamp_ms(
541 local_dt.year,
542 local_dt.mon,
543 local_dt.day,
544 23,
545 59,
546 59,
547 static_cast<int>(MS_PER_SEC - 1));
548 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
549 }
550
553 const DateTimeStruct local_dt = to_date_time_struct_local();
554 const ts_ms_t local_start_ms = to_timestamp_ms(local_dt.year, local_dt.mon, 1);
555 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
556 }
557
560 const DateTimeStruct local_dt = to_date_time_struct_local();
561 const int days = num_days_in_month(local_dt.year, local_dt.mon);
562 const ts_ms_t local_end_ms = to_timestamp_ms(
563 local_dt.year,
564 local_dt.mon,
565 days,
566 23,
567 59,
568 59,
569 static_cast<int>(MS_PER_SEC - 1));
570 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
571 }
572
575 const year_t local_year = year();
576 const ts_ms_t local_start_ms = to_timestamp_ms(local_year, 1, 1);
577 return from_unix_ms(local_start_ms - offset_to_ms(m_offset), m_offset);
578 }
579
582 const year_t local_year = year();
583 const ts_ms_t local_end_ms = to_timestamp_ms(
584 local_year,
585 12,
586 31,
587 23,
588 59,
589 59,
590 static_cast<int>(MS_PER_SEC - 1));
591 return from_unix_ms(local_end_ms - offset_to_ms(m_offset), m_offset);
592 }
593
597 return from_unix_ms(to_timestamp_ms(utc_dt.year, utc_dt.mon, utc_dt.day), m_offset);
598 }
599
603 return from_unix_ms(
605 utc_dt.year,
606 utc_dt.mon,
607 utc_dt.day,
608 23,
609 59,
610 59,
611 static_cast<int>(MS_PER_SEC - 1)),
612 m_offset);
613 }
614
618 return from_unix_ms(to_timestamp_ms(utc_dt.year, utc_dt.mon, 1), m_offset);
619 }
620
624 const int days = num_days_in_month(utc_dt.year, utc_dt.mon);
625 return from_unix_ms(
627 utc_dt.year,
628 utc_dt.mon,
629 days,
630 23,
631 59,
632 59,
633 static_cast<int>(MS_PER_SEC - 1)),
634 m_offset);
635 }
636
639 const year_t utc_year_value = utc_year();
640 return from_unix_ms(to_timestamp_ms(utc_year_value, 1, 1), m_offset);
641 }
642
645 const year_t utc_year_value = utc_year();
646 return from_unix_ms(
648 utc_year_value,
649 12,
650 31,
651 23,
652 59,
653 59,
654 static_cast<int>(MS_PER_SEC - 1)),
655 m_offset);
656 }
657
658 private:
659 static bool try_parse_iso8601_buffer(const char* data, std::size_t size, DateTime& out) noexcept {
660 if (data == nullptr) {
661 return false;
662 }
665 if (!time_shield::parse_iso8601(data, size, dt, tz)) {
666 return false;
667 }
668 const tz_t offset = time_zone_struct_to_offset(tz);
669 if (!is_valid_tz_offset(offset)) {
670 return false;
671 }
672 const ts_ms_t utc_ms = dt_to_timestamp_ms(dt) - offset_to_ms(offset);
673 out = from_unix_ms(utc_ms, offset);
674 return true;
675 }
676
677 static DateTime parse_iso8601_buffer(const char* data, std::size_t size) {
678 DateTime result;
679 if (!try_parse_iso8601_buffer(data, size, result)) {
680 throw std::invalid_argument("Invalid ISO8601 datetime");
681 }
682 return result;
683 }
684
685 DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
686 : m_utc_ms(utc_ms)
687 , m_offset(offset) {}
688
689 static TIME_SHIELD_CONSTEXPR ts_ms_t offset_to_ms(tz_t offset) noexcept {
690 return static_cast<ts_ms_t>(offset) * MS_PER_SEC;
691 }
692
693 ts_ms_t local_ms() const noexcept {
695 }
696
699 };
700
701} // namespace time_shield
702
703#endif // TIME_SHIELD_HEADER_DATETIME_DATETIME_HPP_INCLUDED
Represents a moment in time with optional fixed UTC offset.
Definition DateTime.mqh:27
DateTime end_of_utc_day() const
End of UTC day.
Definition DateTime.hpp:601
static DateTime now_utc(tz_t offset=0) noexcept
Construct instance for current UTC time.
Definition DateTime.hpp:52
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:221
static DateTime parse_iso8601(std::string_view str)
Parse ISO8601 string_view, throws on failure.
Definition DateTime.hpp:192
DateTime add_hours(int64_t hours) const noexcept
Add hours to UTC instant.
Definition DateTime.hpp:501
DateStruct date() const
Local date components.
Definition DateTime.hpp:327
DateTime start_of_day() const
Start of local day.
Definition DateTime.hpp:531
static bool try_parse_iso_week_date(const std::string &str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string.
Definition DateTime.hpp:213
tz_t utc_offset() const noexcept
Access stored UTC offset.
Definition DateTime.hpp:282
int day() const
Local day component.
Definition DateTime.hpp:302
static bool try_parse_iso_week_date(const char *str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date C-string.
Definition DateTime.hpp:229
int utc_millisecond() const
UTC millisecond component.
Definition DateTime.hpp:389
TimeStruct time_of_day() const
Local time-of-day components.
Definition DateTime.hpp:333
static DateTime from_unix_s(ts_t utc_s, tz_t offset=0) noexcept
Create instance from UTC seconds.
Definition DateTime.hpp:45
IsoWeekDateStruct utc_iso_week_date() const
UTC ISO week date.
Definition DateTime.hpp:424
Weekday utc_weekday() const
UTC weekday.
Definition DateTime.hpp:412
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
Definition DateTime.hpp:406
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:82
DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
Definition DateTime.hpp:685
bool operator!=(const DateTime &other) const noexcept
Compare inequality by UTC instant.
Definition DateTime.hpp:456
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
Definition DateTime.hpp:156
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
Definition DateTime.hpp:237
DateTime end_of_day() const
End of local day.
Definition DateTime.hpp:538
DateTime end_of_utc_year() const
End of UTC year.
Definition DateTime.hpp:644
DateTime start_of_utc_month() const
Start of UTC month.
Definition DateTime.hpp:616
bool same_local(const DateTime &other) const noexcept
Check if local representations match including offset.
Definition DateTime.hpp:481
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:141
bool operator>(const DateTime &other) const noexcept
Greater-than comparison by UTC instant.
Definition DateTime.hpp:471
int millisecond() const
Local millisecond component.
Definition DateTime.hpp:322
bool operator==(const DateTime &other) const noexcept
Compare equality by UTC instant.
Definition DateTime.hpp:451
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:115
ts_t unix_s() const noexcept
Access UTC seconds.
Definition DateTime.hpp:277
bool utc_is_weekend() const noexcept
Check if UTC date is a weekend.
Definition DateTime.hpp:446
DateTime end_of_month() const
End of local month.
Definition DateTime.hpp:559
std::string format(std::string_view fmt) const
Format using custom string_view pattern.
Definition DateTime.hpp:253
double diff_seconds(const DateTime &other) const noexcept
Difference in seconds to another DateTime.
Definition DateTime.hpp:516
static bool try_parse_iso8601(std::string_view str, DateTime &out) noexcept
Try to parse ISO8601 string_view to DateTime.
Definition DateTime.hpp:165
int utc_second() const
UTC second component.
Definition DateTime.hpp:384
DateTime end_of_utc_month() const
End of UTC month.
Definition DateTime.hpp:622
bool utc_is_workday() const noexcept
Check if UTC date is a workday.
Definition DateTime.hpp:440
bool operator<=(const DateTime &other) const noexcept
Less-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:466
DateTime with_offset(tz_t new_offset) const noexcept
Return copy with new offset preserving instant.
Definition DateTime.hpp:521
TimeStruct utc_time_of_day() const
UTC time-of-day components.
Definition DateTime.hpp:349
int hour() const
Local hour component.
Definition DateTime.hpp:307
static DateTime parse_iso8601(const std::string &str)
Parse ISO8601 string, throws on failure.
Definition DateTime.hpp:184
static bool try_parse_iso8601_buffer(const char *data, std::size_t size, DateTime &out) noexcept
Definition DateTime.hpp:659
Weekday weekday() const
Local weekday.
Definition DateTime.hpp:394
DateTime() noexcept
Default constructor sets epoch with zero offset.
Definition DateTime.hpp:29
std::string to_mql5_date_time() const
Format to MQL5 date-time string.
Definition DateTime.hpp:267
int month() const
Local month component.
Definition DateTime.hpp:297
int minute() const
Local minute component.
Definition DateTime.hpp:312
DateTimeStruct to_date_time_struct_utc() const
Convert to UTC date-time structure.
Definition DateTime.hpp:136
std::string format(const std::string &fmt) const
Format using custom pattern.
Definition DateTime.hpp:247
int iso_weekday() const
Local ISO weekday number (1..7).
Definition DateTime.hpp:400
int utc_day() const
UTC day component.
Definition DateTime.hpp:369
DateTime add_minutes(int64_t minutes) const noexcept
Add minutes to UTC instant.
Definition DateTime.hpp:496
DateTime start_of_month() const
Start of local month.
Definition DateTime.hpp:552
int64_t diff_ms(const DateTime &other) const noexcept
Difference in milliseconds to another DateTime.
Definition DateTime.hpp:511
DateStruct utc_date() const
UTC date components.
Definition DateTime.hpp:343
static DateTime from_date_time_struct(const DateTimeStruct &local_dt, tz_t offset=0)
Build from DateTimeStruct interpreted in provided offset.
Definition DateTime.hpp:104
int second() const
Local second component.
Definition DateTime.hpp:317
bool operator<(const DateTime &other) const noexcept
Less-than comparison by UTC instant.
Definition DateTime.hpp:461
int utc_hour() const
UTC hour component.
Definition DateTime.hpp:374
year_t year() const
Local year component.
Definition DateTime.hpp:292
static bool try_parse_iso8601(const char *str, DateTime &out) noexcept
Try to parse ISO8601 C-string to DateTime.
Definition DateTime.hpp:174
bool operator>=(const DateTime &other) const noexcept
Greater-than-or-equal comparison by UTC instant.
Definition DateTime.hpp:476
static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset=0) noexcept
Create instance from UTC milliseconds.
Definition DateTime.hpp:37
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
Definition DateTime.hpp:242
static DateTime parse_iso8601_buffer(const char *data, std::size_t size)
Definition DateTime.hpp:677
year_t utc_year() const
UTC year component.
Definition DateTime.hpp:359
DateTimeStruct to_date_time_struct_local() const
Convert to date-time structure using stored offset.
Definition DateTime.hpp:131
ts_ms_t local_ms() const noexcept
Definition DateTime.hpp:693
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
Definition DateTime.hpp:272
int utc_month() const
UTC month component.
Definition DateTime.hpp:364
DateTime add_ms(int64_t delta_ms) const noexcept
Add milliseconds to UTC instant.
Definition DateTime.hpp:486
static DateTime parse_iso8601(const char *str)
Parse ISO8601 C-string, throws on failure.
Definition DateTime.hpp:200
int utc_minute() const
UTC minute component.
Definition DateTime.hpp:379
bool is_workday() const noexcept
Check if local date is a workday.
Definition DateTime.hpp:430
int utc_iso_weekday() const
UTC ISO weekday number (1..7).
Definition DateTime.hpp:418
bool is_weekend() const noexcept
Check if local date is a weekend.
Definition DateTime.hpp:435
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:57
DateTime to_utc() const noexcept
Return copy with zero offset.
Definition DateTime.hpp:526
DateTime add_seconds(int64_t seconds) const noexcept
Add seconds to UTC instant.
Definition DateTime.hpp:491
DateTime start_of_utc_year() const
Start of UTC year.
Definition DateTime.hpp:638
DateTime start_of_year() const
Start of local year.
Definition DateTime.hpp:574
std::string format(const char *fmt) const
Format using C-string pattern.
Definition DateTime.hpp:259
TimeZoneStruct time_zone() const
Get timezone structure from offset.
Definition DateTime.hpp:287
DateTime start_of_utc_day() const
Start of UTC day.
Definition DateTime.hpp:595
DateTime end_of_year() const
End of local year.
Definition DateTime.hpp:581
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
Definition DateTime.hpp:506
static TIME_SHIELD_CONSTEXPR ts_ms_t offset_to_ms(tz_t offset) noexcept
Definition DateTime.hpp:689
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.
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: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:40
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.
TIME_SHIELD_CONSTEXPR bool is_workday(ts_t ts) noexcept
Check if a given timestamp corresponds to a workday (Monday to Friday).
TIME_SHIELD_CONSTEXPR bool is_workday_ms(ts_ms_t ts_ms) noexcept
Check if a given timestamp in milliseconds corresponds to a workday (Monday to Friday).
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.