SatCat5
satcat5::ptp::Time Class Reference

Detailed Description

High-precision timestamp for use with PTP / IEEE1588.

The "Time" object can be used to represent a time-difference or an absolute time in the TAI epoch, matching the finest resolution supported by IEEE1588-2019.

The internal representation is based on increments of 1/65536 nanoseconds, which is referred to as a "subnanosecond" or "subns".

Definition at line 41 of file ptp_time.h.

#include <ptp_time.h>

Public Member Functions

constexpr Time ()
 Default constructor.
 
constexpr Time (s64 subnanoseconds)
 Single argument constructor is scaled in subnanoseconds. More...
 
constexpr Time (const Time &other)
 Copy constructor.
 
 Time (u64 seconds, u32 nanoseconds, u16 subnanoseconds=0)
 Multi-argument constructor accepts seconds, nanoseconds, and subnanosecods. More...
 
s64 field_secs () const
 Read the "seconds" field without intermediate rounding.
 
u32 field_nsec () const
 Read the "nanoseconds" field, rounding down. More...
 
u64 field_subns () const
 Read the raw "subnanoseconds" field. More...
 
s64 round_secs () const
 Return "seconds" field after rounding to the nearest nanosecond. More...
 
u32 round_nsec () const
 Return "nanoseconds" field after rounding to the nearest nanosecond.
 
bool read_from (satcat5::io::Readable *src)
 Read the standard 10-byte timestamp from a PTP message (e.g., originTimestamp: u48 seconds + u32 nanoseconds) Note: Add correctionField separately to maintain full precision.
 
void write_to (satcat5::io::Writeable *dst) const
 Read or write the 10-byte timestamp from a PTP message. More...
 
void log_to (satcat5::log::LogBuffer &wr) const
 User-readable format for logging. More...
 
u64 correction () const
 Get the correctionField value in subnanoseconds. More...
 
s64 to_datetime () const
 Convert to SatCat5 date/time. More...
 
s64 delta_subns () const
 Convert time-differences to the designated unit. More...
 
s64 delta_nsec () const
 Convert time-differences to the designated unit. More...
 
s64 delta_usec () const
 Convert time-differences to the designated unit. More...
 
s64 delta_msec () const
 Convert time-differences to the designated unit. More...
 
satcat5::ptp::Time abs () const
 Standard arithmetic operations.
 
void operator+= (const satcat5::ptp::Time &other)
 Standard arithmetic operations.
 
void operator-= (const satcat5::ptp::Time &other)
 Standard arithmetic operations.
 
satcat5::ptp::Time operator= (const satcat5::ptp::Time &other)
 Standard arithmetic operations.
 
satcat5::ptp::Time operator+ (const satcat5::ptp::Time &other) const
 Standard arithmetic operations.
 
satcat5::ptp::Time operator- (const satcat5::ptp::Time &other) const
 Standard arithmetic operations.
 
satcat5::ptp::Time operator- () const
 Standard arithmetic operations.
 
void operator*= (unsigned scale)
 Scalar multiply and divide are used for weighted averaging. More...
 
void operator/= (unsigned scale)
 Scalar multiply and divide are used for weighted averaging. More...
 
satcat5::ptp::Time operator* (unsigned scale) const
 Scalar multiply and divide are used for weighted averaging. More...
 
satcat5::ptp::Time operator/ (unsigned scale) const
 Scalar multiply and divide are used for weighted averaging. More...
 
