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
2#ifndef _TIME_SHIELD_VALIDATION_HPP_INCLUDED
3#define _TIME_SHIELD_VALIDATION_HPP_INCLUDED
4
9
10#include "config.hpp"
11#include "types.hpp"
12#include "constants.hpp"
13#include "time_zone_struct.hpp"
14
15namespace time_shield {
16
48
53 template<class T = year_t>
54 constexpr const bool is_leap_year_date(T year) noexcept {
55 return ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0));
56 }
57
60 template<class T = year_t>
61 constexpr const bool check_leap_year(T year) noexcept {
62 return is_leap_year_date(year);
63 }
64
67 template<class T = year_t>
68 constexpr const bool leap_year(T year) noexcept {
69 return is_leap_year_date(year);
70 }
71
72//------------------------------------------------------------------------------
73
82 TIME_SHIELD_CONSTEXPR bool is_leap_year_ts(ts_t ts) {
83 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
84 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
85 // Поэтому пришлось снизить до 9223371890843040000
86 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
87 constexpr int64_t BIAS_2000 = 946684800LL;
88
89 int64_t y = MAX_YEAR;
90 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
91
92 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
93 secs -= n_400_years * SEC_PER_400_YEARS;
94 y -= n_400_years * 400;
95
96 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
97 secs -= n_100_years * SEC_PER_100_YEARS;
98 y -= n_100_years * 100;
99
100 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
101 secs -= n_4_years * SEC_PER_4_YEARS;
102 y -= n_4_years * 4;
103
104 const int64_t n_1_years = secs / SEC_PER_YEAR;
105 secs -= n_1_years * SEC_PER_YEAR;
106 y -= n_1_years;
107
108 y = secs == 0 ? y : y - 1;
109 return is_leap_year_date(y);
110 }
111
114 TIME_SHIELD_CONSTEXPR bool leap_year_ts(ts_t ts) {
115 return is_leap_year_ts(ts);
116 }
117
120 TIME_SHIELD_CONSTEXPR bool check_leap_year_ts(ts_t ts) {
121 return is_leap_year_ts(ts);
122 }
123
126 TIME_SHIELD_CONSTEXPR bool is_leap_year(ts_t ts) {
127 return is_leap_year_ts(ts);
128 }
129
130//------------------------------------------------------------------------------
131
137 template<class T = int>
138 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone(
139 T hour,
140 T min) noexcept {
141 if (hour < 0 || hour > 23) return false;
142 if (min < 0 || min > 59) return false;
143 return true;
144 }
145
148 template<class T = int>
149 TIME_SHIELD_CONSTEXPR inline bool is_valid_tz(
150 T hour,
151 T min) {
152 return is_valid_time_zone(hour, min);
153 }
154
155//------------------------------------------------------------------------------
156
162 template<class T = TimeZoneStruct>
163 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone_offset(
164 const T& time_zone) noexcept {
165 return is_valid_time_zone(time_zone.hour, time_zone.min);
166 }
167
171 template<class T = TimeZoneStruct>
172 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone(
173 const T& time_zone) {
174 return is_valid_time_zone_offset(time_zone);
175 }
176
180 template<class T = TimeZoneStruct>
181 TIME_SHIELD_CONSTEXPR inline bool is_valid_tz(
182 const T& time_zone) {
183 return is_valid_time_zone_offset(time_zone);
184 }
185
186//------------------------------------------------------------------------------
187
196 template<class T1 = int, class T2 = int>
197 TIME_SHIELD_CONSTEXPR inline bool is_valid_time(
198 T1 hour,
199 T1 min,
200 T1 sec,
201 T2 ms = 0) noexcept {
202 if (hour < 0 || hour > 23) return false;
203 if (min < 0 || min > 59) return false;
204 if (sec < 0 || sec > 59) return false;
205 if (ms < 0 || ms > 999) return false;
206 return true;
207 }
208
214 template<class T>
215 TIME_SHIELD_CONSTEXPR inline bool is_valid_time(
216 const T& time) noexcept {
217 return is_valid_time(time.hour, time.min, time.sec, time.ms);
218 }
219
227 template<class T1 = year_t, class T2 = int>
228 TIME_SHIELD_CONSTEXPR inline bool is_valid_date(
229 T1 year,
230 T2 month,
231 T2 day) noexcept {
232 if (day > 31 && year <= 31) {
233 return is_valid_date((T1)day, month, (T2)year);
234 }
235 if (year > MAX_YEAR) return false;
236 if (month < 1 || month > 12) return false;
237 if (day < 1 || day > 31) return false;
238 if (month == FEB) {
239 const bool is_leap_year = is_leap_year_date(year);
240 if (is_leap_year && day > 29) return false;
241 if (!is_leap_year && day > 28) return false;
242 } else {
243 switch(month) {
244 case 4:
245 case 6:
246 case 9:
247 case 11:
248 if (day > 30) return false;
249 default:
250 break;
251 };
252 }
253 return true;
254 }
255
261 template<class T>
262 TIME_SHIELD_CONSTEXPR inline bool is_valid_date(const T& date) noexcept {
263 return is_valid_date(date.year, date.mon, date.day);
264 }
265
278 template<class T1 = year_t, class T2 = int, class T3 = int>
279 TIME_SHIELD_CONSTEXPR inline bool is_valid_date_time(
280 T1 year,
281 T2 month,
282 T2 day,
283 T2 hour = 0,
284 T2 min = 0,
285 T2 sec = 0,
286 T3 ms = 0) noexcept {
287 if (!is_valid_date(year, month, day)) return false;
288 if (!is_valid_time(hour, min, sec, ms)) return false;
289 return true;
290 }
291
297 template<class T>
298 TIME_SHIELD_CONSTEXPR inline bool is_valid_date_time(
299 const T &date_time) noexcept {
300 if (!is_valid_date(date_time)) return false;
301 if (!is_valid_time(date_time)) return false;
302 return true;
303 }
304
305//------------------------------------------------------------------------------
306
313 TIME_SHIELD_CONSTEXPR inline bool is_day_off(ts_t ts) noexcept {
314 const int wd = ((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
315 return (wd == SUN || wd == SAT);
316 }
317
320 TIME_SHIELD_CONSTEXPR inline bool is_weekend(ts_t ts) noexcept {
321 return is_day_off(ts);
322 }
323
324//------------------------------------------------------------------------------
325
331 template<class T = uday_t>
332 TIME_SHIELD_CONSTEXPR inline bool is_day_off_unix_day(T unix_day) noexcept {
333 const int wd = (unix_day + THU) % DAYS_PER_WEEK;
334 return (wd == SUN || wd == SAT);
335 }
336
339 template<class T = uday_t>
340 TIME_SHIELD_CONSTEXPR inline bool is_weekend_unix_day(T unix_day) noexcept {
342 }
343
345
346}; // namespace time_shield
347
348#endif // _TIME_SHIELD_VALIDATION_HPP_INCLUDED
Header file with preprocessor definitions for C++ standards and constexpr usage.
Header file with time-related constants.
constexpr int64_t DAYS_PER_WEEK
Days per week.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
Definition constants.hpp:77
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
Definition constants.hpp:82
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
Definition constants.hpp:83
constexpr int64_t SEC_PER_DAY
Seconds per day.
Definition constants.hpp:76
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
Definition constants.hpp:80
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR const T year(ts_t ts=ts())
Alias for get_year function.
@ FEB
February.
Definition enums.hpp:94
@ SUN
Sunday.
Definition enums.hpp:27
@ SAT
Saturday.
Definition enums.hpp:33
@ THU
Thursday.
Definition enums.hpp:31
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone_offset(const T &time_zone) noexcept
Check if the time zone is valid.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:45
TIME_SHIELD_CONSTEXPR bool is_day_off(ts_t ts) noexcept
Check if a given timestamp corresponds to a weekend day (Saturday or Sunday).
constexpr const bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR bool is_leap_year(ts_t ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR bool is_valid_tz(T hour, T min)
Alias for is_valid_time_zone function.
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone(T hour, T min) noexcept
Check if the time zone is valid.
constexpr const bool check_leap_year(T year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR bool is_valid_date(T1 year, T2 month, T2 day) noexcept
Checks the correctness of the specified date.
TIME_SHIELD_CONSTEXPR bool is_weekend(ts_t ts) noexcept
Alias for is_day_off function.
TIME_SHIELD_CONSTEXPR bool is_valid_time(T1 hour, T1 min, T1 sec, T2 ms=0) noexcept
Checks the correctness of the specified time.
constexpr const bool leap_year(T year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR bool leap_year_ts(ts_t ts)
Alias for is_leap_year_ts 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_weekend_unix_day(T unix_day) noexcept
Alias for is_day_off_unix_day function.
TIME_SHIELD_CONSTEXPR bool is_day_off_unix_day(T unix_day) noexcept
Check if a given day (since Unix epoch) corresponds to a weekend day (Saturday or Sunday)....
TIME_SHIELD_CONSTEXPR bool check_leap_year_ts(ts_t ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR bool is_leap_year_ts(ts_t ts)
Checks if the given year is a leap year.
Main namespace for the Time Shield library.
Header for time zone structure and related functions.
Type definitions for time-related units and formats.