Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
validation.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_VALIDATION_HPP_INCLUDED
4#define _TIME_SHIELD_VALIDATION_HPP_INCLUDED
5
10
11#include "config.hpp"
12#include "types.hpp"
13#include "constants.hpp"
14#include "time_zone_struct.hpp"
15
16namespace time_shield {
17
49
54 template<class T = year_t>
55 constexpr bool is_leap_year_date(T year) noexcept {
56 return ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0));
57 }
58
61 template<class T = year_t>
62 constexpr bool check_leap_year(T year) noexcept {
63 return is_leap_year_date(year);
64 }
65
68 template<class T = year_t>
69 constexpr bool leap_year(T year) noexcept {
70 return is_leap_year_date(year);
71 }
72
73//------------------------------------------------------------------------------
74
83 TIME_SHIELD_CONSTEXPR bool is_leap_year_ts(ts_t ts) {
84 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
85 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
86 // Поэтому пришлось снизить до 9223371890843040000
87 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
88 constexpr int64_t BIAS_2000 = 946684800LL;
89
90 int64_t y = MAX_YEAR;
91 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
92
93 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
94 secs -= n_400_years * SEC_PER_400_YEARS;
95 y -= n_400_years * 400;
96
97 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
98 secs -= n_100_years * SEC_PER_100_YEARS;
99 y -= n_100_years * 100;
100
101 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
102 secs -= n_4_years * SEC_PER_4_YEARS;
103 y -= n_4_years * 4;
104
105 const int64_t n_1_years = secs / SEC_PER_YEAR;
106 secs -= n_1_years * SEC_PER_YEAR;
107 y -= n_1_years;
108
109 y = secs == 0 ? y : y - 1;
110 return is_leap_year_date(y);
111 }
112
115 TIME_SHIELD_CONSTEXPR bool leap_year_ts(ts_t ts) {
116 return is_leap_year_ts(ts);
117 }
118
121 TIME_SHIELD_CONSTEXPR bool check_leap_year_ts(ts_t ts) {
122 return is_leap_year_ts(ts);
123 }
124
127 TIME_SHIELD_CONSTEXPR bool is_leap_year(ts_t ts) {
128 return is_leap_year_ts(ts);
129 }
130
131//------------------------------------------------------------------------------
132
138 template<class T = int>
139 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone(
140 T hour,
141 T min) noexcept {
142 if (hour < 0 || hour > 23) return false;
143 if (min < 0 || min > 59) return false;
144 return true;
145 }
146
149 template<class T = int>
150 TIME_SHIELD_CONSTEXPR inline bool is_valid_tz(
151 T hour,
152 T min) {
153 return is_valid_time_zone(hour, min);
154 }
155
156//------------------------------------------------------------------------------
157
163 template<class T = TimeZoneStruct>
164 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone_offset(
165 const T& time_zone) noexcept {
166 return is_valid_time_zone(time_zone.hour, time_zone.min);
167 }
168
172 template<class T = TimeZoneStruct>
173 TIME_SHIELD_CONSTEXPR inline bool is_valid_time_zone(
174 const T& time_zone) {
175 return is_valid_time_zone_offset(time_zone);
176 }
177
181 template<class T = TimeZoneStruct>
182 TIME_SHIELD_CONSTEXPR inline bool is_valid_tz(
183 const T& time_zone) {
184 return is_valid_time_zone_offset(time_zone);
185 }
186
187//------------------------------------------------------------------------------
188
197 template<class T1 = int, class T2 = int>
198 TIME_SHIELD_CONSTEXPR inline bool is_valid_time(
199 T1 hour,
200 T1 min,
201 T1 sec,
202 T2 ms = 0) noexcept {
203 if (hour < 0 || hour > 23) return false;
204 if (min < 0 || min > 59) return false;
205 if (sec < 0 || sec > 59) return false;
206 if (ms < 0 || ms > 999) return false;
207 return true;
208 }
209
215 template<class T>
216 TIME_SHIELD_CONSTEXPR inline bool is_valid_time(
217 const T& time) noexcept {
218 return is_valid_time(time.hour, time.min, time.sec, time.ms);
219 }
220
228 template<class T1 = year_t, class T2 = int>
229 TIME_SHIELD_CONSTEXPR inline bool is_valid_date(
230 T1 year,
231 T2 month,
232 T2 day) noexcept {
233 if (day > 31 && year <= 31) {
234 return is_valid_date((T1)day, month, (T2)year);
235 }
236 if (year > MAX_YEAR) return false;
237 if (month < 1 || month > 12) return false;
238 if (day < 1 || day > 31) return false;
239 if (month == FEB) {
240 const bool is_leap_year = is_leap_year_date(year);
241 if (is_leap_year && day > 29) return false;
242 if (!is_leap_year && day > 28) return false;
243 } else {
244 switch(month) {
245 case 4:
246 case 6:
247 case 9:
248 case 11:
249 if (day > 30) return false;
250 default:
251 break;
252 };
253 }
254 return true;
255 }
256
262 template<class T>
263 TIME_SHIELD_CONSTEXPR inline bool is_valid_date(const T& date) noexcept {
264 return is_valid_date(date.year, date.mon, date.day);
265 }
266
279 template<class T1 = year_t, class T2 = int, class T3 = int>
280 TIME_SHIELD_CONSTEXPR inline bool is_valid_date_time(
281 T1 year,
282 T2 month,
283 T2 day,
284 T2 hour = 0,
285 T2 min = 0,
286 T2 sec = 0,
287 T3 ms = 0) noexcept {
288 if (!is_valid_date(year, month, day)) return false;
289 if (!is_valid_time(hour, min, sec, ms)) return false;
290 return true;
291 }
292
298 template<class T>
299 TIME_SHIELD_CONSTEXPR inline bool is_valid_date_time(
300 const T &date_time) noexcept {
301 if (!is_valid_date(date_time)) return false;
302 if (!is_valid_time(date_time)) return false;
303 return true;
304 }
305
306//------------------------------------------------------------------------------
307
314 TIME_SHIELD_CONSTEXPR inline bool is_day_off(ts_t ts) noexcept {
315 const int wd = ((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
316 return (wd == SUN || wd == SAT);
317 }
318
321 TIME_SHIELD_CONSTEXPR inline bool is_weekend(ts_t ts) noexcept {
322 return is_day_off(ts);
323 }
324
325//------------------------------------------------------------------------------
326
332 template<class T = uday_t>
333 TIME_SHIELD_CONSTEXPR inline bool is_day_off_unix_day(T unix_day) noexcept {
334 const int wd = (unix_day + THU) % DAYS_PER_WEEK;
335 return (wd == SUN || wd == SAT);
336 }
337
340 template<class T = uday_t>
341 TIME_SHIELD_CONSTEXPR inline bool is_weekend_unix_day(T unix_day) noexcept {
343 }
344
346
347}; // namespace time_shield
348
349#endif // _TIME_SHIELD_VALIDATION_HPP_INCLUDED
Configuration macros for the library.
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)
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
constexpr int64_t SEC_PER_DAY
Seconds per day.
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
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 T year(ts_t ts=time_shield::ts())
Alias for get_year function.
@ FEB
February.
Definition enums.hpp:95
@ SUN
Sunday.
Definition enums.hpp:28
@ SAT
Saturday.
Definition enums.hpp:34
@ THU
Thursday.
Definition enums.hpp:32
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:46
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 bool check_leap_year(T year) noexcept
Alias for is_leap_year_date function.
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.
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.
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)....
constexpr bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
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.
constexpr bool leap_year(T year) noexcept
Alias for is_leap_year_date function.
Main namespace for the Time Shield library.
Header for time zone structure and related functions.
Type definitions for time-related units and formats.