Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Clock.h File Reference
#include "Stroika/Foundation/StroikaPreComp.h"
#include <chrono>
#include "Stroika/Foundation/Common/Common.h"
#include "Clock.inl"

Go to the source code of this file.

Classes

struct  Stroika::Foundation::Time::AppStartZeroedClock< BASE_CLOCK_T, DURATION_T >
 

Namespaces

namespace  Stroika::Foundation
 

Functions

template<typename DESTINATION_CLOCK_T , typename SOURCE_CLOCK_T , typename DURATION_T >
DESTINATION_CLOCK_T::time_point Stroika::Foundation::Time::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::chrono::clock_cast (through experiment/approximation), and ranges of time_points...
 

Detailed Description

Note
Code-Status: Alpha

Definition in file Clock.h.

Function Documentation

◆ clock_cast()

template<typename DESTINATION_CLOCK_T , typename SOURCE_CLOCK_T , typename DURATION_T >
DESTINATION_CLOCK_T::time_point Stroika::Foundation::Time::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::chrono::clock_cast (through experiment/approximation), and ranges of time_points...

Background: std::chrono::clock_cast is a C++20 standard library function template used to convert a std::chrono::time_point from one clock to another (e.g., system_clock to utc_clock or file_clock).

But - it only supports few cases. This function supports those, but also ALL others by approximating.

See also
commentary in https://stackoverflow.com/questions/35282308/convert-between-c11-clocks
Note
- for some cases, the conversion is estimated, and may vary slightly from run run to run.
- range overload is both HANDY for normal case, and CRITICAL for case where we approximate, so that valid ranges always map to valid ranges (one jitter, not two). Note also - use of template/template param for RANGE is to avoid mutual inclusion issues.
Example Usage
// background: using TimePointSeconds = time_point<RealtimeClock, DurationSeconds>;
// using DisplayedRealtimeClock = AppStartZeroedClock<RealtimeClock, DurationSeconds>;
TimePointSeconds tickCount = GetTickCount (); // tick count not zero-based (zero at app start)
DisplayedRealtimeClock::time_point secondsSinceAppStart = clock_cast<DisplayedRealtimeClock> (tickCount);
Example Usage
// Since the adjustment to clocks is not gauranteed consistent across calls, its handy sometimes
// to do several at a time to assure they are all mutually consistent (and preserve distance etc).
TimePointSeconds start = GetTickCount ();
Sleep (20ms); // DoSomeWork ();
TimePointSeconds now = GetTickCount ();
Range<TimePointSeconds> taken = clock_cast<DisplayedRealtimeClock> (Range{start, now});

Definition at line 82 of file Clock.inl.