Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Foundation/Time/Common.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Foundation::Time {
6
7 template <typename CLOCK_T, typename DURATION_T>
8 auto Pin2SafeSeconds (const chrono::time_point<CLOCK_T, DURATION_T>& tp) -> chrono::time_point<CLOCK_T, DURATION_T>
9 {
10 /*
11 * Intuitively, you might expect to be able to just use chrono::seconds::min () and chrono::seconds::max (). But
12 * converting big integers to / from floats, and back is lossy and imprecise. So a little fudge-factor appears needed
13 * to avoid failures, and pragmatically, should cause no problems.
14 *
15 * --LGP 2023-11-28
16 */
17#if 1
18 static constexpr auto kMin_ = chrono::time_point_cast<chrono::seconds> (
19 chrono::time_point<CLOCK_T, DURATION_T>{chrono::seconds{chrono::seconds::min ().count () + 1000}});
20 static constexpr auto kMax_ = chrono::time_point_cast<chrono::seconds> (
21 chrono::time_point<CLOCK_T, DURATION_T>{chrono::seconds{chrono::seconds::max ().count () - 1000}});
22#else
23 static constexpr auto kMin = chrono::time_point<CLOCK_T, DURATION_T>{chrono::seconds::min ()};
24 static constexpr auto kMax_ = chrono::time_point<CLOCK_T, DURATION_T>{chrono::seconds::max ()};
25#endif
26#if qStroika_Foundation_Debug_AssertionsChecked
27 [[maybe_unused]] static auto test1 = chrono::time_point_cast<chrono::seconds> (kMax_); // verify the +1000/-1000 stuff enuf
28 [[maybe_unused]] static auto test2 = chrono::time_point_cast<chrono::seconds> (kMin_);
29#endif
30
31 typename DURATION_T::rep tpSeconds = tp.time_since_epoch ().count () * DURATION_T::period::den / DURATION_T::period::num;
32 if (tpSeconds > static_cast<typename DURATION_T::rep> (chrono::seconds::max ().count ())) {
33 return kMax_;
34 }
35 if (tpSeconds < static_cast<typename DURATION_T::rep> (chrono::seconds::min ().count ())) [[unlikely]] {
36 return kMin_;
37 }
38 return tp;
39 }
40
41}
auto Pin2SafeSeconds(const chrono::time_point< CLOCK_T, DURATION_T > &tp) -> chrono::time_point< CLOCK_T, DURATION_T >