3#ifndef _TIME_SHIELD_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
26 template<
class T =
int>
28 const int64_t ns =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
NS_PER_SEC)));
36 template<
class T =
int>
38 const int64_t us =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
US_PER_SEC)));
46 template<
class T =
int>
48 const int64_t ms =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
MS_PER_SEC)));
56 template<
class T =
int>
59 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
NS_PER_SEC)));
66 template<
class T =
int>
69 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
US_PER_SEC)));
76 template<
class T =
int>
79 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
MS_PER_SEC)));
86 template<
class T =
int>
95 template<
class T =
int>
104 template<
class T =
int>
113 template<
class T =
int>
122 template<
class T =
int,
class T2 =
int64_t>
127# ifndef TIME_SHIELD_CPP17
154 template<
class T1 = ts_ms_t,
class T2>
156# ifdef TIME_SHIELD_CPP17
157 if constexpr (std::is_floating_point_v<T2>) {
158 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_SEC)));
160 return static_cast<T1
>(
ts) *
static_cast<T1
>(
MS_PER_SEC);
164 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
183 template<
class T1 = ts_t,
class T2 = ts_ms_t>
186 static_cast<int64_t
>(
ts_ms),
194 template<
class T = ts_ms_t>
202# ifndef TIME_SHIELD_CPP17
229 template<
class T1 = ts_ms_t,
class T2>
231# ifdef TIME_SHIELD_CPP17
232 if constexpr (std::is_floating_point_v<T2>) {
233 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_MIN)));
235 return static_cast<T1
>(
ts) *
static_cast<T1
>(
MS_PER_MIN);
239 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
251 template<
class T1 =
int,
class T2 = ts_ms_t>
254 static_cast<int64_t
>(
ts),
261# ifndef TIME_SHIELD_CPP17
288 template<
class T1 = ts_t,
class T2>
290# ifdef TIME_SHIELD_CPP17
291 if constexpr (std::is_floating_point_v<T2>) {
292 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
SEC_PER_MIN)));
298 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
310 template<
class T1 =
int,
class T2 = ts_t>
313 static_cast<int64_t
>(
ts),
321 template<
class T =
int>
330 template<
class T = ts_t>
332 return static_cast<double>(
ts) /
static_cast<double>(
SEC_PER_MIN);
339# ifndef TIME_SHIELD_CPP17
366 template<
class T1 = ts_ms_t,
class T2>
368# ifdef TIME_SHIELD_CPP17
369 if constexpr (std::is_floating_point_v<T2>) {
370 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_HOUR)));
376 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
388 template<
class T1 =
int,
class T2 = ts_ms_t>
391 static_cast<int64_t
>(
ts),
399# ifndef TIME_SHIELD_CPP17
426 template<
class T1 = ts_t,
class T2>
428# ifdef TIME_SHIELD_CPP17
429 if constexpr (std::is_floating_point_v<T2>) {
430 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
SEC_PER_HOUR)));
436 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
448 template<
class T1 =
int,
class T2 = ts_t>
451 static_cast<int64_t
>(
ts),
459 template<
class T =
int>
468 template<
class T = ts_t>
470 return static_cast<double>(
ts) /
static_cast<double>(
SEC_PER_HOUR);
477 template<
class T =
int>
479 if (hour == 0 || hour > 12)
return 12;
Configuration macros for the library.
Header file with time-related constants.
Floor division and modulus helpers.
constexpr int64_t MS_PER_MIN
Milliseconds per minute.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
constexpr int64_t MS_PER_HOUR
Milliseconds per hour.
constexpr int64_t US_PER_SEC
Microseconds per second.
TIME_SHIELD_CONSTEXPR T ms_of_sec_signed(fts_t ts) noexcept
Get the millisecond part of the second from a floating-point timestamp (truncating).
TIME_SHIELD_CONSTEXPR T ns_of_sec_signed(fts_t ts) noexcept
Get the nanosecond part of the second from a floating-point timestamp (truncating).
constexpr ts_ms_t sec_to_ms_impl(T t, std::true_type) noexcept
Helper function for converting seconds to milliseconds (floating-point version).
constexpr T1 min_to_sec(T2 ts) noexcept
Converts a timestamp from minutes to seconds.
TIME_SHIELD_CONSTEXPR T us_of_sec_signed(fts_t ts) noexcept
Get the microsecond part of the second from a floating-point timestamp (truncating).
constexpr T ms_part(ts_ms_t ts) noexcept
Get the millisecond part of the timestamp.
constexpr T ns_part(T2 ts) noexcept
Get the nanosecond part of the timestamp.
constexpr ts_t hour_to_sec_impl(T t, std::true_type) noexcept
Helper function for converting hours to seconds (floating-point version).
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
Alias for ms_part.
constexpr double sec_to_fhour(T ts) noexcept
Converts a timestamp from seconds to floating-point hours.
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 double sec_to_fmin(T ts) noexcept
Converts a timestamp from seconds to floating-point minutes.
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 ts_ms_t hour_to_ms_impl(T t, std::true_type) noexcept
Helper function for converting hours to milliseconds (floating-point version).
constexpr ts_t min_to_sec_impl(T t, std::true_type) noexcept
Helper function for converting minutes to seconds (floating-point version).
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 T us_of_ts(ts_us_t ts) noexcept
Alias for us_part.
constexpr ts_ms_t min_to_ms_impl(T t, std::true_type) 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.
constexpr T us_part(ts_us_t ts) noexcept
Get the microsecond part of the timestamp.
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.
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
double fts_t
Floating-point timestamp (fractional seconds since epoch).
int64_t ts_us_t
Unix timestamp in microseconds since epoch.
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.
TIME_SHIELD_CONSTEXPR T floor_mod(T a, T b) noexcept
Floor-mod for positive modulus (returns r in [0..b)).
TIME_SHIELD_CONSTEXPR T floor_div(T a, T b) noexcept
Floor division for positive divisor.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.