DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
DotNetDupe::System::TimeProvider Class Referenceabstract

An abstraction that provides time-related functions to enable testability of time-dependent code. More...

#include <TimeProvider.h>

Inheritance diagram for DotNetDupe::System::TimeProvider:

Public Member Functions

virtual ~TimeProvider ()=default
 Virtual destructor for polymorphic cleanup.
TimeSpan GetElapsedTime (int64_t startingTimestamp) const
 Gets the elapsed time since the startingTimestamp was recorded.
TimeSpan GetElapsedTime (int64_t startingTimestamp, int64_t endingTimestamp) const
 Gets the elapsed time between two timestamp values.
virtual DateTimeOffset GetLocalNow () const =0
 Gets a DateTimeOffset value that is set to the current date and time in the local time zone.
virtual int64_t GetTimestamp () const =0
 Gets the current high-frequency counter value in ticks.
virtual int64_t GetTimestampFrequency () const =0
 Gets the frequency of the high-frequency counter in ticks per second.
virtual DateTimeOffset GetUtcNow () const =0
 Gets a DateTimeOffset value that is set to the current Coordinated Universal Time (UTC) date and time.
Public Member Functions inherited from DotNetDupe::System::Object
virtual ~Object ()=default
 Virtual destructor ensuring polymorphic cleanup.
virtual bool Equals (const Object &obj) const
 Determines whether the specified Object is equal to the current Object.
virtual int GetHashCode () const
 Serves as the default hash function.
bool operator== (const Object &obj) const
 Determines reference equality between two objects.
char * ToStringA () const
 Returns narrow-character string representation.
wchar_t * ToStringW () const
 Returns wide-character string representation.

Static Public Member Functions

static TimeProviderPtr GetSystem ()
 Gets a TimeProvider that provides the system's current time and high-resolution timer.
Static Public Member Functions inherited from DotNetDupe::System::Object
static bool Equals (const Object &obj1, const Object &obj2)
 Static helper determining whether two object instances are equal.

Detailed Description

An abstraction that provides time-related functions to enable testability of time-dependent code.

Modeled after .NET 8 System.TimeProvider. Provides mockable access to high-resolution timestamps, UTC now, local now, and elapsed duration calculations.

Note
Thread Safety: TimeProvider implementations are thread-safe.

Definition at line 30 of file TimeProvider.h.

Constructor & Destructor Documentation

◆ ~TimeProvider()

virtual DotNetDupe::System::TimeProvider::~TimeProvider ( )
virtualdefault

Virtual destructor for polymorphic cleanup.

Member Function Documentation

◆ GetElapsedTime() [1/2]

TimeSpan DotNetDupe::System::TimeProvider::GetElapsedTime ( int64_t startingTimestamp) const

Gets the elapsed time since the startingTimestamp was recorded.

Parameters
startingTimestampThe timestamp obtained from a previous call to GetTimestamp.
Returns
A TimeSpan representing elapsed duration.

Compute elapsed time from starting timestamp to current timestamp.

Definition at line 73 of file TimeProvider.cpp.

References GetElapsedTime(), and GetTimestamp().

Referenced by GetElapsedTime(), and GetTimestampFrequency().

◆ GetElapsedTime() [2/2]

TimeSpan DotNetDupe::System::TimeProvider::GetElapsedTime ( int64_t startingTimestamp,
int64_t endingTimestamp ) const

Gets the elapsed time between two timestamp values.

Parameters
startingTimestampThe initial timestamp value.
endingTimestampThe final timestamp value.
Returns
A TimeSpan representing the duration between the timestamps.

Compute elapsed TimeSpan based on timestamp frequency ticks per second.

Definition at line 78 of file TimeProvider.cpp.

References DotNetDupe::System::TimeSpan::TimeSpan(), GetTimestampFrequency(), and DotNetDupe::System::TimeSpan::TicksPerSecond.

◆ GetLocalNow()

virtual DateTimeOffset DotNetDupe::System::TimeProvider::GetLocalNow ( ) const
pure virtual

Gets a DateTimeOffset value that is set to the current date and time in the local time zone.

Returns
Current local DateTimeOffset.

References DOTNETDUPE_API.

Referenced by DotNetDupe::System::DateTimeOffset::Now().

◆ GetSystem()

TimeProviderPtr DotNetDupe::System::TimeProvider::GetSystem ( )
static

Gets a TimeProvider that provides the system's current time and high-resolution timer.

Returns
The default system TimeProvider singleton.

Retrieve singleton shared instance of system TimeProvider.

Definition at line 84 of file TimeProvider.cpp.

References DotNetDupe::System::DynamicCast(), and DotNetDupe::System::SmartPointer< T >::NewShared().

Referenced by GetTimestampFrequency(), DotNetDupe::System::DateTimeOffset::Now(), and DotNetDupe::System::DateTimeOffset::UtcNow().

◆ GetTimestamp()

virtual int64_t DotNetDupe::System::TimeProvider::GetTimestamp ( ) const
pure virtual

Gets the current high-frequency counter value in ticks.

Returns
An integer representing high-frequency timer ticks.

References DOTNETDUPE_API.

Referenced by GetElapsedTime(), and DotNetDupe::System::Diagnostics::Stopwatch::Restart().

◆ GetTimestampFrequency()

virtual int64_t DotNetDupe::System::TimeProvider::GetTimestampFrequency ( ) const
pure virtual

Gets the frequency of the high-frequency counter in ticks per second.

Returns
Frequency in Hertz.

References DOTNETDUPE_API, GetElapsedTime(), and GetSystem().

Referenced by GetElapsedTime().

◆ GetUtcNow()

virtual DateTimeOffset DotNetDupe::System::TimeProvider::GetUtcNow ( ) const
pure virtual

Gets a DateTimeOffset value that is set to the current Coordinated Universal Time (UTC) date and time.

Returns
Current UTC DateTimeOffset.

References DOTNETDUPE_API.

Referenced by DotNetDupe::System::DateTimeOffset::UtcNow().


The documentation for this class was generated from the following files: