Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Clock.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Time_Clock_h_
5#define _Stroika_Foundation_Time_Clock_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <chrono>
10
11#include "Stroika/Foundation/Common/Common.h"
12
13/**
14 * \file
15 *
16 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
17 */
18
19namespace Stroika::Foundation::Time {
20
21 /**
22 * \brief like std::chrono::clock_cast, but supports steady_clock, and others not explicitly supported by std::chrono::clock_cast (through experiment/approximation), and ranges of time_points...
23 *
24 * Background:
25 * std::chrono::clock_cast is a C++20 standard library function template used to convert a std::chrono::time_point
26 * from one clock to another (e.g., system_clock to utc_clock or file_clock).
27 *
28 * But - it only supports few cases. This function supports those, but also ALL others by approximating.
29 *
30 * \see commentary in https://stackoverflow.com/questions/35282308/convert-between-c11-clocks
31 *
32 * \note - for some cases, the conversion is estimated, and may vary slightly from run run to run.
33 * \note - range overload is both HANDY for normal case, and CRITICAL for case where we approximate, so that valid ranges
34 * always map to valid ranges (one jitter, not two). Note also - use of template/template param for RANGE is to avoid mutual inclusion issues.
35 *
36 * \par Example Usage
37 * \code
38 * // background: using TimePointSeconds = time_point<RealtimeClock, DurationSeconds>;
39 * // using DisplayedRealtimeClock = AppStartZeroedClock<RealtimeClock, DurationSeconds>;
40 * TimePointSeconds tickCount = GetTickCount (); // tick count not zero-based (zero at app start)
41 * DisplayedRealtimeClock::time_point secondsSinceAppStart = clock_cast<DisplayedRealtimeClock> (tickCount);
42 * \endcode
43 *
44 * \par Example Usage
45 * \code
46 * // Since the adjustment to clocks is not gauranteed consistent across calls, its handy sometimes
47 * // to do several at a time to assure they are all mutually consistent (and preserve distance etc).
48 * TimePointSeconds start = GetTickCount ();
49 * Sleep (20ms); // DoSomeWork ();
50 * TimePointSeconds now = GetTickCount ();
51 * Range<TimePointSeconds> taken = clock_cast<DisplayedRealtimeClock> (Range{start, now});
52 * \endcode
53 */
54 template <typename DESTINATION_CLOCK_T, typename SOURCE_CLOCK_T, typename DURATION_T>
55 typename DESTINATION_CLOCK_T::time_point clock_cast (chrono::time_point<SOURCE_CLOCK_T, DURATION_T> tp);
56 template <typename DESTINATION_CLOCK_T, template <typename> typename RANGE, typename SOURCE_CLOCK_T, typename DURATION_T>
57 RANGE<typename DESTINATION_CLOCK_T::time_point> clock_cast (RANGE<chrono::time_point<SOURCE_CLOCK_T, DURATION_T>> tpRange);
58
59 /**
60 * AppStartZeroedClock is just like BASE_CLOCK_T, except that its time values are magically adjusted so that
61 * zero corresponds to when the application (code starts) begins. This defaults to using the base clocks
62 * duration representation, but its often simpler to use Time::DurationSeconds as this value (so stuff natively prints in seconds).
63 */
64 template <typename BASE_CLOCK_T, typename DURATION_T = typename BASE_CLOCK_T::duration>
66 private:
67 using Implementation_ = BASE_CLOCK_T;
68
69 public:
70 using duration = DURATION_T;
71 using rep = typename duration::rep;
72 using period = typename duration::period;
73 using time_point = chrono::time_point<AppStartZeroedClock<BASE_CLOCK_T, DURATION_T>>;
74 static constexpr bool is_steady = Implementation_::is_steady;
75
76 public:
77 [[nodiscard]] static time_point now () noexcept;
78
79 private:
80 static const inline duration kTimeAppStartedOffset_ = chrono::duration_cast<duration> (BASE_CLOCK_T::now ().time_since_epoch ());
81 };
82
83}
84
85/*
86 ********************************************************************************
87 ***************************** Implementation Details ***************************
88 ********************************************************************************
89 */
90#include "Clock.inl"
91
92#endif /*_Stroika_Foundation_Time_Clock_h_*/
DESTINATION_CLOCK_T::time_point clock_cast(chrono::time_point< SOURCE_CLOCK_T, DURATION_T > tp)
like std::chrono::clock_cast, but supports steady_clock, and others not explicitly supported by std::...
Definition Clock.inl:82