7#ifndef _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
8#define _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
61 template<
class T = Month>
63 if (month.empty())
throw std::invalid_argument(
"Invalid month name");
65 std::string month_copy = month;
66 std::transform(month_copy .begin(), month_copy .end(), month_copy .begin(), [](
char &ch) {
67 return std::use_facet<std::ctype<char>>(std::locale()).tolower(ch);
69 month_copy [0] = toupper(month_copy [0]);
71 static const std::array<std::string, 12> short_names = {
72 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
73 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
75 static const std::array<std::string, 12> full_names = {
76 "January",
"February",
"March",
"April",
"May",
"June",
77 "July",
"August",
"September",
"October",
"November",
"December"
81 if (month == short_names[i] || month == full_names[i]) {
82 return static_cast<T
>(i + 1);
85 throw std::invalid_argument(
"Invalid month name");
90 template<
class T = Month>
102 template<
class T = Month>
104 if (month.empty())
return false;
106 std::string month_copy = month;
107 std::transform(month_copy.begin(), month_copy.end(), month_copy.begin(), [](
char &ch) {
108 return std::use_facet<std::ctype<char>>(std::locale()).tolower(ch);
110 month_copy[0] = toupper(month_copy[0]);
112 static const std::array<std::string, MONTHS_PER_YEAR> short_names = {
113 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
114 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
116 static const std::array<std::string, MONTHS_PER_YEAR> full_names = {
117 "January",
"February",
"March",
"April",
"May",
"June",
118 "July",
"August",
"September",
"October",
"November",
"December"
122 if (month_copy == short_names[i] || month_copy == full_names[i]) {
123 value =
static_cast<T
>(i + 1);
132 template<
class T = Month>
139 template<
class T = Month>
153 if (tz_str.empty()) {
166 tz.
hour = std::stoi(tz_str.substr(1, 2));
167 tz.
min = std::stoi(tz_str.substr(4, 2));
187 const std::string& input,
191 static const std::regex date_regex(R
"((\d{4})[-\/\.](\d{2})[-\/\.](\d{2}))");
192 std::smatch date_match;
195 static const std::regex time_regex(R
"((\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})?)");
196 std::smatch time_match;
202 if (std::regex_search(input, date_match, date_regex)) {
203 dt.
year = std::stoll(date_match[1].str());
204 dt.
mon = std::stoi(date_match[2].str());
205 dt.
day = std::stoi(date_match[3].str());
211 if (std::regex_search(input, time_match, time_regex)) {
212 dt.
hour = std::stoi(time_match[1].str());
213 dt.
min = std::stoi(time_match[2].str());
214 dt.
sec = std::stoi(time_match[3].str());
215 if (time_match[4].matched) {
216 dt.
ms = std::stoi(time_match[4].str());
218 if (time_match[5].matched) {
279 inline const ts_t ts(
const std::string& str) {
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 const T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR const T month_of_year(ts_t ts) noexcept
Get the month of the year.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Converts a date and time to a timestamp.
TIME_SHIELD_CONSTEXPR const fts_t to_ftimestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0)
Converts a date and time to a floating-point timestamp.
TIME_SHIELD_CONSTEXPR const 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 and time to a timestamp in milliseconds.
const tz_t to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
const bool try_get_month_number(const std::string &month, T &value)
Get the month number by name, with output parameter.
const bool parse_time_zone(const std::string &tz_str, TimeZoneStruct &tz)
Parse a time zone string into a TimeZoneStruct.
const bool str_to_fts(const std::string &str, fts_t &ts)
Convert an ISO8601 string to a floating-point timestamp (fts_t).
const bool str_to_ts_ms(const std::string &str, ts_ms_t &ts)
Convert an ISO8601 string to a millisecond timestamp (ts_ms_t).
const bool str_to_ts(const std::string &str, ts_t &ts)
Convert an ISO8601 string to a timestamp (ts_t).
const bool parse_tz(const std::string &tz_str, TimeZoneStruct &tz)
Alias for parse_time_zone function.
const T get_month_number(const std::string &month)
Get the month number by name.
const bool parse_iso8601(const std::string &input, DateTimeStruct &dt, TimeZoneStruct &tz)
Parse a date and time string in ISO8601 format.
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
Type for representing timestamps in seconds.
int64_t ts_ms_t
Type for representing timestamps in milliseconds.
double fts_t
Type for representing timestamps as floating-point numbers (e.g., fractional 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.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
TIME_SHIELD_CONSTEXPR const 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 const bool is_valid_time_zone(T hour, T min) noexcept
Check if the time zone is valid.
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.