Xclox
C++11 header-only cross-platform date and time library with an asynchronous NTP client
xclox::ntp::Timestamp Class Reference

Timestamp is an immutable class representing a NTP timestamp. More...

#include <timestamp.hpp>

Public Member Functions

 Timestamp (uint64_t value=0)
 
 Timestamp (uint32_t seconds, uint32_t fraction)
 
 Timestamp (std::chrono::system_clock::duration duration)
 
 Timestamp (std::chrono::system_clock::time_point timePoint)
 
uint32_t seconds () const
 Returns the number of seconds of the NTP timestamp.
 
uint32_t fraction () const
 Returns the fraction of a second of the NTP timestamp.
 
uint64_t value () const
 Returns the NTP timestamp in long format.
 
std::chrono::system_clock::duration duration () const
 Returns the NTP timestamp as a system time duration.
 
bool operator== (const Timestamp &other) const
 Equality operator.
 
bool operator!= (const Timestamp &other) const
 Inequality operator.
 
std::chrono::system_clock::duration operator- (const Timestamp &other) const
 Returns the result of subtracting other from this timestamp as a system time duration.
 

Detailed Description

Timestamp is an immutable class representing a NTP timestamp.

A NTP timestamp is a 64-bit, unsigned fixed-point number in seconds relative to the prime epoch "1900-01-01 00:00:00". It includes a 32-bit unsigned seconds field spanning 136 years and a 32-bit fraction field resolving 232 picoseconds. Timestamp handles the fractional part at the resolution of the operating system clock.

Era 0 includes dates from the prime epoch "1900-01-01 00:00:00" to "2036-02-07 06:28:15". The base date for era 1 is established when the timestamp field wraps around, that is, "2036-02-07 06:28:16" corresponds to "1900-01-01 00:00:00".

NTP timestamps are unsigned values, and operations on them produce results in the same or adjacent eras. The only arithmetic operation permitted on NTP timestamps is subtraction. It yields a time duration ranging from 136 years in the past to 136 years in the future.

Timestamp objects are comparable to each other in terms of equality through operators.

Default-constructed Timestamp objects have a value of zero, which is a special case representing unknown or unsynchronized time. Timestamp objects can also be constructed from integer and fractional parts, a system time duration, or a system time point. duration() can be used to convert the timestamp to a system time duration since the prime epoch.

See also
The unit tests in timestamp.h for further details.

Constructor & Destructor Documentation

◆ Timestamp() [1/4]

xclox::ntp::Timestamp::Timestamp ( uint64_t  value = 0)
inlineexplicit
Parameters
valueis a raw NTP timestamp in long format with the first 32 bits being the seconds and the other 32 bits being the fraction of a second.

◆ Timestamp() [2/4]

xclox::ntp::Timestamp::Timestamp ( uint32_t  seconds,
uint32_t  fraction 
)
inlineexplicit
Parameters
secondsis the number of seconds since the prime epoch.
fractionis the fraction of a second.

◆ Timestamp() [3/4]

xclox::ntp::Timestamp::Timestamp ( std::chrono::system_clock::duration  duration)
inlineexplicit
Parameters
durationis a system time duration since the prime epoch.

◆ Timestamp() [4/4]

xclox::ntp::Timestamp::Timestamp ( std::chrono::system_clock::time_point  timePoint)
inlineexplicit
Parameters
timePointis a system time point.

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