2#ifndef _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
3#define _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
63 template<
class T = Month>
65 if (month.empty())
throw std::invalid_argument(
"Invalid month name");
67 std::string month_copy = month;
68 std::transform(month_copy.begin(), month_copy.end(), month_copy.begin(), [](
char &ch) {
69 return std::use_facet<std::ctype<char>>(std::locale()).tolower(ch);
71 month_copy[0] = toupper(month_copy[0]);
73 static const std::array<std::string, 12> short_names = {
74 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
75 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
77 static const std::array<std::string, 12> full_names = {
78 "January",
"February",
"March",
"April",
"May",
"June",
79 "July",
"August",
"September",
"October",
"November",
"December"
83 if (month == short_names[i] || month == full_names[i]) {
84 return static_cast<T
>(i + 1);
87 throw std::invalid_argument(
"Invalid month name");
92 template<
class T = Month>
104 template<
class T = Month>
106 if (month.empty())
return false;
108 std::string month_copy = month;
109 std::transform(month_copy.begin(), month_copy.end(), month_copy.begin(), [](
char &ch) {
110 return std::use_facet<std::ctype<char>>(std::locale()).tolower(ch);
112 month_copy[0] = toupper(month_copy[0]);
114 static const std::array<std::string, MONTHS_PER_YEAR> short_names = {
115 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
116 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
118 static const std::array<std::string, MONTHS_PER_YEAR> full_names = {
119 "January",
"February",
"March",
"April",
"May",
"June",
120 "July",
"August",
"September",
"October",
"November",
"December"
124 if (month_copy == short_names[i] || month_copy == full_names[i]) {
125 value =
static_cast<T
>(i + 1);
134 template<
class T = Month>
141 template<
class T = Month>
155 if (tz_str.empty()) {
168 tz.
hour = std::stoi(tz_str.substr(1, 2));
169 tz.
min = std::stoi(tz_str.substr(4, 2));
189 const std::string& input,
193 static const std::regex date_regex(R
"((\d{4})[-\/\.](\d{2})[-\/\.](\d{2}))");
194 std::smatch date_match;
197 static const std::regex time_regex(R
"((\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})?)");
198 std::smatch time_match;
204 if (std::regex_search(input, date_match, date_regex)) {
205 dt.
year = std::stoll(date_match[1].str());
206 dt.
mon = std::stoi(date_match[2].str());
207 dt.
day = std::stoi(date_match[3].str());
213 if (std::regex_search(input, time_match, time_regex)) {
214 dt.
hour = std::stoi(time_match[1].str());
215 dt.
min = std::stoi(time_match[2].str());
216 dt.
sec = std::stoi(time_match[3].str());
217 if (time_match[4].matched) {
218 dt.
ms = std::stoi(time_match[4].str());
220 if (time_match[5].matched) {
322 template<
class T =
int>
324 if (str.empty())
return false;
326 const char* p = str.c_str();
327 int parts[3] = {0, 0, 0};
330 while (*p && idx < 3) {
333 bool has_digit =
false;
335 while (*p >=
'0' && *p <=
'9') {
337 value = value * 10 + (*p -
'0');
341 if (!has_digit)
return false;
342 parts[idx++] = value;
347 }
else if (*p ==
'\0') {
354 if (idx == 0)
return false;
355 if (!
is_valid_time(parts[0], parts[1], parts[2]))
return false;
357 sec =
static_cast<T
>(
sec_of_day(parts[0], parts[1], parts[2]));
371 template<
class T =
int>
Header file with time-related constants.
Header for date and time structure and related functions.
Header file with enumerations for weekdays, months, and other time-related categories.
const int64_t MONTHS_PER_YEAR
Months per year.
constexpr int64_t SEC_PER_DAY
Seconds per day.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp_ms(const T &date_time)
Alias for dt_to_timestamp_ms function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR const T month_of_year(ts_t ts) noexcept
Get the month of the year.
constexpr fts_t to_ftimestamp(const T &date_time)
Alias for dt_to_ftimestamp.
constexpr const T sec_of_day(ts_t ts=ts()) noexcept
Get the second of the day.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp(const T &date_time)
Alias for dt_to_timestamp function.
bool str_to_ts_ms(const std::string &str, ts_ms_t &ts)
Convert an ISO8601 string to a millisecond timestamp (ts_ms_t).
bool parse_iso8601(const std::string &input, DateTimeStruct &dt, TimeZoneStruct &tz)
Parse a date and time string in ISO8601 format.
bool try_get_month_number(const std::string &month, T &value)
Get the month number by name, with output parameter.
T get_month_number(const std::string &month)
Get the month number by name.
bool str_to_ts(const std::string &str, ts_t &ts)
Convert an ISO8601 string to a timestamp (ts_t).
bool str_to_fts(const std::string &str, fts_t &ts)
Convert an ISO8601 string to a floating-point timestamp (fts_t).
bool parse_time_zone(const std::string &tz_str, TimeZoneStruct &tz)
Parse a time zone string into a TimeZoneStruct.
bool parse_tz(const std::string &tz_str, TimeZoneStruct &tz)
Alias for parse_time_zone function.
const tz_t to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
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.
const TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
double fts_t
Floating-point timestamp (fractional seconds since epoch).
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone(T hour, T min) noexcept
Check if the time zone is valid.
TIME_SHIELD_CONSTEXPR bool is_valid_time(T1 hour, T1 min, T1 sec, T2 ms=0) noexcept
Checks the correctness of the specified time.
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.
Main namespace for the Time Shield library.
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 time zone information.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
bool is_positive
True if the time zone offset is positive, false if negative.
Header file for time conversion functions.
Header for time zone structure and related functions.
Header file with time-related validation functions.