Time Shield Library 2.0.0
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_HEADER_CONVERSIONS_WORKDAY_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_CONVERSIONS_WORKDAY_CONVERSIONS_HPP_INCLUDED
5
8
10#include "date_conversions.hpp"
13
14namespace time_shield {
15
18
22 TIME_SHIELD_CONSTEXPR inline int first_workday_day(year_t year, int month) noexcept {
23 const int days = num_days_in_month(year, month);
24 if (days <= 0) {
25 return 0;
26 }
27 for (int day = 1; day <= days; ++day) {
28 if (is_workday(year, month, day)) {
29 return day;
30 }
31 }
32 return 0;
33 }
34
38 TIME_SHIELD_CONSTEXPR inline int last_workday_day(year_t year, int month) noexcept {
39 const int days = num_days_in_month(year, month);
40 if (days <= 0) {
41 return 0;
42 }
43 for (int day = days; day >= 1; --day) {
44 if (is_workday(year, month, day)) {
45 return day;
46 }
47 }
48 return 0;
49 }
50
54 TIME_SHIELD_CONSTEXPR inline int count_workdays_in_month(year_t year, int month) noexcept {
55 const int days = num_days_in_month(year, month);
56 if (days <= 0) {
57 return 0;
58 }
59 int total = 0;
60 for (int day = 1; day <= days; ++day) {
61 if (is_workday(year, month, day)) {
62 ++total;
63 }
64 }
65 return total;
66 }
67
72 TIME_SHIELD_CONSTEXPR inline int workday_index_in_month(year_t year, int month, int day) noexcept {
73 if (!is_workday(year, month, day)) {
74 return 0;
75 }
76 const int days = num_days_in_month(year, month);
77 if (days <= 0) {
78 return 0;
79 }
80 int index = 0;
81 for (int current = 1; current <= days; ++current) {
82 if (is_workday(year, month, current)) {
83 ++index;
84 if (current == day) {
85 return index;
86 }
87 }
88 }
89 return 0;
90 }
91
96 TIME_SHIELD_CONSTEXPR inline bool is_first_workday_of_month(year_t year, int month, int day) noexcept {
97 return is_workday(year, month, day) && first_workday_day(year, month) == day;
98 }
99
105 TIME_SHIELD_CONSTEXPR inline bool is_within_first_workdays_of_month(year_t year, int month, int day, int count) noexcept {
106 if (count <= 0) {
107 return false;
108 }
109 const int total = count_workdays_in_month(year, month);
110 if (count > total) {
111 return false;
112 }
113 const int index = workday_index_in_month(year, month, day);
114 return index > 0 && index <= count;
115 }
116
121 TIME_SHIELD_CONSTEXPR inline bool is_last_workday_of_month(year_t year, int month, int day) noexcept {
122 return is_workday(year, month, day) && last_workday_day(year, month) == day;
123 }
124
130 TIME_SHIELD_CONSTEXPR inline bool is_within_last_workdays_of_month(year_t year, int month, int day, int count) noexcept {
131 if (count <= 0) {
132 return false;
133 }
134 const int total = count_workdays_in_month(year, month);
135 if (count > total) {
136 return false;
137 }
138 const int index = workday_index_in_month(year, month, day);
139 return index > 0 && index >= (total - count + 1);
140 }
141
144 TIME_SHIELD_CONSTEXPR inline bool is_first_workday_of_month(ts_t ts) noexcept {
146 }
147
153
157 TIME_SHIELD_CONSTEXPR inline bool is_within_first_workdays_of_month(ts_t ts, int count) noexcept {
159 }
160
167
170 TIME_SHIELD_CONSTEXPR inline bool is_last_workday_of_month(ts_t ts) noexcept {
172 }
173
179
183 TIME_SHIELD_CONSTEXPR inline bool is_within_last_workdays_of_month(ts_t ts, int count) noexcept {
185 }
186
193
197 TIME_SHIELD_CONSTEXPR inline ts_t start_of_first_workday_month(year_t year, int month) noexcept {
198 const int day = first_workday_day(year, month);
199 if (day <= 0) {
200 return ERROR_TIMESTAMP;
201 }
202 return to_timestamp(year, month, day);
203 }
204
208 TIME_SHIELD_CONSTEXPR inline ts_ms_t start_of_first_workday_month_ms(year_t year, int month) noexcept {
209 const int day = first_workday_day(year, month);
210 if (day <= 0) {
211 return ERROR_TIMESTAMP;
212 }
213 const ts_t day_start = to_timestamp(year, month, day);
214 return sec_to_ms(day_start);
215 }
216
222
228
232 TIME_SHIELD_CONSTEXPR inline ts_t end_of_first_workday_month(year_t year, int month) noexcept {
233 const int day = first_workday_day(year, month);
234 if (day <= 0) {
235 return ERROR_TIMESTAMP;
236 }
237 const ts_t day_start = to_timestamp(year, month, day);
238 return end_of_day(day_start);
239 }
240
244 TIME_SHIELD_CONSTEXPR inline ts_ms_t end_of_first_workday_month_ms(year_t year, int month) noexcept {
245 const int day = first_workday_day(year, month);
246 if (day <= 0) {
247 return ERROR_TIMESTAMP;
248 }
249 const ts_t day_start = to_timestamp(year, month, day);
252 }
253
259
265
269 TIME_SHIELD_CONSTEXPR inline ts_t start_of_last_workday_month(year_t year, int month) noexcept {
270 const int day = last_workday_day(year, month);
271 if (day <= 0) {
272 return ERROR_TIMESTAMP;
273 }
274 return to_timestamp(year, month, day);
275 }
276
280 TIME_SHIELD_CONSTEXPR inline ts_ms_t start_of_last_workday_month_ms(year_t year, int month) noexcept {
281 const int day = last_workday_day(year, month);
282 if (day <= 0) {
283 return ERROR_TIMESTAMP;
284 }
285 const ts_t day_start = to_timestamp(year, month, day);
286 return sec_to_ms(day_start);
287 }
288
294
300
304 TIME_SHIELD_CONSTEXPR inline ts_t end_of_last_workday_month(year_t year, int month) noexcept {
305 const int day = last_workday_day(year, month);
306 if (day <= 0) {
307 return ERROR_TIMESTAMP;
308 }
309 const ts_t day_start = to_timestamp(year, month, day);
310 return end_of_day(day_start);
311 }
312
316 TIME_SHIELD_CONSTEXPR inline ts_ms_t end_of_last_workday_month_ms(year_t year, int month) noexcept {
317 const int day = last_workday_day(year, month);
318 if (day <= 0) {
319 return ERROR_TIMESTAMP;
320 }
321 const ts_t day_start = to_timestamp(year, month, day);
324 }
325
328 TIME_SHIELD_CONSTEXPR inline ts_t end_of_last_workday_month(ts_t ts = time_shield::ts()) noexcept {
330 }
331
337
339
340}; // namespace time_shield
341
342#endif // TIME_SHIELD_HEADER_CONVERSIONS_WORKDAY_CONVERSIONS_HPP_INCLUDED
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 T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
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 bool is_first_workday_of_month_ms(ts_ms_t ts_ms) noexcept
Checks whether millisecond timestamp is the first workday of the 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.
TIME_SHIELD_CONSTEXPR bool is_within_last_workdays_of_month_ms(ts_ms_t ts_ms, int count) noexcept
Checks if millisecond timestamp falls within the last N workdays of the month.
TIME_SHIELD_CONSTEXPR bool is_within_first_workdays_of_month(year_t year, int month, int day, int count) noexcept
Checks if date falls within the first N workdays of the month.
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_t day_start(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_day function.
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 bool is_within_first_workdays_of_month_ms(ts_ms_t ts_ms, int count) noexcept
Checks if millisecond timestamp falls within the first N workdays of the month.
TIME_SHIELD_CONSTEXPR bool is_last_workday_of_month(year_t year, int month, int day) noexcept
Checks whether date is the last workday of the month.
TIME_SHIELD_CONSTEXPR int count_workdays_in_month(year_t year, int month) noexcept
Counts workdays within a month.
TIME_SHIELD_CONSTEXPR bool is_first_workday_of_month(year_t year, int month, int day) noexcept
Checks whether date is the first workday of the month.
TIME_SHIELD_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 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 bool is_last_workday_of_month_ms(ts_ms_t ts_ms) noexcept
Checks whether millisecond timestamp is the last workday of the month.
TIME_SHIELD_CONSTEXPR int first_workday_day(year_t year, int month) noexcept
Finds the first workday number within a month.
TIME_SHIELD_CONSTEXPR T days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
TIME_SHIELD_CONSTEXPR bool is_within_last_workdays_of_month(year_t year, int month, int day, int count) noexcept
Checks if date falls within the last N workdays of the 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.
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.
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.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
TIME_SHIELD_CONSTEXPR T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
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.
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.
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.
TIME_SHIELD_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.
TIME_SHIELD_CONSTEXPR T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
TIME_SHIELD_CONSTEXPR ts_t end_of_day(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the end of the day.
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:40
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR bool is_workday(ts_t ts) noexcept
Check if a given timestamp corresponds to a workday (Monday to Friday).
TIME_SHIELD_CONSTEXPR bool is_workday_ms(ts_ms_t ts_ms) noexcept
Check if a given timestamp in milliseconds corresponds to a workday (Monday to Friday).
Main namespace for the Time Shield library.
Helper functions for unit conversions between seconds, minutes, hours, and milliseconds.