3#ifndef _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
4#define _TIME_SHIELD_DATE_TIME_HPP_INCLUDED
27#ifdef TIME_SHIELD_CPP17
172 #ifdef TIME_SHIELD_CPP17
187 if (str ==
nullptr) {
200 #ifdef TIME_SHIELD_CPP17
213 if (str ==
nullptr) {
214 throw std::invalid_argument(
"Invalid ISO8601 datetime");
227 #ifdef TIME_SHIELD_CPP17
236 if (str ==
nullptr) {
253 std::string
format(
const std::string& fmt)
const {
257 #ifdef TIME_SHIELD_CPP17
259 std::string
format(std::string_view fmt)
const {
265 std::string
format(
const char* fmt)
const {
266 if (fmt ==
nullptr) {
267 return std::string();
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));
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));
399 return !(*
this == other);
538 if (data ==
nullptr) {
555 throw std::invalid_argument(
"Invalid ISO8601 datetime");
static DateTime now_utc(tz_t offset=0) noexcept
Construct instance for current UTC time.
static bool try_parse_iso_week_date(std::string_view str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string_view.
static DateTime parse_iso8601(std::string_view str)
Parse ISO8601 string_view, throws on failure.
DateTime add_hours(int64_t hours) const noexcept
Add hours to UTC instant.
DateStruct date() const
Local date components.
DateTime start_of_day() const
Start of local day.
static bool try_parse_iso_week_date(const std::string &str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string.
tz_t utc_offset() const noexcept
Access stored UTC offset.
int day() const
Local day component.
static bool try_parse_iso_week_date(const char *str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date C-string.
TimeStruct time_of_day() const
Local time-of-day components.
static DateTime from_unix_s(ts_t utc_s, tz_t offset=0) noexcept
Create instance from UTC seconds.
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
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.
DateTime(ts_ms_t utc_ms, tz_t offset) noexcept
bool operator!=(const DateTime &other) const noexcept
Compare inequality by UTC instant.
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
DateTime end_of_day() const
End of local day.
bool same_local(const DateTime &other) const noexcept
Check if local representations match including offset.
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.
bool operator>(const DateTime &other) const noexcept
Greater-than comparison by UTC instant.
int millisecond() const
Local millisecond component.
bool operator==(const DateTime &other) const noexcept
Compare equality by UTC instant.
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.
ts_t unix_s() const noexcept
Access UTC seconds.
DateTime end_of_month() const
End of local month.
std::string format(std::string_view fmt) const
Format using custom string_view pattern.
double diff_seconds(const DateTime &other) const noexcept
Difference in seconds to another DateTime.
static bool try_parse_iso8601(std::string_view str, DateTime &out) noexcept
Try to parse ISO8601 string_view to DateTime.
bool operator<=(const DateTime &other) const noexcept
Less-than-or-equal comparison by UTC instant.
DateTime with_offset(tz_t new_offset) const noexcept
Return copy with new offset preserving instant.
TimeStruct utc_time_of_day() const
UTC time-of-day components.
int hour() const
Local hour component.
static DateTime parse_iso8601(const std::string &str)
Parse ISO8601 string, throws on failure.
static bool try_parse_iso8601_buffer(const char *data, std::size_t size, DateTime &out) noexcept
Weekday weekday() const
Local weekday.
DateTime() noexcept
Default constructor sets epoch with zero offset.
std::string to_mql5_date_time() const
Format to MQL5 date-time string.
int month() const
Local month component.
int minute() const
Local minute component.
DateTimeStruct to_date_time_struct_utc() const
Convert to UTC date-time structure.
std::string format(const std::string &fmt) const
Format using custom pattern.
int iso_weekday() const
Local ISO weekday number (1..7).
DateTime add_minutes(int64_t minutes) const noexcept
Add minutes to UTC instant.
DateTime start_of_month() const
Start of local month.
int64_t diff_ms(const DateTime &other) const noexcept
Difference in milliseconds to another DateTime.
DateStruct utc_date() const
UTC date components.
static DateTime from_date_time_struct(const DateTimeStruct &local_dt, tz_t offset=0)
Build from DateTimeStruct interpreted in provided offset.
int second() const
Local second component.
bool operator<(const DateTime &other) const noexcept
Less-than comparison by UTC instant.
year_t year() const
Local year component.
static bool try_parse_iso8601(const char *str, DateTime &out) noexcept
Try to parse ISO8601 C-string to DateTime.
bool operator>=(const DateTime &other) const noexcept
Greater-than-or-equal comparison by UTC instant.
static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset=0) noexcept
Create instance from UTC milliseconds.
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
static DateTime parse_iso8601_buffer(const char *data, std::size_t size)
DateTimeStruct to_date_time_struct_local() const
Convert to date-time structure using stored offset.
ts_ms_t local_ms() const noexcept
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
DateTime add_ms(int64_t delta_ms) const noexcept
Add milliseconds to UTC instant.
static DateTime parse_iso8601(const char *str)
Parse ISO8601 C-string, throws on failure.
static constexpr ts_ms_t offset_to_ms(tz_t offset) noexcept
bool is_workday() const noexcept
Check if local date is a workday.
bool is_weekend() const noexcept
Check if local date is a weekend.
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.
DateTime to_utc() const noexcept
Return copy with zero offset.
DateTime add_seconds(int64_t seconds) const noexcept
Add seconds to UTC instant.
DateTime start_of_year() const
Start of local year.
std::string format(const char *fmt) const
Format using C-string pattern.
TimeZoneStruct time_zone() const
Get timezone structure from offset.
DateTime end_of_year() const
End of local year.
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
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.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
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.
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.
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
int64_t year_t
Year as an integer (e.g., 2024).
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 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.