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