qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::TimePoint Class Reference

Represents a point in time with nanosecond precision. More...

#include <timestamp.h>

Inheritance diagram for qb::TimePoint:

Public Types

using rep = uint64_t
 Type used for nanosecond representation.
using chrono_time_point
 Underlying std::chrono time point type.

Public Member Functions

constexpr TimePoint () noexcept=default
 Default constructor, initializes to epoch.
constexpr TimePoint (rep nanoseconds) noexcept
 Constructs a time point with specified time since epoch.
template<typename Clock, typename ChronoDuration>
 TimePoint (const std::chrono::time_point< Clock, ChronoDuration > &time_point) noexcept
 Constructs a time point from std::chrono::time_point.
constexpr TimePoint (const TimePoint &) noexcept=default
constexpr TimePoint (TimePoint &&) noexcept=default
constexpr TimePointoperator= (const TimePoint &) noexcept=default
constexpr TimePointoperator= (TimePoint &&) noexcept=default
chrono_time_point to_chrono () const noexcept
 Converts to std::chrono::time_point.
template<typename Clock, typename ChronoDuration = typename Clock::duration>
std::chrono::time_point< Clock, ChronoDuration > to () const noexcept
 Converts to any std::chrono::time_point.
constexpr rep days () const noexcept
 Gets the time in days since epoch.
constexpr double days_float () const noexcept
 Gets the time in days since epoch with fractional precision.
constexpr rep hours () const noexcept
 Gets the time in hours since epoch.
constexpr double hours_float () const noexcept
 Gets the time in hours since epoch with fractional precision.
constexpr rep minutes () const noexcept
 Gets the time in minutes since epoch.
constexpr double minutes_float () const noexcept
 Gets the time in minutes since epoch with fractional precision.
constexpr rep seconds () const noexcept
 Gets the time in seconds since epoch.
constexpr double seconds_float () const noexcept
 Gets the time in seconds since epoch with fractional precision.
constexpr rep milliseconds () const noexcept
 Gets the time in milliseconds since epoch.
constexpr double milliseconds_float () const noexcept
 Gets the time in milliseconds since epoch with fractional precision.
constexpr rep microseconds () const noexcept
 Gets the time in microseconds since epoch.
constexpr double microseconds_float () const noexcept
 Gets the time in microseconds since epoch with fractional precision.
constexpr rep nanoseconds () const noexcept
 Gets the time in nanoseconds since epoch.
constexpr double nanoseconds_float () const noexcept
 Gets the time in nanoseconds since epoch with double precision.
Duration time_since_epoch () const noexcept
 Gets the duration since epoch.
constexpr rep count () const noexcept
 Gets the total time in nanoseconds since epoch.
std::string format (std::string_view format) const
 Formats the time point as a string.
std::string to_iso8601 () const
 Converts to ISO8601 string.
TimePointoperator+= (const Duration &duration) noexcept
 Adds a duration to this time point.
TimePointoperator-= (const Duration &duration) noexcept
 Subtracts a duration from this time point.

Static Public Member Functions

static constexpr TimePoint epoch () noexcept
 Represents the epoch (1970-01-01 00:00:00 UTC)
static TimePoint now () noexcept
 Gets current system time.
static constexpr TimePoint from_days (int64_t days) noexcept
 Factory method to create a TimePoint from days since epoch.
static constexpr TimePoint from_hours (int64_t hours) noexcept
 Factory method to create a TimePoint from hours since epoch.
static constexpr TimePoint from_minutes (int64_t minutes) noexcept
 Factory method to create a TimePoint from minutes since epoch.
static constexpr TimePoint from_seconds (int64_t seconds) noexcept
 Factory method to create a TimePoint from seconds since epoch.
static constexpr TimePoint from_milliseconds (int64_t ms) noexcept
 Factory method to create a TimePoint from milliseconds since epoch.
static constexpr TimePoint from_microseconds (int64_t us) noexcept
 Factory method to create a TimePoint from microseconds since epoch.
static constexpr TimePoint from_nanoseconds (int64_t ns) noexcept
 Factory method to create a TimePoint from nanoseconds since epoch.