bool operator== (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 
bool operator< (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 
bool operator> (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 
bool operator!= (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 
bool operator<= (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 
bool operator>= (const satcat5::ptp::Time &other) const
 Standard comparison operators.
 

Protected Member Functions

void normalize ()
 

Protected Attributes

s64 m_secs
 
s64 m_subns
 

Constructor & Destructor Documentation

◆ Time() [1/2]

constexpr satcat5::ptp::Time::Time ( s64  subnanoseconds)
inlineexplicitconstexpr

Single argument constructor is scaled in subnanoseconds.

This matches the format used for the PTP "correction" field.

Definition at line 49 of file ptp_time.h.

◆ Time() [2/2]

Time::Time ( u64  seconds,
u32  nanoseconds,
u16  subnanoseconds = 0 
)

Multi-argument constructor accepts seconds, nanoseconds, and subnanosecods.

This matches the format used for the PTP "timestamp" field.

Definition at line 21 of file ptp_time.cc.

Member Function Documentation

◆ correction()

u64 satcat5::ptp::Time::correction ( ) const
inline

Get the correctionField value in subnanoseconds.

For use with field_sec, field_nsec, or write_to methods.

Returns
Residual offset in subnanoseconds.

Definition at line 108 of file ptp_time.h.

◆ delta_msec()

s64 Time::delta_msec ( ) const

Convert time-differences to the designated unit.

The representable range is always at least +/- 24 hours. Times beyond the safe range will return INT64_MIN or INT64_MAX.

Definition at line 61 of file ptp_time.cc.

◆ delta_nsec()

s64 Time::delta_nsec ( ) const

Convert time-differences to the designated unit.

The representable range is always at least +/- 24 hours. Times beyond the safe range will return INT64_MIN or INT64_MAX.

Definition at line 57 of file ptp_time.cc.

◆ delta_subns()

s64 Time::delta_subns ( ) const

Convert time-differences to the designated unit.

The representable range is always at least +/- 24 hours. Times beyond the safe range will return INT64_MIN or INT64_MAX.

Definition at line 55 of file ptp_time.cc.

◆ delta_usec()

s64 Time::delta_usec ( ) const

Convert time-differences to the designated unit.

The representable range is always at least +/- 24 hours. Times beyond the safe range will return INT64_MIN or INT64_MAX.

Definition at line 59 of file ptp_time.cc.

◆ field_nsec()

u32 satcat5::ptp::Time::field_nsec ( ) const
inline

Read the "nanoseconds" field, rounding down.

Use this method in combination with

See also
correction.

Definition at line 66 of file ptp_time.h.

◆ field_subns()

u64 satcat5::ptp::Time::field_subns ( ) const
inline

Read the raw "subnanoseconds" field.

This is equal to 65536 * field_nsec() + correction()

Definition at line 70 of file ptp_time.h.

◆ log_to()

void Time::log_to ( satcat5::log::LogBuffer wr) const

User-readable format for logging.

See also
satcat5::log::Log

Definition at line 83 of file ptp_time.cc.

◆ operator*()

Time Time::operator* ( unsigned  scale) const

Scalar multiply and divide are used for weighted averaging.

Do not use scaling factors larger than ~10000 or it may overflow.

Definition at line 195 of file ptp_time.cc.

◆ operator*=()

void Time::operator*= ( unsigned  scale)

Scalar multiply and divide are used for weighted averaging.

Do not use scaling factors larger than ~10000 or it may overflow.

Definition at line 157 of file ptp_time.cc.

◆ operator/()

Time Time::operator/ ( unsigned  scale) const

Scalar multiply and divide are used for weighted averaging.

Do not use scaling factors larger than ~10000 or it may overflow.

Definition at line 201 of file ptp_time.cc.

◆ operator/=()

void Time::operator/= ( unsigned  scale)

Scalar multiply and divide are used for weighted averaging.

Do not use scaling factors larger than ~10000 or it may overflow.

Definition at line 164 of file ptp_time.cc.

◆ round_secs()

s64 satcat5::ptp::Time::round_secs ( ) const
inline

Return "seconds" field after rounding to the nearest nanosecond.

If you use round_nsec, then you should also use round_sec.

Definition at line 75 of file ptp_time.h.

◆ to_datetime()

s64 Time::to_datetime ( ) const

Convert to SatCat5 date/time.

See also
datetime.h

Definition at line 96 of file ptp_time.cc.

◆ write_to()

void Time::write_to ( satcat5::io::Writeable dst) const

Read or write the 10-byte timestamp from a PTP message.

(e.g., originTimestamp: u48 seconds + u32 nanoseconds) Note: User must write correction to the PTP correctionField to maintain full end-to-end precision.

Definition at line 77 of file ptp_time.cc.


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