Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Clock.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 * \see commentary in https://stackoverflow.com/questions/35282308/convert-between-c11-clocks
25 *
26 * \note - for some cases, the conversion is estimated, and may vary slightly from run run to run.
27 * \note - range overload is both HANDY for normal case, and CRITICAL for case where we approximate, so that valid ranges
28 * always map to valid ranges (one jitter, not two). Note also - use of template/template param for RANGE is to avoid mutual inclusion issues.
29 */
30 template <typename DESTINATION_CLOCK_T, typename SOURCE_CLOCK_T, typename DURATION_T>
31 typename DESTINATION_CLOCK_T::time_point clock_cast (chrono::time_point<SOURCE_CLOCK_T, DURATION_T> tp);
32 template <typename DESTINATION_CLOCK_T, template <typename> typename RANGE, typename SOURCE_CLOCK_T, typename DURATION_T>
33 RANGE<typename DESTINATION_CLOCK_T::time_point> clock_cast (RANGE<chrono::time_point<SOURCE_CLOCK_T, DURATION_T>> tpRange);
34
35 /**
36 * AppStartZeroedClock is just like BASE_CLOCK_T, except that its time values are magically adjusted so that
37 * zero corresponds to when the application (code starts) begins. This defaults to using the base clocks
38 * duration representation, but its often simpler to use Time::DurationSeconds as this value (so stuff natively prints in seconds).
39 */
40 template <typename BASE_CLOCK_T, typename DURATION_T = typename BASE_CLOCK_T::duration>
42 private:
43 using Implementation_ = BASE_CLOCK_T;
44
45 public:
46 using duration = DURATION_T;
47 using rep = typename duration::rep;
48 using period = typename duration::period;
49 using time_point = chrono::time_point<AppStartZeroedClock<BASE_CLOCK_T, DURATION_T>>;
50 static constexpr bool is_steady = Implementation_::is_steady;
51
52 public:
53 [[nodiscard]] static time_point now () noexcept;
54
55 private:
56 static const inline duration kTimeAppStartedOffset_ = chrono::duration_cast<duration> (BASE_CLOCK_T::now ().time_since_epoch ());
57 };
58
59}
60
61/*
62 ********************************************************************************
63 ***************************** Implementation Details ***************************
64 ********************************************************************************
65 */
66#include "Clock.inl"
67
68#endif /*_Stroika_Foundation_Time_Clock_h_*/