static std::optional< TimePointfrom_iso8601 (std::string_view iso8601) noexcept
 Creates a TimePoint from ISO8601 string.
static std::optional< TimePointparse (std::string_view time_string, std::string_view format) noexcept
 Factory method to parse a string into a TimePoint.
static uint64_t read_tsc () noexcept
 Reads CPU timestamp counter.
static uint64_t nano () noexcept
 Gets the current time in nanoseconds since epoch.

Protected Attributes

rep _time_since_epoch {0}
 Time in nanoseconds since epoch.

Detailed Description

Represents a point in time with nanosecond precision.

TimePoint provides a platform-independent way to represent moments in time with high precision. It supports arithmetic operations with Duration objects and provides conversions to various time units and formats.

Member Typedef Documentation

◆ chrono_time_point

Initial value:
std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<rep, std::nano>>

Underlying std::chrono time point type.

Constructor & Destructor Documentation

◆ TimePoint() [1/2]

qb::TimePoint::TimePoint ( rep nanoseconds)
inlineexplicitconstexprnoexcept

Constructs a time point with specified time since epoch.

Parameters
nanosecondsTime in nanoseconds since epoch

◆ TimePoint() [2/2]

template<typename Clock, typename ChronoDuration>
qb::TimePoint::TimePoint ( const std::chrono::time_point< Clock, ChronoDuration > & time_point)
inlineexplicitnoexcept

Constructs a time point from std::chrono::time_point.

Template Parameters
ClockThe clock type
DurationThe duration type
Parameters
time_pointA std::chrono time point

Member Function Documentation

◆ now()

TimePoint qb::TimePoint::now ( )
inlinestaticnoexcept

Gets current system time.

Returns
TimePoint representing current system time

◆ to_chrono()

chrono_time_point qb::TimePoint::to_chrono ( ) const
inlinenodiscardnoexcept

Converts to std::chrono::time_point.

Returns
Equivalent std::chrono::time_point

◆ to()

template<typename Clock, typename ChronoDuration = typename Clock::duration>
std::chrono::time_point< Clock, ChronoDuration > qb::TimePoint::to ( ) const
inlinenodiscardnoexcept

Converts to any std::chrono::time_point.

Template Parameters
ClockThe target clock type
DurationThe target duration type
Returns
Converted time point

◆ from_days()

constexpr TimePoint qb::TimePoint::from_days ( int64_t days)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from days since epoch.

Parameters
daysNumber of days since epoch
Returns
A TimePoint at the specified time

◆ from_hours()

constexpr TimePoint qb::TimePoint::from_hours ( int64_t hours)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from hours since epoch.

Parameters
hoursNumber of hours since epoch
Returns
A TimePoint at the specified time

◆ from_minutes()

constexpr TimePoint qb::TimePoint::from_minutes ( int64_t minutes)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from minutes since epoch.

Parameters
minutesNumber of minutes since epoch
Returns
A TimePoint at the specified time

◆ from_seconds()

constexpr TimePoint qb::TimePoint::from_seconds ( int64_t seconds)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from seconds since epoch.

Parameters
secondsNumber of seconds since epoch
Returns
A TimePoint at the specified time

◆ from_milliseconds()

constexpr TimePoint qb::TimePoint::from_milliseconds ( int64_t ms)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from milliseconds since epoch.

Parameters
msNumber of milliseconds since epoch
Returns
A TimePoint at the specified time

◆ from_microseconds()

constexpr TimePoint qb::TimePoint::from_microseconds ( int64_t us)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from microseconds since epoch.

Parameters
usNumber of microseconds since epoch
Returns
A TimePoint at the specified time

◆ from_nanoseconds()

constexpr TimePoint qb::TimePoint::from_nanoseconds ( int64_t ns)
inlinestaticnodiscardconstexprnoexcept

Factory method to create a TimePoint from nanoseconds since epoch.

Parameters
nsNumber of nanoseconds since epoch
Returns
A TimePoint at the specified time

◆ from_iso8601()

std::optional< TimePoint > qb::TimePoint::from_iso8601 ( std::string_view iso8601)
inlinestaticnodiscardnoexcept

