Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_unit_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
5
8
9#include "config.hpp"
10#include "constants.hpp"
11#include "types.hpp"
12
13#include <cmath>
14#include <type_traits>
15
16namespace time_shield {
17
20
25 template<class T = int>
26 constexpr T ns_of_sec(fts_t ts) noexcept {
27 fts_t temp;
28 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(NS_PER_SEC)));
29 }
30
35 template<class T = int>
36 constexpr T us_of_sec(fts_t ts) noexcept {
37 fts_t temp;
38 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(US_PER_SEC)));
39 }
40
45 template<class T = int>
46 constexpr T ms_of_sec(fts_t ts) noexcept {
47 fts_t temp;
48 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(MS_PER_SEC)));
49 }
50
55 template<class T = int>
56 constexpr T ms_of_ts(ts_ms_t ts) noexcept {
57 return static_cast<T>(ts % MS_PER_SEC);
58 }
59
60# ifndef TIME_SHIELD_CPP17
66 template<class T>
67 constexpr ts_ms_t sec_to_ms_impl(T t, std::true_type tag) noexcept {
68 return static_cast<ts_ms_t>(std::round(t * static_cast<T>(MS_PER_SEC)));
69 }
70
76 template<class T>
77 constexpr ts_ms_t sec_to_ms_impl(T t, std::false_type tag) noexcept {
78 return static_cast<ts_ms_t>(t) * static_cast<ts_ms_t>(MS_PER_SEC);
79 }
80# endif // TIME_SHIELD_CPP17
81
87 template<class T1 = ts_ms_t, class T2>
88 constexpr T1 sec_to_ms(T2 ts) noexcept {
89# ifdef TIME_SHIELD_CPP17
90 if constexpr (std::is_floating_point_v<T2>) {
91 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_SEC)));
92 } else {
93 return static_cast<T1>(ts) * static_cast<T1>(MS_PER_SEC);
94 }
95# else
96 return sec_to_ms_impl(ts, typename std::conditional<
97 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
98 std::true_type,
99 std::false_type
100 >::type{});
101# endif
102 }
103
107 inline ts_ms_t fsec_to_ms(fts_t ts) noexcept {
108 return static_cast<ts_ms_t>(std::round(ts * static_cast<fts_t>(MS_PER_SEC)));
109 }
110
116 template<class T1 = ts_t, class T2 = ts_ms_t>
117 constexpr T1 ms_to_sec(T2 ts_ms) noexcept {
118 return static_cast<T1>(ts_ms) / static_cast<T1>(MS_PER_SEC);
119 }
120
125 template<class T = ts_ms_t>
126 constexpr fts_t ms_to_fsec(T ts_ms) noexcept {
127 return static_cast<fts_t>(ts_ms) / static_cast<fts_t>(MS_PER_SEC);
128 }
129
130//----------------------------------------------------------------------------//
131// Minutes -> Milliseconds
132//----------------------------------------------------------------------------//
133# ifndef TIME_SHIELD_CPP17
139 template<class T>
140 constexpr ts_ms_t min_to_ms_impl(T t, std::true_type tag) noexcept {
141 return static_cast<ts_ms_t>(std::round(t * static_cast<T>(MS_PER_MIN)));
142 }
143
149 template<class T>
150 constexpr ts_ms_t min_to_ms_impl(T t, std::false_type tag) noexcept {
151 return static_cast<ts_ms_t>(t) * static_cast<ts_ms_t>(MS_PER_MIN);
152 }
153# endif // TIME_SHIELD_CPP17
154
160 template<class T1 = ts_ms_t, class T2>
161 constexpr T1 min_to_ms(T2 ts) noexcept {
162# ifdef TIME_SHIELD_CPP17
163 if constexpr (std::is_floating_point_v<T2>) {
164 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_MIN)));
165 } else {
166 return static_cast<T1>(ts) * static_cast<T1>(MS_PER_MIN);
167 }
168# else
169 return min_to_ms_impl(ts, typename std::conditional<
170 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
171 std::true_type,
172 std::false_type
173 >::type{});
174# endif
175 }
176
182 template<class T1 = int, class T2 = ts_ms_t>
183 constexpr T1 ms_to_min(T2 ts) noexcept {
184 return static_cast<T1>(ts) / static_cast<T1>(MS_PER_MIN);
185 }
186
187//----------------------------------------------------------------------------//
188// Minutes -> Seconds
189//----------------------------------------------------------------------------//
190# ifndef TIME_SHIELD_CPP17
196 template<class T>
197 constexpr ts_t min_to_sec_impl(T t, std::true_type tag) noexcept {
198 return static_cast<ts_t>(std::round(t * static_cast<T>(SEC_PER_MIN)));
199 }
200
206 template<class T>
207 constexpr ts_t min_to_sec_impl(T t, std::false_type tag) noexcept {
208 return static_cast<ts_t>(t) * static_cast<ts_t>(SEC_PER_MIN);
209 }
210# endif // TIME_SHIELD_CPP17
211
217 template<class T1 = ts_t, class T2>
218 constexpr T1 min_to_sec(T2 ts) noexcept {
219# ifdef TIME_SHIELD_CPP17
220 if constexpr (std::is_floating_point_v<T2>) {
221 return static_cast<T1>(std::round(ts * static_cast<T2>(SEC_PER_MIN)));
222 } else {
223 return static_cast<T1>(ts) * static_cast<T1>(SEC_PER_MIN);
224 }
225# else
226 return min_to_sec_impl(ts, typename std::conditional<
227 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
228 std::true_type,
229 std::false_type
230 >::type{});
231# endif
232 }
233
239 template<class T1 = int, class T2 = ts_t>
240 constexpr T1 sec_to_min(T2 ts) noexcept {
241 return static_cast<T1>(ts) / static_cast<T1>(SEC_PER_MIN);
242 }
243
248 template<class T = int>
249 constexpr fts_t min_to_fsec(T min) noexcept {
250 return static_cast<fts_t>(min) * static_cast<fts_t>(SEC_PER_MIN);
251 }
252
257 template<class T = ts_t>
258 constexpr double sec_to_fmin(T ts) noexcept {
259 return static_cast<double>(ts) / static_cast<double>(SEC_PER_MIN);
260 }
261
262//----------------------------------------------------------------------------//
263// Hours -> Milliseconds
264//----------------------------------------------------------------------------//
265
266# ifndef TIME_SHIELD_CPP17
272 template<class T>
273 constexpr ts_ms_t hour_to_ms_impl(T t, std::true_type tag) noexcept {
274 return static_cast<ts_ms_t>(std::round(t * static_cast<T>(MS_PER_HOUR)));
275 }
276
282 template<class T>
283 constexpr ts_ms_t hour_to_ms_impl(T t, std::false_type tag) noexcept {
284 return static_cast<ts_ms_t>(t) * static_cast<ts_ms_t>(MS_PER_HOUR);
285 }
286# endif // TIME_SHIELD_CPP17
287
293 template<class T1 = ts_ms_t, class T2>
294 constexpr T1 hour_to_ms(T2 ts) noexcept {
295# ifdef TIME_SHIELD_CPP17
296 if constexpr (std::is_floating_point_v<T2>) {
297 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_HOUR)));
298 } else {
299 return static_cast<T1>(ts) * static_cast<T1>(MS_PER_HOUR);
300 }
301# else
302 return hour_to_ms_impl(ts, typename std::conditional<
303 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
304 std::true_type,
305 std::false_type
306 >::type{});
307# endif
308 }
309
315 template<class T1 = int, class T2 = ts_ms_t>
316 constexpr T1 ms_to_hour(T2 ts) noexcept {
317 return static_cast<T1>(ts) / static_cast<T1>(MS_PER_HOUR);
318 }
319
320//----------------------------------------------------------------------------//
321// Hours -> Seconds
322//----------------------------------------------------------------------------//
323
324# ifndef TIME_SHIELD_CPP17
330 template<class T>
331 constexpr ts_t hour_to_sec_impl(T t, std::true_type tag) noexcept {
332 return static_cast<ts_t>(std::round(t * static_cast<T>(SEC_PER_HOUR)));
333 }
334
340 template<class T>
341 constexpr ts_t hour_to_sec_impl(T t, std::false_type tag) noexcept {
342 return static_cast<ts_t>(t) * static_cast<ts_t>(SEC_PER_HOUR);
343 }
344# endif // TIME_SHIELD_CPP17
345
351 template<class T1 = ts_t, class T2>
352 constexpr T1 hour_to_sec(T2 ts) noexcept {
353# ifdef TIME_SHIELD_CPP17
354 if constexpr (std::is_floating_point_v<T2>) {
355 return static_cast<T1>(std::round(ts * static_cast<T2>(SEC_PER_HOUR)));
356 } else {
357 return static_cast<T1>(ts) * static_cast<T1>(SEC_PER_HOUR);
358 }
359# else
360 return hour_to_sec_impl(ts, typename std::conditional<
361 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
362 std::true_type,
363 std::false_type
364 >::type{});
365# endif
366 }
367
373 template<class T1 = int, class T2 = ts_t>
374 constexpr T1 sec_to_hour(T2 ts) noexcept {
375 return static_cast<T1>(ts) / static_cast<T1>(SEC_PER_HOUR);
376 }
377
382 template<class T = int>
383 constexpr fts_t hour_to_fsec(T hr) noexcept {
384 return static_cast<fts_t>(hr) * static_cast<fts_t>(SEC_PER_HOUR);
385 }
386
391 template<class T = ts_t>
392 constexpr double sec_to_fhour(T ts) noexcept {
393 return static_cast<double>(ts) / static_cast<double>(SEC_PER_HOUR);
394 }
395
400 template<class T = int>
401 TIME_SHIELD_CONSTEXPR inline T hour24_to_12(T hour) noexcept {
402 if (hour == 0 || hour > 12) return 12;
403 return hour;
404 }
405
407
408}; // namespace time_shield
409
410#endif // _TIME_SHIELD_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
Configuration macros for the library.
Header file with time-related constants.
constexpr int64_t MS_PER_MIN
Milliseconds per minute.
Definition constants.hpp:83
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:77
constexpr int64_t SEC_PER_MIN
Seconds per minute.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:73
constexpr int64_t MS_PER_HOUR
Milliseconds per hour.
Definition constants.hpp:90
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:76
constexpr T1 min_to_sec(T2 ts) noexcept
Converts a timestamp from minutes to seconds.
constexpr T1 hour_to_ms(T2 ts) noexcept
Converts a timestamp from hours to milliseconds.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
constexpr T ms_of_ts(ts_ms_t ts) noexcept
Get the millisecond part of the timestamp.
constexpr double sec_to_fhour(T ts) noexcept
Converts a timestamp from seconds to floating-point hours.
constexpr ts_ms_t sec_to_ms_impl(T t, std::true_type tag) noexcept
Helper function for converting seconds to milliseconds (floating-point version).
TIME_SHIELD_CONSTEXPR T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
constexpr T1 ms_to_hour(T2 ts) noexcept
Converts a timestamp from milliseconds to hours.
TIME_SHIELD_CONSTEXPR ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr T1 hour_to_sec(T2 ts) noexcept
Converts a timestamp from hours to seconds.
constexpr ts_t min_to_sec_impl(T t, std::true_type tag) noexcept
Helper function for converting minutes to seconds (floating-point version).
constexpr double sec_to_fmin(T ts) noexcept
Converts a timestamp from seconds to floating-point minutes.
constexpr ts_t hour_to_sec_impl(T t, std::true_type tag) noexcept
Helper function for converting hours to seconds (floating-point version).
constexpr fts_t hour_to_fsec(T hr) noexcept
Converts a timestamp from hours to floating-point seconds.
constexpr fts_t min_to_fsec(T min) noexcept
Converts a timestamp from minutes to floating-point seconds.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
constexpr T1 min_to_ms(T2 ts) noexcept
Converts a timestamp from minutes to milliseconds.
constexpr T1 ms_to_min(T2 ts) noexcept
Converts a timestamp from milliseconds to minutes.
constexpr T1 sec_to_min(T2 ts) noexcept
Converts a timestamp from seconds to minutes.
constexpr T1 sec_to_hour(T2 ts) noexcept
Converts a timestamp from seconds to hours.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
constexpr ts_ms_t hour_to_ms_impl(T t, std::true_type tag) noexcept
Helper function for converting hours to milliseconds (floating-point version).
constexpr ts_ms_t min_to_ms_impl(T t, std::true_type tag) noexcept
Helper function for converting minutes to milliseconds (floating-point version).
constexpr fts_t ms_to_fsec(T ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
ts_ms_t fsec_to_ms(fts_t ts) noexcept
Converts a floating-point timestamp from seconds to milliseconds.
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
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:51
T ns_of_sec() noexcept
Get the nanosecond part of the current second.
T ms_of_sec() noexcept
Get the millisecond part of the current second.
T us_of_sec() noexcept
Get the microsecond part of the current second.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.