3#ifndef TIME_SHIELD_HEADER_DATETIME_DATETIME_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_DATETIME_DATETIME_HPP_INCLUDED
17#ifdef TIME_SHIELD_CPP17
160 #ifdef TIME_SHIELD_CPP17
175 if (str ==
nullptr) {
188 #ifdef TIME_SHIELD_CPP17
201 if (str ==
nullptr) {
202 throw std::invalid_argument(
"Invalid ISO8601 datetime");
217 #ifdef TIME_SHIELD_CPP17
230 if (str ==
nullptr) {
247 std::string
format(
const std::string& fmt)
const {
251 #ifdef TIME_SHIELD_CPP17
253 std::string
format(std::string_view fmt)
const {
259 std::string
format(
const char* fmt)
const {
260 if (fmt ==
nullptr) {
261 return std::string();
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));
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));
457 return !(*
this == other);
660 if (data ==
nullptr) {
680 throw std::invalid_argument(
"Invalid ISO8601 datetime");
Represents a moment in time with optional fixed UTC offset.
DateTime end_of_utc_day() const
End of UTC day.
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.
int utc_millisecond() const
UTC millisecond component.
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 utc_iso_week_date() const
UTC ISO week date.
Weekday utc_weekday() const
UTC weekday.
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.
DateTime end_of_utc_year() const
End of UTC year.
DateTime start_of_utc_month() const
Start of UTC month.
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.
bool utc_is_weekend() const noexcept
Check if UTC date is a weekend.
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.
int utc_second() const
UTC second component.
DateTime end_of_utc_month() const
End of UTC month.
bool utc_is_workday() const noexcept
Check if UTC date is a workday.
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).
int utc_day() const
UTC day component.
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.
int utc_hour() const
UTC hour component.
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)
year_t utc_year() const
UTC year component.
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.
int utc_month() const
UTC month component.
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.
int utc_minute() const
UTC minute component.
bool is_workday() const noexcept
Check if local date is a workday.
int utc_iso_weekday() const
UTC ISO weekday number (1..7).
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_utc_year() const
Start of UTC year.
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 start_of_utc_day() const
Start of UTC day.
DateTime end_of_year() const
End of local year.
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
static TIME_SHIELD_CONSTEXPR ts_ms_t offset_to_ms(tz_t offset) noexcept
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.
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.
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.
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.
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.