Creates a TimePoint from ISO8601 string.

Parameters
iso8601ISO8601 formatted date-time string
Returns
Optional TimePoint, empty if parsing failed

◆ parse()

std::optional< TimePoint > qb::TimePoint::parse ( std::string_view time_string,
std::string_view format )
inlinestaticnodiscardnoexcept

Factory method to parse a string into a TimePoint.

Parameters
time_stringTime string
formatFormat string (strptime/std::get_time compatible)
Returns
Optional TimePoint, empty if parsing failed

◆ days()

rep qb::TimePoint::days ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in days since epoch.

Returns
Number of whole days

◆ days_float()

double qb::TimePoint::days_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in days since epoch with fractional precision.

Returns
Number of days with decimal point

◆ hours()

rep qb::TimePoint::hours ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in hours since epoch.

Returns
Number of whole hours

◆ hours_float()

double qb::TimePoint::hours_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in hours since epoch with fractional precision.

Returns
Number of hours with decimal point

◆ minutes()

rep qb::TimePoint::minutes ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in minutes since epoch.

Returns
Number of whole minutes

◆ minutes_float()

double qb::TimePoint::minutes_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in minutes since epoch with fractional precision.

Returns
Number of minutes with decimal point

◆ seconds()

rep qb::TimePoint::seconds ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in seconds since epoch.

Returns
Number of whole seconds

◆ seconds_float()

double qb::TimePoint::seconds_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in seconds since epoch with fractional precision.

Returns
Number of seconds with decimal point

◆ milliseconds()

rep qb::TimePoint::milliseconds ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in milliseconds since epoch.

Returns
Number of whole milliseconds

◆ milliseconds_float()

double qb::TimePoint::milliseconds_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in milliseconds since epoch with fractional precision.

Returns
Number of milliseconds with decimal point

◆ microseconds()

rep qb::TimePoint::microseconds ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in microseconds since epoch.

Returns
Number of whole microseconds

◆ microseconds_float()

double qb::TimePoint::microseconds_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in microseconds since epoch with fractional precision.

Returns
Number of microseconds with decimal point

◆ nanoseconds()

rep qb::TimePoint::nanoseconds ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in nanoseconds since epoch.

Returns
Number of nanoseconds

◆ nanoseconds_float()

double qb::TimePoint::nanoseconds_float ( ) const
inlinenodiscardconstexprnoexcept

Gets the time in nanoseconds since epoch with double precision.

Returns
Number of nanoseconds as double

◆ time_since_epoch()

Duration qb::TimePoint::time_since_epoch ( ) const
inlinenodiscardnoexcept

Gets the duration since epoch.

Returns
Duration object representing time since epoch

◆ count()

rep qb::TimePoint::count ( ) const
inlinenodiscardconstexprnoexcept

Gets the total time in nanoseconds since epoch.

Returns
Time in nanoseconds

◆ format()

std::string qb::TimePoint::format ( std::string_view format) const
inlinenodiscard

Formats the time point as a string.

Parameters
formatFormat string (strftime compatible)
Returns
Formatted string representation

◆ to_iso8601()

std::string qb::TimePoint::to_iso8601 ( ) const
inlinenodiscard

Converts to ISO8601 string.

Returns
ISO8601 formatted date-time string

◆ read_tsc()

uint64_t qb::TimePoint::read_tsc ( )
inlinestaticnoexcept

Reads CPU timestamp counter.

Returns
Raw TSC value (platform dependent)

◆ operator+=()

TimePoint & qb::TimePoint::operator+= ( const Duration & duration)
inlinenoexcept

Adds a duration to this time point.

Parameters
durationDuration to add
Returns
Reference to this time point after addition

◆ operator-=()

TimePoint & qb::TimePoint::operator-= ( const Duration & duration)
inlinenoexcept

Subtracts a duration from this time point.

Parameters
durationDuration to subtract
Returns
Reference to this time point after subtraction

◆ nano()

uint64_t qb::TimePoint::nano ( )
inlinestaticnoexcept

Gets the current time in nanoseconds since epoch.

Returns
Nanoseconds since epoch