Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
workday_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_WORKDAY_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_WORKDAY_CONVERSIONS_HPP_INCLUDED
5
8
9#include "config.hpp"
10#include "date_conversions.hpp"
13#include "validation.hpp"
14
15namespace time_shield {
16
19
23 TIME_SHIELD_CONSTEXPR inline int first_workday_day(year_t year, int month) noexcept {
24 const int days = num_days_in_month(year, month);
25 if (days <= 0) {
26 return 0;
27 }
28 for (int day = 1; day <= days; ++day) {
29 if (is_workday(year, month, day)) {
30 return day;
31 }
32 }
33 return 0;
34 }
35
39 TIME_SHIELD_CONSTEXPR inline int last_workday_day(year_t year, int month) noexcept {
40 const int days = num_days_in_month(year, month);
41 if (days <= 0) {
42 return 0;
43 }
44 for (int day = days; day >= 1; --day) {
45 if (is_workday(year, month, day)) {
46 return day;
47 }
48 }
49 return 0;
50 }
51
55 TIME_SHIELD_CONSTEXPR inline int count_workdays_in_month(year_t year, int month) noexcept {
56 const int days = num_days_in_month(year, month);
57 if (days <= 0) {
58 return 0;
59 }
60 int total = 0;
61 for (int day = 1; day <= days; ++day) {
62 if (is_workday(year, month, day)) {
63 ++total;
64 }
65 }
66 return total;
67 }
68
73 TIME_SHIELD_CONSTEXPR inline int workday_index_in_month(year_t year, int month, int day) noexcept {
74 if (!is_workday(year, month, day)) {
75 return 0;
76 }
77 const int days = num_days_in_month(year, month);
78 if (days <= 0) {
79 return 0;
80 }
81 int index = 0;
82 for (int current = 1; current <= days; ++current) {
83 if (is_workday(year, month, current)) {
84 ++index;
85 if (current == day) {
86 return index;
87 }
88 }
89 }
90 return 0;
91 }
92
97 TIME_SHIELD_CONSTEXPR inline bool is_first_workday_of_month(year_t year, int month, int day) noexcept {
98 return is_workday(year, month, day) && first_workday_day(year, month) == day;
99 }
100
106 TIME_SHIELD_CONSTEXPR inline bool is_within_first_workdays_of_month(year_t year, int month, int day, int count) noexcept {
107 if (count <= 0) {
108 return false;
109 }
110 const int total = count_workdays_in_month(year, month);
111 if (count > total) {
112 return false;
113 }
114 const int index = workday_index_in_month(year, month, day);
115 return index > 0 && index <= count;
116 }
117
122 TIME_SHIELD_CONSTEXPR inline bool is_last_workday_of_month(year_t year, int month, int day) noexcept {
123 return is_workday(year, month, day) && last_workday_day(year, month) == day;
124 }
125
131 TIME_SHIELD_CONSTEXPR inline bool is_within_last_workdays_of_month(year_t year, int month, int day, int count) noexcept {
132 if (count <= 0) {
133 return false;
134 }
135 const int total = count_workdays_in_month(year, month);
136 if (count > total) {
137 return false;
138 }
139 const int index = workday_index_in_month(year, month, day);
140 return index > 0 && index >= (total - count + 1);
141 }
142
145 TIME_SHIELD_CONSTEXPR inline bool is_first_workday_of_month(ts_t ts) noexcept {
147 }
148
154
158 TIME_SHIELD_CONSTEXPR inline bool is_within_first_workdays_of_month(ts_t ts, int count) noexcept {
160 }
161
168
171 TIME_SHIELD_CONSTEXPR inline bool is_last_workday_of_month(ts_t ts) noexcept {
173 }
174
180
184 TIME_SHIELD_CONSTEXPR inline bool is_within_last_workdays_of_month(ts_t ts, int count) noexcept {
186 }
187
194
198 TIME_SHIELD_CONSTEXPR inline ts_t start_of_first_workday_month(year_t year, int month) noexcept {
199 const int day = first_workday_day(year, month);
200 if (day <= 0) {
201 return ERROR_TIMESTAMP;
202 }
203 return to_timestamp(year, month, day);
204 }
205
209 TIME_SHIELD_CONSTEXPR inline ts_ms_t start_of_first_workday_month_ms(year_t year, int month) noexcept {
210 const int day = first_workday_day(year, month);
211 if (day <= 0) {
212 return ERROR_TIMESTAMP;
213 }
214 const ts_t day_start = to_timestamp(year, month, day);
215 return sec_to_ms(day_start);
216 }
217
223
229
233 TIME_SHIELD_CONSTEXPR inline ts_t end_of_first_workday_month(year_t year, int month) noexcept {
234 const int day = first_workday_day(year, month);
235 if (day <= 0) {
236 return ERROR_TIMESTAMP;
237 }
238 const ts_t day_start = to_timestamp(year, month, day);
239 return end_of_day(day_start);
240 }
241
245 TIME_SHIELD_CONSTEXPR inline ts_ms_t end_of_first_workday_month_ms(year_t year, int month) noexcept {
246 const int day = first_workday_day(year, month);
247 if (day <= 0) {
248 return ERROR_TIMESTAMP;
249 }
250 const ts_t day_start = to_timestamp(year, month, day);
253 }
254
260
266
270 TIME_SHIELD_CONSTEXPR inline ts_t start_of_last_workday_month(year_t year, int month) noexcept {
271 const int day = last_workday_day(year, month);
272 if (day <= 0) {
273 return ERROR_TIMESTAMP;
274 }
275 return to_timestamp(year, month, day);
276 }
277
281 TIME_SHIELD_CONSTEXPR inline ts_ms_t start_of_last_workday_month_ms(year_t year, int month) noexcept {
282 const int day = last_workday_day(year, month);
283 if (day <= 0) {
284 return ERROR_TIMESTAMP;
285 }
286 const ts_t day_start = to_timestamp(year, month, day);
287 return sec_to_ms(day_start);
288 }
289
295
301
305 TIME_SHIELD_CONSTEXPR inline ts_t end_of_last_workday_month(year_t year, int month) noexcept {
306 const int day = last_workday_day(year, month);
307 if (day <= 0) {
308 return ERROR_TIMESTAMP;
309 }
310 const ts_t day_start = to_timestamp(year, month, day);
311 return end_of_day(day_start);
312 }
313
317 TIME_SHIELD_CONSTEXPR inline ts_ms_t end_of_last_workday_month_ms(year_t year, int month) noexcept {
318 const int day = last_workday_day(year, month);
319 if (day <= 0) {
320 return ERROR_TIMESTAMP;
321 }
322 const ts_t day_start = to_timestamp(year, month, day);
325 }
326
329 TIME_SHIELD_CONSTEXPR inline ts_t end_of_last_workday_month(ts_t ts = time_shield::ts()) noexcept {
331 }
332
338
340
341}; // namespace time_shield
342
343#endif // _TIME_SHIELD_WORKDAY_CONVERSIONS_HPP_INCLUDED
Configuration macros for the library.
Conversions related to calendar dates and DateStruct helpers.
Conversions involving DateTimeStruct and day boundary helpers.
constexpr int64_t ERROR_TIMESTAMP
Error timestamp value.
TIME_SHIELD_CONSTEXPR ts_ms_t end_of_last_workday_month_ms(year_t year, int month) noexcept
Returns end-of-day millisecond timestamp for the last workday of month.
TIME_SHIELD_CONSTEXPR ts_t end_of_first_workday_month(year_t year, int month) noexcept
Returns end-of-day timestamp for the first workday of month.
constexpr ts_ms_t day_start_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_day_ms function.
TIME_SHIELD_CONSTEXPR int last_workday_day(year_t year, int month) noexcept
Finds the last workday number within a month.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_first_workday_month_ms(year_t year, int month) noexcept
Returns start-of-day millisecond timestamp for the first workday of month.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR int count_workdays_in_month(year_t year, int month) noexcept
Counts workdays within a month.
TIME_SHIELD_CONSTEXPR ts_t end_of_last_workday_month(year_t year, int month) noexcept
Returns end-of-day timestamp for the last workday of month.
TIME_SHIELD_CONSTEXPR ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr ts_t day_start(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_day function.
TIME_SHIELD_CONSTEXPR int first_workday_day(year_t year, int month) noexcept
Finds the first workday number within a month.
TIME_SHIELD_CONSTEXPR int workday_index_in_month(year_t year, int month, int day) noexcept
Returns workday position in month starting from 1.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_last_workday_month_ms(year_t year, int month) noexcept
Returns start-of-day millisecond timestamp for the last workday of month.
TIME_SHIELD_CONSTEXPR T year_of_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Get the year from the timestamp in milliseconds.
constexpr T days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
TIME_SHIELD_CONSTEXPR ts_t start_of_last_workday_month(year_t year, int month) noexcept
Returns start-of-day timestamp for the last workday of month.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
TIME_SHIELD_CONSTEXPR T year_of(ts_t ts=time_shield::ts())
Get the year from the timestamp.
TIME_SHIELD_CONSTEXPR ts_ms_t end_of_first_workday_month_ms(year_t year, int month) noexcept
Returns end-of-day millisecond timestamp for the first workday of month.
TIME_SHIELD_CONSTEXPR ts_t start_of_first_workday_month(year_t year, int month) noexcept
Returns start-of-day timestamp for the first workday of month.
bool is_last_workday_of_month(const std::string &str)
Parse an ISO8601 string and check if it is the last workday of its month (seconds).
bool is_within_last_workdays_of_month_ms(const std::string &str, int count)
Parse ISO8601 string and check if it is within last N workdays of its month (milliseconds).
bool is_first_workday_of_month_ms(const std::string &str)
Parse an ISO8601 string and check if it is the first workday of its month (millisecond precision).
bool is_within_first_workdays_of_month_ms(const std::string &str, int count)
Parse an ISO8601 string and check if it falls within the first N workdays of its month (millisecond p...
bool is_workday(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (seconds precision).
bool is_last_workday_of_month_ms(const std::string &str)
Parse an ISO8601 string and check if it is the last workday of its month (millisecond).
bool is_within_first_workdays_of_month(const std::string &str, int count)
Parse an ISO8601 string and check if it falls within the first N workdays of its month.
bool is_first_workday_of_month(const std::string &str)
Parse ISO8601 string and check if it is the first workday of its month (seconds).
bool is_within_last_workdays_of_month(const std::string &str, int count)
Parse ISO8601 string and check if it is within last N workdays of its month (seconds).
bool is_workday_ms(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (milliseconds precision).
TIME_SHIELD_CONSTEXPR T day_of_month(ts_t ts=time_shield::ts())
Get the day of the month.
TIME_SHIELD_CONSTEXPR T month_of_year(ts_t ts) noexcept
Get the month of the year.
constexpr ts_ms_t end_of_day_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the day in milliseconds.
constexpr ts_t end_of_day(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the end of the day.
TIME_SHIELD_CONSTEXPR 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.
constexpr T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:48
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:49
int64_t year_t
Year as an integer (e.g., 2024).
Definition types.hpp:41
Main namespace for the Time Shield library.
Helper functions for unit conversions between seconds, minutes, hours, and milliseconds.
Header file with time-related validation functions.