Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_parser.hpp
Go to the documentation of this file.
1#pragma once
7#ifndef _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
8#define _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
9
10#include "enums.hpp"
11#include "constants.hpp"
12#include "date_time_struct.hpp"
13#include "time_zone_struct.hpp"
14#include "validation.hpp"
15#include "time_conversions.hpp"
16#include <regex>
17#include <algorithm>
18#include <locale>
19#include <array>
20#include <stdexcept>
21
22namespace time_shield {
23
55
61 template<class T = Month>
62 const T get_month_number(const std::string& month) {
63 if (month.empty()) throw std::invalid_argument("Invalid month name");
64
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);
68 });
69 month_copy [0] = toupper(month_copy [0]);
70
71 static const std::array<std::string, 12> short_names = {
72 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
73 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
74 };
75 static const std::array<std::string, 12> full_names = {
76 "January", "February", "March", "April", "May", "June",
77 "July", "August", "September", "October", "November", "December"
78 };
79
80 for(int i = 0; i < MONTHS_PER_YEAR; ++i) {
81 if (month == short_names[i] || month == full_names[i]) {
82 return static_cast<T>(i + 1);
83 }
84 }
85 throw std::invalid_argument("Invalid month name");
86 }
87
90 template<class T = Month>
91 const T month_of_year(const std::string& month) {
92 return get_month_number(month);
93 }
94
95//------------------------------------------------------------------------------
96
102 template<class T = Month>
103 const bool try_get_month_number(const std::string& month, T& value) {
104 if (month.empty()) return false;
105
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);
109 });
110 month_copy[0] = toupper(month_copy[0]);
111
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"
115 };
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"
119 };
120
121 for (int i = 0; i < MONTHS_PER_YEAR; ++i) {
122 if (month_copy == short_names[i] || month_copy == full_names[i]) {
123 value = static_cast<T>(i + 1);
124 return true;
125 }
126 }
127 return false;
128 }
129
132 template<class T = Month>
133 const bool get_month_number(const std::string& month, T& value) {
134 return try_get_month_number(month, value);
135 }
136
139 template<class T = Month>
140 const bool month_of_year(const std::string& month, T& value) {
141 return try_get_month_number(month, value);
142 }
143
144//------------------------------------------------------------------------------
145
152 const bool parse_time_zone(const std::string& tz_str, TimeZoneStruct &tz) {
153 if (tz_str.empty()) {
154 tz.hour = 0;
155 tz.min = 0;
156 tz.is_positive = true;
157 return true;
158 }
159 if (tz_str == "Z") {
160 tz.hour = 0;
161 tz.min = 0;
162 tz.is_positive = true;
163 return true;
164 }
165 tz.is_positive = (tz_str[0] == '+');
166 tz.hour = std::stoi(tz_str.substr(1, 2));
167 tz.min = std::stoi(tz_str.substr(4, 2));
168 return is_valid_time_zone(tz);
169 }
170
173 inline const bool parse_tz(const std::string& tz_str, TimeZoneStruct &tz) {
174 return parse_time_zone(tz_str, tz);
175 }
176
177//------------------------------------------------------------------------------
178
186 const bool parse_iso8601(
187 const std::string& input,
188 DateTimeStruct &dt,
189 TimeZoneStruct &tz) {
190 // Регулярное выражение для даты в формате ISO8601
191 static const std::regex date_regex(R"((\d{4})[-\/\.](\d{2})[-\/\.](\d{2}))");
192 std::smatch date_match;
193
194 // Регулярное выражение для времени в формате ISO8601 с часовым поясом и без
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;
197
199 tz = create_time_zone_struct(0, 0);
200
201 // Парсинг даты
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());
206 } else {
207 return false;
208 }
209
210 // Парсинг времени и часового пояса
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());
217 }
218 if (time_match[5].matched) {
219 if (!parse_time_zone(time_match[5].str(), tz)) return false;
220 }
221 return is_valid_date_time(dt);
222 }
223 return true;
224 }
225
231 inline const bool str_to_ts(const std::string &str, ts_t& ts) {
234 if (!parse_iso8601(str, dt, tz)) return false;
235 try {
236 ts = to_timestamp(dt) + to_offset(tz);
237 return true;
238 } catch(...) {}
239 return false;
240 }
241
247 inline const bool str_to_ts_ms(const std::string &str, ts_ms_t& ts) {
250 if (!parse_iso8601(str, dt, tz)) return false;
251 try {
253 return true;
254 } catch(...) {}
255 return false;
256 }
257
263 inline const bool str_to_fts(const std::string &str, fts_t& ts) {
266 if (!parse_iso8601(str, dt, tz)) return false;
267 try {
268 ts = to_ftimestamp(dt) + static_cast<fts_t>(to_offset(tz));
269 return true;
270 } catch(...) {}
271 return false;
272 }
273
279 inline const ts_t ts(const std::string& str) {
280 ts_t ts = 0;
281 str_to_ts(str, ts);
282 return ts;
283 }
284
290 inline const ts_ms_t ts_ms(const std::string& str) {
291 ts_ms_t ts = 0;
292 str_to_ts_ms(str, ts);
293 return ts;
294 }
295
301 inline const fts_t fts(const std::string& str) {
302 fts_t ts = 0;
303 str_to_fts(str, ts);
304 return ts;
305 }
306
307
313 inline const ts_t ts(const char *str) {
314 ts_t ts = 0;
315 str_to_ts(str, ts);
316 return ts;
317 }
318
324 inline const ts_ms_t ts_ms(const char *str) {
325 ts_ms_t ts = 0;
326 str_to_ts_ms(str, ts);
327 return ts;
328 }
329
335 inline const fts_t fts(const char *str) {
336 fts_t ts = 0;
337 str_to_fts(str, ts);
338 return ts;
339 }
340
342
343};
344
345#endif // _TIME_SHIELD_TIME_PARSER_HPP_INCLUDED
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.
Definition constants.hpp:91
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.
Definition types.hpp:33
int64_t ts_ms_t
Type for representing timestamps in milliseconds.
Definition types.hpp:34
double fts_t
Type for representing timestamps as floating-point numbers (e.g., fractional seconds).
Definition types.hpp:36
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.
Definition constants.hpp:12
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.