@@ -88,38 +88,49 @@ namespace time_shield {
8888 return ts % MS_PER_SEC;
8989 }
9090
91- # ifndef TIME_SHIELD_CPP17
92- template <class T >
93- constexpr const ts_ms_t sec_to_ms_impl (T t, std::true_type) noexcept {
94- return static_cast <ts_ms_t >(std::round (t * static_cast <T>(MS_PER_SEC)));
95- }
96-
97- template <class T >
98- constexpr const ts_ms_t sec_to_ms_impl (T t, std::false_type) noexcept {
99- return static_cast <ts_ms_t >(t) * static_cast <ts_ms_t >(MS_PER_SEC);
100- }
101- # endif
102-
103- // / \brief Converts a timestamp from seconds to milliseconds.
104- // / \tparam T1 The type of the output timestamp (default is ts_ms_t).
105- // / \tparam T2 The type of the input timestamp.
106- // / \param ts Timestamp in seconds.
107- // / \return T1 Timestamp in milliseconds.
108- template <class T1 = ts_ms_t , class T2 >
109- constexpr const T1 sec_to_ms (T2 ts) noexcept {
110- # ifdef TIME_SHIELD_CPP17
111- if constexpr (std::is_same_v<T2, double >) {
112- return static_cast <T1>(std::round (ts * static_cast <T2>(MS_PER_SEC)));
113- } else
114- if constexpr (std::is_same_v<T2, float >) {
115- return static_cast <T1>(std::round (ts * static_cast <T2>(MS_PER_SEC)));
116- } else {
117- return static_cast <T1>(ts) * static_cast <T1>(MS_PER_SEC);
118- }
119- # else
120- return sec_to_ms_impl (ts, std::is_same<T2, double >());
121- # endif
122- }
91+ # ifndef TIME_SHIELD_CPP17
92+ // / \brief Helper function for converting seconds to milliseconds (floating-point version).
93+ // / \tparam T Type of the input timestamp.
94+ // / \param t Timestamp in seconds.
95+ // / \param tag std::true_type indicates a floating-point type.
96+ // / \return ts_ms_t Timestamp in milliseconds.
97+ template <class T >
98+ constexpr ts_ms_t sec_to_ms_impl (T t, std::true_type) noexcept {
99+ return static_cast <ts_ms_t >(std::round (t * static_cast <T>(MS_PER_SEC)));
100+ }
101+
102+ // / \brief Helper function for converting seconds to milliseconds (integral version).
103+ // / \tparam T Type of the input timestamp.
104+ // / \param t Timestamp in seconds.
105+ // / \param tag std::false_type indicates a non-floating-point type.
106+ // / \return ts_ms_t Timestamp in milliseconds.
107+ template <class T >
108+ constexpr ts_ms_t sec_to_ms_impl (T t, std::false_type) noexcept {
109+ return static_cast <ts_ms_t >(t) * static_cast <ts_ms_t >(MS_PER_SEC);
110+ }
111+ # endif // TIME_SHIELD_CPP17
112+
113+ // / \brief Converts a timestamp from seconds to milliseconds.
114+ // / \tparam T1 The type of the output timestamp (default is ts_ms_t).
115+ // / \tparam T2 The type of the input timestamp.
116+ // / \param ts Timestamp in seconds.
117+ // / \return T1 Timestamp in milliseconds.
118+ template <class T1 = ts_ms_t , class T2 >
119+ constexpr T1 sec_to_ms (T2 ts) noexcept {
120+ # ifdef TIME_SHIELD_CPP17
121+ if constexpr (std::is_floating_point_v<T2>) {
122+ return static_cast <T1>(std::round (ts * static_cast <T2>(MS_PER_SEC)));
123+ } else {
124+ return static_cast <T1>(ts) * static_cast <T1>(MS_PER_SEC);
125+ }
126+ # else
127+ return sec_to_ms_impl (ts, typename std::conditional<
128+ (std::is_same<T2, double >::value || std::is_same<T2, float >::value),
129+ std::true_type,
130+ std::false_type
131+ >::type{});
132+ # endif
133+ }
123134
124135 // / \brief Converts a floating-point timestamp from seconds to milliseconds.
125136 // / \param ts Timestamp in floating-point seconds.
@@ -147,6 +158,272 @@ namespace time_shield {
147158 return static_cast <fts_t >(ts_ms) / static_cast <fts_t >(MS_PER_SEC);
148159 }
149160
161+ // ----------------------------------------------------------------------------//
162+ // Minutes -> Milliseconds
163+ // ----------------------------------------------------------------------------//
164+ # ifndef TIME_SHIELD_CPP17
165+ // / \brief Helper function for converting minutes to milliseconds (floating-point version).
166+ // / \tparam T Type of the input timestamp.
167+ // / \param t Timestamp in minutes.
168+ // / \param tag std::true_type indicates a floating-point type (double or float).
169+ // / \return ts_ms_t Timestamp in milliseconds.
170+ template <class T >
171+ constexpr ts_ms_t min_to_ms_impl (T t, std::true_type) noexcept {
172+ return static_cast <ts_ms_t >(std::round (t * static_cast <T>(MS_PER_MIN)));
173+ }
174+
175+ // / \brief Helper function for converting minutes to milliseconds (integral version).
176+ // / \tparam T Type of the input timestamp.
177+ // / \param t Timestamp in minutes.
178+ // / \param tag std::false_type indicates a non-floating-point type.
179+ // / \return ts_ms_t Timestamp in milliseconds.
180+ template <class T >
181+ constexpr ts_ms_t min_to_ms_impl (T t, std::false_type) noexcept {
182+ return static_cast <ts_ms_t >(t) * static_cast <ts_ms_t >(MS_PER_MIN);
183+ }
184+ # endif // TIME_SHIELD_CPP17
185+
186+ // / \brief Converts a timestamp from minutes to milliseconds.
187+ // / \tparam T1 The type of the output timestamp (default is ts_ms_t).
188+ // / \tparam T2 The type of the input timestamp.
189+ // / \param ts Timestamp in minutes.
190+ // / \return T1 Timestamp in milliseconds.
191+ template <class T1 = ts_ms_t , class T2 >
192+ constexpr T1 min_to_ms (T2 ts) noexcept {
193+ # ifdef TIME_SHIELD_CPP17
194+ if constexpr (std::is_floating_point_v<T2>) {
195+ return static_cast <T1>(std::round (ts * static_cast <T2>(MS_PER_MIN)));
196+ } else {
197+ return static_cast <T1>(ts) * static_cast <T1>(MS_PER_MIN);
198+ }
199+ # else
200+ return min_to_ms_impl (ts, typename std::conditional<
201+ (std::is_same<T2, double >::value || std::is_same<T2, float >::value),
202+ std::true_type,
203+ std::false_type
204+ >::type{});
205+ # endif
206+ }
207+
208+ // / \brief Converts a timestamp from milliseconds to minutes.
209+ // / \tparam T1 The type of the output timestamp (default is int).
210+ // / \tparam T2 The type of the input timestamp (default is ts_ms_t).
211+ // / \param ts Timestamp in milliseconds.
212+ // / \return T1 Timestamp in minutes.
213+ template <class T1 = int , class T2 = ts_ms_t >
214+ constexpr T1 ms_to_min (T2 ts) noexcept {
215+ return static_cast <T1>(ts) / static_cast <T1>(MS_PER_MIN);
216+ }
217+
218+ // ----------------------------------------------------------------------------//
219+ // Minutes -> Seconds
220+ // ----------------------------------------------------------------------------//
221+ # ifndef TIME_SHIELD_CPP17
222+ // / \brief Helper function for converting minutes to seconds (floating-point version).
223+ // / \tparam T Type of the input timestamp.
224+ // / \param t Timestamp in minutes.
225+ // / \param tag std::true_type indicates a floating-point type (double or float).
226+ // / \return ts_t Timestamp in seconds.
227+ template <class T >
228+ constexpr ts_t min_to_sec_impl (T t, std::true_type) noexcept {
229+ return static_cast <ts_t >(std::round (t * static_cast <T>(SEC_PER_MIN)));
230+ }
231+
232+ // / \brief Helper function for converting minutes to seconds (integral version).
233+ // / \tparam T Type of the input timestamp.
234+ // / \param t Timestamp in minutes.
235+ // / \param tag std::false_type indicates a non-floating-point type.
236+ // / \return ts_t Timestamp in seconds.
237+ template <class T >
238+ constexpr ts_t min_to_sec_impl (T t, std::false_type) noexcept {
239+ return static_cast <ts_t >(t) * static_cast <ts_t >(SEC_PER_MIN);
240+ }
241+ # endif // TIME_SHIELD_CPP17
242+
243+ // / \brief Converts a timestamp from minutes to seconds.
244+ // / \tparam T1 The type of the output timestamp (default is ts_t).
245+ // / \tparam T2 The type of the input timestamp.
246+ // / \param ts Timestamp in minutes.
247+ // / \return T1 Timestamp in seconds.
248+ template <class T1 = ts_t , class T2 >
249+ constexpr T1 min_to_sec (T2 ts) noexcept {
250+ # ifdef TIME_SHIELD_CPP17
251+ if constexpr (std::is_floating_point_v<T2>) {
252+ return static_cast <T1>(std::round (ts * static_cast <T2>(SEC_PER_MIN)));
253+ } else {
254+ return static_cast <T1>(ts) * static_cast <T1>(SEC_PER_MIN);
255+ }
256+ # else
257+ return min_to_sec_impl (ts, typename std::conditional<
258+ (std::is_same<T2, double >::value || std::is_same<T2, float >::value),
259+ std::true_type,
260+ std::false_type
261+ >::type{});
262+ # endif
263+ }
264+
265+ // / \brief Converts a timestamp from seconds to minutes.
266+ // / \tparam T1 The type of the output timestamp (default is int).
267+ // / \tparam T2 The type of the input timestamp (default is ts_t).
268+ // / \param ts Timestamp in seconds.
269+ // / \return T1 Timestamp in minutes.
270+ template <class T1 = int , class T2 = ts_t >
271+ constexpr T1 sec_to_min (T2 ts) noexcept {
272+ return static_cast <T1>(ts) / static_cast <T1>(SEC_PER_MIN);
273+ }
274+
275+ // / \brief Converts a timestamp from minutes to floating-point seconds.
276+ // / \tparam T The type of the input timestamp (default is int).
277+ // / \param min Timestamp in minutes.
278+ // / \return fts_t Timestamp in floating-point seconds.
279+ template <class T = int >
280+ constexpr fts_t min_to_fsec (T min) noexcept {
281+ return static_cast <fts_t >(min) * static_cast <fts_t >(SEC_PER_MIN);
282+ }
283+
284+ // / \brief Converts a timestamp from seconds to floating-point minutes.
285+ // / \tparam T The type of the input timestamp (default is ts_t).
286+ // / \param ts Timestamp in seconds.
287+ // / \return double Timestamp in floating-point minutes.
288+ template <class T = ts_t >
289+ constexpr double sec_to_fmin (T ts) noexcept {
290+ return static_cast <double >(ts) / static_cast <double >(SEC_PER_MIN);
291+ }
292+
293+ // ----------------------------------------------------------------------------//
294+ // Hours -> Milliseconds
295+ // ----------------------------------------------------------------------------//
296+
297+ # ifndef TIME_SHIELD_CPP17
298+ // / \brief Helper function for converting hours to milliseconds (floating-point version).
299+ // / \tparam T Type of the input timestamp.
300+ // / \param t Timestamp in hours.
301+ // / \param tag std::true_type indicates a floating-point type (double or float).
302+ // / \return ts_ms_t Timestamp in milliseconds.
303+ template <class T >
304+ constexpr ts_ms_t hour_to_ms_impl (T t, std::true_type) noexcept {
305+ return static_cast <ts_ms_t >(std::round (t * static_cast <T>(MS_PER_HOUR)));
306+ }
307+
308+ // / \brief Helper function for converting hours to milliseconds (integral version).
309+ // / \tparam T Type of the input timestamp.
310+ // / \param t Timestamp in hours.
311+ // / \param tag std::false_type indicates a non-floating-point type.
312+ // / \return ts_ms_t Timestamp in milliseconds.
313+ template <class T >
314+ constexpr ts_ms_t hour_to_ms_impl (T t, std::false_type) noexcept {
315+ return static_cast <ts_ms_t >(t) * static_cast <ts_ms_t >(MS_PER_HOUR);
316+ }
317+ # endif // TIME_SHIELD_CPP17
318+
319+ // / \brief Converts a timestamp from hours to milliseconds.
320+ // / \tparam T1 The type of the output timestamp (default is ts_ms_t).
321+ // / \tparam T2 The type of the input timestamp.
322+ // / \param ts Timestamp in hours.
323+ // / \return T1 Timestamp in milliseconds.
324+ template <class T1 = ts_ms_t , class T2 >
325+ constexpr T1 hour_to_ms (T2 ts) noexcept {
326+ # ifdef TIME_SHIELD_CPP17
327+ if constexpr (std::is_floating_point_v<T2>) {
328+ return static_cast <T1>(std::round (ts * static_cast <T2>(MS_PER_HOUR)));
329+ } else {
330+ return static_cast <T1>(ts) * static_cast <T1>(MS_PER_HOUR);
331+ }
332+ # else
333+ return hour_to_ms_impl (ts, typename std::conditional<
334+ (std::is_same<T2, double >::value || std::is_same<T2, float >::value),
335+ std::true_type,
336+ std::false_type
337+ >::type{});
338+ # endif
339+ }
340+
341+ // / \brief Converts a timestamp from milliseconds to hours.
342+ // / \tparam T1 The type of the output timestamp (default is int).
343+ // / \tparam T2 The type of the input timestamp (default is ts_ms_t).
344+ // / \param ts Timestamp in milliseconds.
345+ // / \return T1 Timestamp in hours.
346+ template <class T1 = int , class T2 = ts_ms_t >
347+ constexpr T1 ms_to_hour (T2 ts) noexcept {
348+ return static_cast <T1>(ts) / static_cast <T1>(MS_PER_HOUR);
349+ }
350+
351+ // ----------------------------------------------------------------------------//
352+ // Hours -> Seconds
353+ // ----------------------------------------------------------------------------//
354+
355+ # ifndef TIME_SHIELD_CPP17
356+ // / \brief Helper function for converting hours to seconds (floating-point version).
357+ // / \tparam T Type of the input timestamp.
358+ // / \param t Timestamp in hours.
359+ // / \param tag std::true_type indicates a floating-point type (double or float).
360+ // / \return ts_t Timestamp in seconds.
361+ template <class T >
362+ constexpr ts_t hour_to_sec_impl (T t, std::true_type) noexcept {
363+ return static_cast <ts_t >(std::round (t * static_cast <T>(SEC_PER_HOUR)));
364+ }
365+
366+ // / \brief Helper function for converting hours to seconds (integral version).
367+ // / \tparam T Type of the input timestamp.
368+ // / \param t Timestamp in hours.
369+ // / \param tag std::false_type indicates a non-floating-point type.
370+ // / \return ts_t Timestamp in seconds.
371+ template <class T >
372+ constexpr ts_t hour_to_sec_impl (T t, std::false_type) noexcept {
373+ return static_cast <ts_t >(t) * static_cast <ts_t >(SEC_PER_HOUR);
374+ }
375+ # endif // TIME_SHIELD_CPP17
376+
377+ // / \brief Converts a timestamp from hours to seconds.
378+ // / \tparam T1 The type of the output timestamp (default is ts_t).
379+ // / \tparam T2 The type of the input timestamp.
380+ // / \param ts Timestamp in hours.
381+ // / \return T1 Timestamp in seconds.
382+ template <class T1 = ts_t , class T2 >
383+ constexpr T1 hour_to_sec (T2 ts) noexcept {
384+ # ifdef TIME_SHIELD_CPP17
385+ if constexpr (std::is_floating_point_v<T2>) {
386+ return static_cast <T1>(std::round (ts * static_cast <T2>(SEC_PER_HOUR)));
387+ } else {
388+ return static_cast <T1>(ts) * static_cast <T1>(SEC_PER_HOUR);
389+ }
390+ # else
391+ return hour_to_sec_impl (ts, typename std::conditional<
392+ (std::is_same<T2, double >::value || std::is_same<T2, float >::value),
393+ std::true_type,
394+ std::false_type
395+ >::type{});
396+ # endif
397+ }
398+
399+ // / \brief Converts a timestamp from seconds to hours.
400+ // / \tparam T1 The type of the output timestamp (default is int).
401+ // / \tparam T2 The type of the input timestamp (default is ts_t).
402+ // / \param ts Timestamp in seconds.
403+ // / \return T1 Timestamp in hours.
404+ template <class T1 = int , class T2 = ts_t >
405+ constexpr T1 sec_to_hour (T2 ts) noexcept {
406+ return static_cast <T1>(ts) / static_cast <T1>(SEC_PER_HOUR);
407+ }
408+
409+ // / \brief Converts a timestamp from hours to floating-point seconds.
410+ // / \tparam T The type of the input timestamp (default is int).
411+ // / \param hr Timestamp in hours.
412+ // / \return fts_t Timestamp in floating-point seconds.
413+ template <class T = int >
414+ constexpr fts_t hour_to_fsec (T hr) noexcept {
415+ return static_cast <fts_t >(hr) * static_cast <fts_t >(SEC_PER_HOUR);
416+ }
417+
418+ // / \brief Converts a timestamp from seconds to floating-point hours.
419+ // / \tparam T The type of the input timestamp (default is ts_t).
420+ // / \param ts Timestamp in seconds.
421+ // / \return double Timestamp in floating-point hours.
422+ template <class T = ts_t >
423+ constexpr double sec_to_fhour (T ts) noexcept {
424+ return static_cast <double >(ts) / static_cast <double >(SEC_PER_HOUR);
425+ }
426+
150427// ------------------------------------------------------------------------------
151428
152429 // / \brief Converts a UNIX timestamp to a year.
0 commit comments