3#ifndef TIME_SHIELD_HEADER_CONVERSIONS_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_CONVERSIONS_TIME_UNIT_CONVERSIONS_HPP_INCLUDED
24 template<
class T =
int>
26 const int64_t ns =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
NS_PER_SEC)));
34 template<
class T =
int>
36 const int64_t us =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
US_PER_SEC)));
44 template<
class T =
int>
46 const int64_t ms =
static_cast<int64_t
>(std::floor(
ts *
static_cast<fts_t>(
MS_PER_SEC)));
54 template<
class T =
int>
57 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
NS_PER_SEC)));
64 template<
class T =
int>
67 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
US_PER_SEC)));
74 template<
class T =
int>
77 return static_cast<T
>(std::round(std::modf(
ts, &temp) *
static_cast<fts_t>(
MS_PER_SEC)));
84 template<
class T =
int>
93 template<
class T =
int>
102 template<
class T =
int>
111 template<
class T =
int>
120 template<
class T =
int,
class T2 =
int64_t>
125# ifndef TIME_SHIELD_CPP17
150 template<
class T1 = ts_ms_t,
class T2>
152# ifdef TIME_SHIELD_CPP17
153 if constexpr (std::is_floating_point_v<T2>) {
154 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_SEC)));
156 return static_cast<T1
>(
ts) *
static_cast<T1
>(
MS_PER_SEC);
160 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
179 template<
class T1 = ts_t,
class T2 = ts_ms_t>
182 static_cast<int64_t
>(
ts_ms),
190 template<
class T = ts_ms_t>
198# ifndef TIME_SHIELD_CPP17
223 template<
class T1 = ts_ms_t,
class T2>
225# ifdef TIME_SHIELD_CPP17
226 if constexpr (std::is_floating_point_v<T2>) {
227 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_MIN)));
229 return static_cast<T1
>(
ts) *
static_cast<T1
>(
MS_PER_MIN);
233 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
245 template<
class T1 =
int,
class T2 = ts_ms_t>
248 static_cast<int64_t
>(
ts),
255# ifndef TIME_SHIELD_CPP17
280 template<
class T1 = ts_t,
class T2>
282# ifdef TIME_SHIELD_CPP17
283 if constexpr (std::is_floating_point_v<T2>) {
284 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
SEC_PER_MIN)));
290 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
302 template<
class T1 =
int,
class T2 = ts_t>
305 static_cast<int64_t
>(
ts),
313 template<
class T =
int>
322 template<
class T = ts_t>
324 return static_cast<double>(
ts) /
static_cast<double>(
SEC_PER_MIN);
331# ifndef TIME_SHIELD_CPP17
356 template<
class T1 = ts_ms_t,
class T2>
358# ifdef TIME_SHIELD_CPP17
359 if constexpr (std::is_floating_point_v<T2>) {
360 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
MS_PER_HOUR)));
366 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
378 template<
class T1 =
int,
class T2 = ts_ms_t>
381 static_cast<int64_t
>(
ts),
389# ifndef TIME_SHIELD_CPP17
414 template<
class T1 = ts_t,
class T2>
416# ifdef TIME_SHIELD_CPP17
417 if constexpr (std::is_floating_point_v<T2>) {
418 return static_cast<T1
>(std::round(
ts *
static_cast<T2
>(
SEC_PER_HOUR)));
424 (std::is_same<T2, double>::value || std::is_same<T2, float>::value),
436 template<
class T1 =
int,
class T2 = ts_t>
439 static_cast<int64_t
>(
ts),
447 template<
class T =
int>
456 template<
class T = ts_t>
458 return static_cast<double>(
ts) /
static_cast<double>(
SEC_PER_HOUR);
465 template<
class T =
int>
467 if (hour == 0 || hour > 12)
return 12;
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 ts_t min_to_sec_impl(T t, std::true_type) noexcept
Helper function for converting minutes to seconds (floating-point version).
TIME_SHIELD_CONSTEXPR T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR ts_ms_t min_to_ms_impl(T t, std::true_type) noexcept
Helper function for converting minutes to milliseconds (floating-point version).
TIME_SHIELD_CONSTEXPR double sec_to_fmin(T ts) noexcept
Converts a timestamp from seconds to floating-point minutes.
TIME_SHIELD_CONSTEXPR T1 min_to_ms(T2 ts) noexcept
Converts a timestamp from minutes to milliseconds.
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).
TIME_SHIELD_CONSTEXPR T1 ms_to_hour(T2 ts) noexcept
Converts a timestamp from milliseconds to hours.
TIME_SHIELD_CONSTEXPR T ms_of_ts(ts_ms_t ts) noexcept
Alias for ms_part.
TIME_SHIELD_CONSTEXPR T1 sec_to_min(T2 ts) noexcept
Converts a timestamp from seconds to minutes.
TIME_SHIELD_CONSTEXPR T1 ms_to_min(T2 ts) noexcept
Converts a timestamp from milliseconds to minutes.
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).
TIME_SHIELD_CONSTEXPR T ns_part(T2 ts) noexcept
Get the nanosecond part of the timestamp.
TIME_SHIELD_CONSTEXPR fts_t ms_to_fsec(T ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
TIME_SHIELD_CONSTEXPR T us_part(ts_us_t ts) noexcept
Get the microsecond part of the timestamp.
TIME_SHIELD_CONSTEXPR T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
TIME_SHIELD_CONSTEXPR T us_of_ts(ts_us_t ts) noexcept
Alias for us_part.
TIME_SHIELD_CONSTEXPR T1 hour_to_ms(T2 ts) noexcept
Converts a timestamp from hours to milliseconds.
TIME_SHIELD_CONSTEXPR T1 min_to_sec(T2 ts) noexcept
Converts a timestamp from minutes to seconds.
TIME_SHIELD_CONSTEXPR double sec_to_fhour(T ts) noexcept
Converts a timestamp from seconds to floating-point hours.
TIME_SHIELD_CONSTEXPR fts_t hour_to_fsec(T hr) noexcept
Converts a timestamp from hours to floating-point seconds.
TIME_SHIELD_CONSTEXPR T ms_part(ts_ms_t ts) noexcept
Get the millisecond part of the timestamp.
TIME_SHIELD_CONSTEXPR fts_t min_to_fsec(T min) noexcept
Converts a timestamp from minutes to floating-point seconds.
TIME_SHIELD_CONSTEXPR ts_ms_t sec_to_ms_impl(T t, std::true_type) noexcept
Helper function for converting seconds to milliseconds (floating-point version).
TIME_SHIELD_CONSTEXPR T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR ts_ms_t hour_to_ms_impl(T t, std::true_type) noexcept
Helper function for converting hours to milliseconds (floating-point version).
TIME_SHIELD_CONSTEXPR T1 sec_to_hour(T2 ts) noexcept
Converts a timestamp from seconds to hours.
ts_ms_t fsec_to_ms(fts_t ts) noexcept
Converts a floating-point timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR ts_t hour_to_sec_impl(T t, std::true_type) noexcept
Helper function for converting hours to seconds (floating-point version).
TIME_SHIELD_CONSTEXPR T1 hour_to_sec(T2 ts) noexcept
Converts a timestamp from hours to seconds.
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.
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
T ms_of_sec() noexcept
Get the millisecond part of the current second.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
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.