Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
validation.hpp
Go to the documentation of this file.
1#pragma once
6#ifndef _TIME_SHIELD_VALIDATION_HPP_INCLUDED
7#define _TIME_SHIELD_VALIDATION_HPP_INCLUDED
8
9#include "config.hpp"
10#include "types.hpp"
11#include "constants.hpp"
12
13namespace time_shield {
14
19 template<class T = year_t>
20 constexpr const bool is_leap_year_date(const T& year) noexcept {
21 return ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0));
22 }
23
26 template<class T = year_t>
27 constexpr const bool check_leap_year(const T& year) noexcept {
28 return is_leap_year_date(year);
29 }
30
33 template<class T = year_t>
34 constexpr const bool leap_year(const T& year) noexcept {
35 return is_leap_year_date(year);
36 }
37
38//------------------------------------------------------------------------------
39
49 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
50 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
51 // Поэтому пришлось снизить до 9223371890843040000
52 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
53 constexpr int64_t BIAS_2000 = 946684800LL;
54
55 int64_t y = MAX_YEAR;
56 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
57
58 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
59 secs -= n_400_years * SEC_PER_400_YEARS;
60 y -= n_400_years * 400;
61
62 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
63 secs -= n_100_years * SEC_PER_100_YEARS;
64 y -= n_100_years * 100;
65
66 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
67 secs -= n_4_years * SEC_PER_4_YEARS;
68 y -= n_4_years * 4;
69
70 const int64_t n_1_years = secs / SEC_PER_YEAR;
71 secs -= n_1_years * SEC_PER_YEAR;
72 y -= n_1_years;
73
74 y = secs == 0 ? y : y - 1;
75 return is_leap_year_date(y);
76 }
77
81 return is_leap_year_ts(ts);
82 }
83
87 return is_leap_year_ts(ts);
88 }
89
93 return is_leap_year_ts(ts);
94 }
95
96//------------------------------------------------------------------------------
97
103 template<class T = int>
105 const T& hour,
106 const T& min) noexcept {
107 if (hour < 0 || hour > 23) return false;
108 if (min < 0 || min > 59) return false;
109 return true;
110 }
111
114 template<class T = int>
116 const T& hour,
117 const T& min) {
118 return is_valid_time_zone(hour, min);
119 }
120
121//------------------------------------------------------------------------------
122
127 template<class T = TimeZoneStruct>
129 const T& time_zone) noexcept {
130 return is_valid_time_zone(time_zone.hour, time_zone.min);
131 }
132
135 template<class T = TimeZoneStruct>
137 const T& time_zone) {
138 return is_valid_time_zone_offset(time_zone);
139 }
140
143 template<class T = TimeZoneStruct>
145 const T& time_zone) {
146 return is_valid_time_zone_offset(time_zone);
147 }
148
149//------------------------------------------------------------------------------
150
159 template<class T1 = int, class T2 = int>
161 const T1& hour,
162 const T1& min,
163 const T1& sec,
164 const T2& ms = 0) noexcept {
165 if (hour < 0 || hour > 23) return false;
166 if (min < 0 || min > 59) return false;
167 if (sec < 0 || sec > 59) return false;
168 if (ms < 0 || ms > 999) return false;
169 return true;
170 }
171
176 template<class T>
178 const T& time) noexcept {
179 return is_valid_time(time.hour, time.min, time.sec, time.ms);
180 }
181
189 template<class T1 = year_t, class T2 = int>
191 const T1& year,
192 const T2& month,
193 const T2& day) noexcept {
194 if (day > 31 && year <= 31) {
195 return is_valid_date((T1)day, month, (T2)year);
196 }
197 if (year > MAX_YEAR) return false;
198 if (month < 1 || month > 12) return false;
199 if (day < 1 || day > 31) return false;
200 if (month == FEB) {
201 const bool is_leap_year = is_leap_year_date(year);
202 if (is_leap_year && day > 29) return false;
203 if (!is_leap_year && day > 28) return false;
204 } else {
205 switch(month) {
206 case 4:
207 case 6:
208 case 9:
209 case 11:
210 if (day > 30) return false;
211 default:
212 break;
213 };
214 }
215 return true;
216 }
217
222 template<class T>
223 TIME_SHIELD_CONSTEXPR inline const bool is_valid_date(const T& date) noexcept {
224 return is_valid_date(date.year, date.mon, date.day);
225 }
226
239 template<class T1 = year_t, class T2 = int, class T3 = int>
241 const T1& year,
242 const T2& month,
243 const T2& day,
244 const T2& hour = 0,
245 const T2& min = 0,
246 const T2& sec = 0,
247 const T3& ms = 0) noexcept {
248 if (!is_valid_date(year, month, day)) return false;
249 if (!is_valid_time(hour, min, sec, ms)) return false;
250 return true;
251 }
252
257 template<class T>
259 const T &date_time) noexcept {
260 if (!is_valid_date(date_time)) return false;
261 if (!is_valid_time(date_time)) return false;
262 return true;
263 }
264
265//------------------------------------------------------------------------------
266
273 TIME_SHIELD_CONSTEXPR inline const bool is_day_off(const ts_t& ts) noexcept {
274 const int wd = ((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
275 return (wd == SUN || wd == SAT);
276 }
277
280 TIME_SHIELD_CONSTEXPR inline const bool is_weekend(const ts_t& ts) noexcept {
281 return is_day_off(ts);
282 }
283
284//------------------------------------------------------------------------------
285
291 template<class T = uday_t>
292 TIME_SHIELD_CONSTEXPR inline bool is_day_off_unix_day(const T& unix_day) noexcept {
293 const int wd = (unix_day + THU) % DAYS_PER_WEEK;
294 return (wd == SUN || wd == SAT);
295 }
296
299 template<class T = uday_t>
300 TIME_SHIELD_CONSTEXPR inline bool is_weekend_unix_day(const T& unix_day) noexcept {
302 }
303
304}; // namespace time_shield
305
306#endif // _TIME_SHIELD_VALIDATION_HPP_INCLUDED
Header file with preprocessor definitions.
#define TIME_SHIELD_CONSTEXPR
Definition config.hpp:16
Header file with time-related constants.
Main namespace for the Time Shield library.
Definition constants.hpp:12
int64_t ts_t
Integer timestamp type.
Definition types.hpp:15
constexpr const bool is_leap_year_date(const T &year) noexcept
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR bool is_valid_tz(const T &hour, const T &min)
Alias for is_valid_time_zone function.
TIME_SHIELD_CONSTEXPR const bool check_leap_year_ts(const ts_t &ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR const bool is_leap_year_ts(const ts_t &ts)
Checks if the given year is a leap year.
constexpr const bool check_leap_year(const T &year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR const bool is_valid_time_zone(const T &hour, const T &min) noexcept
Check if the time zone is valid.
TIME_SHIELD_CONSTEXPR const bool is_day_off(const ts_t &ts) noexcept
Check if a given timestamp corresponds to a weekend day (Saturday or Sunday).
constexpr int64_t SEC_PER_DAY
Seconds per day.
Definition constants.hpp:31
constexpr const bool leap_year(const T &year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR bool is_weekend_unix_day(const T &unix_day) noexcept
Alias for is_day_off_unix_day function.
constexpr const T unix_day(const ts_t &ts=ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR const bool is_valid_date(const T1 &year, const T2 &month, const T2 &day) noexcept
Checks the correctness of the specified date.
TIME_SHIELD_CONSTEXPR const bool is_valid_time(const T1 &hour, const T1 &min, const T1 &sec, const T2 &ms=0) noexcept
Checks the correctness of the specified time.
constexpr int64_t DAYS_PER_WEEK
Days per week.
Definition constants.hpp:50
TIME_SHIELD_CONSTEXPR const bool leap_year_ts(const ts_t &ts)
Alias for is_leap_year_ts function.
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
Definition constants.hpp:35
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
Definition constants.hpp:37
TIME_SHIELD_CONSTEXPR const T year(const ts_t &ts=ts())
Alias for get_year function.
TIME_SHIELD_CONSTEXPR const bool is_weekend(const ts_t &ts) noexcept
Alias for is_day_off function.
TIME_SHIELD_CONSTEXPR bool is_day_off_unix_day(const T &unix_day) noexcept
Check if a given day (since Unix epoch) corresponds to a weekend day (Saturday or Sunday)....
TIME_SHIELD_CONSTEXPR const bool is_valid_date_time(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T3 &ms=0) noexcept
Checks the correctness of a date and time.
TIME_SHIELD_CONSTEXPR const bool is_leap_year(const ts_t &ts)
Alias for is_leap_year_ts function.
@ FEB
February.
Definition enums.hpp:87
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
Definition constants.hpp:38
constexpr int64_t MAX_YEAR
Maximum representable year.
Definition constants.hpp:64
TIME_SHIELD_CONSTEXPR const bool is_valid_time_zone_offset(const T &time_zone) noexcept
Check if the time zone is valid.
@ SUN
Sunday.
Definition enums.hpp:23
@ SAT
Saturday.
Definition enums.hpp:29
@ THU
Thursday.
Definition enums.hpp:27
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
Definition constants.hpp:32
Header file with type definitions.