Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::NtpClient Class Reference

NTP client for measuring time offset. More...

#include <ntp_client.hpp>

Public Member Functions

 NtpClient (std::string server="pool.ntp.org", int port=123)
 Constructs NTP client with specified host and port.
 
bool query ()
 Queries the NTP server and updates the local offset.
 
bool success () const noexcept
 Returns whether the last NTP query was successful.
 
int64_t offset_us () const noexcept
 Returns the last measured offset in microseconds.
 
int64_t delay_us () const noexcept
 Returns the last measured delay in microseconds.
 
int stratum () const noexcept
 Returns the last received stratum value.
 
int64_t utc_time_us () const noexcept
 Returns current UTC time in microseconds based on last NTP offset.
 
int64_t utc_time_ms () const noexcept
 Returns current UTC time in milliseconds based on last NTP offset.
 
time_t utc_time_sec () const noexcept
 Returns current UTC time as time_t (seconds since Unix epoch).
 
int last_error_code () const noexcept
 Returns last socket error code (if any).
 
 NtpClient ()
 
 NtpClient ()
 

Static Private Member Functions

static int & last_error_code_slot () noexcept
 

Private Attributes

std::string m_host
 
int m_port
 
std::atomic< int64_t > m_offset_us
 
std::atomic< int64_t > m_delay_us
 
std::atomic< int > m_stratum
 
std::atomic< bool > m_is_success
 

Static Private Attributes

static const int k_default_timeout_ms = 5000
 

Detailed Description

NTP client for measuring time offset.

Definition at line 43 of file ntp_client.hpp.

Constructor & Destructor Documentation

◆ NtpClient() [1/3]

time_shield::NtpClient::NtpClient ( std::string server = "pool.ntp.org",
int port = 123 )
inline

Constructs NTP client with specified host and port.

Parameters
serverNTP server host name.
portNTP server port.

Definition at line 48 of file ntp_client.hpp.

◆ NtpClient() [2/3]

time_shield::NtpClient::NtpClient ( )
inline

Definition at line 158 of file ntp_client.hpp.

◆ NtpClient() [3/3]

time_shield::NtpClient::NtpClient ( )
inline

Definition at line 172 of file ntp_client.hpp.

Member Function Documentation

◆ delay_us()

int64_t time_shield::NtpClient::delay_us ( ) const
inlinenoexcept

Returns the last measured delay in microseconds.

Returns
Round-trip delay estimate in microseconds.

Definition at line 117 of file ntp_client.hpp.

◆ last_error_code()

int time_shield::NtpClient::last_error_code ( ) const
inlinenoexcept

Returns last socket error code (if any).

Returns
Error code from last query attempt.

Definition at line 137 of file ntp_client.hpp.

◆ last_error_code_slot()

static int & time_shield::NtpClient::last_error_code_slot ( )
inlinestaticprivatenoexcept

Definition at line 148 of file ntp_client.hpp.

◆ offset_us()

int64_t time_shield::NtpClient::offset_us ( ) const
inlinenoexcept

Returns the last measured offset in microseconds.

Returns
Offset in microseconds (UTC - local realtime).

Definition at line 113 of file ntp_client.hpp.

◆ query()

bool time_shield::NtpClient::query ( )
inline

Queries the NTP server and updates the local offset.

Returns
True when response parsed successfully.
Note
Requires network connectivity and a reachable server.

Definition at line 61 of file ntp_client.hpp.

◆ stratum()

int time_shield::NtpClient::stratum ( ) const
inlinenoexcept

Returns the last received stratum value.

Returns
NTP stratum value.

Definition at line 121 of file ntp_client.hpp.

◆ success()

bool time_shield::NtpClient::success ( ) const
inlinenoexcept

Returns whether the last NTP query was successful.

Returns
True when the last query updated internal state.

Definition at line 109 of file ntp_client.hpp.

◆ utc_time_ms()

int64_t time_shield::NtpClient::utc_time_ms ( ) const
inlinenoexcept

Returns current UTC time in milliseconds based on last NTP offset.

Returns
UTC time in milliseconds using last offset.

Definition at line 129 of file ntp_client.hpp.

◆ utc_time_sec()

time_t time_shield::NtpClient::utc_time_sec ( ) const
inlinenoexcept

Returns current UTC time as time_t (seconds since Unix epoch).

Returns
UTC time in seconds since Unix epoch.

Definition at line 133 of file ntp_client.hpp.

◆ utc_time_us()

int64_t time_shield::NtpClient::utc_time_us ( ) const
inlinenoexcept

Returns current UTC time in microseconds based on last NTP offset.

Returns
UTC time in microseconds using last offset.

Definition at line 125 of file ntp_client.hpp.

Member Data Documentation

◆ k_default_timeout_ms

const int time_shield::NtpClient::k_default_timeout_ms = 5000
staticprivate

Definition at line 146 of file ntp_client.hpp.

◆ m_delay_us

std::atomic<int64_t> time_shield::NtpClient::m_delay_us
private

Definition at line 143 of file ntp_client.hpp.

◆ m_host

std::string time_shield::NtpClient::m_host
private

Definition at line 140 of file ntp_client.hpp.

◆ m_is_success

std::atomic<bool> time_shield::NtpClient::m_is_success
private

Definition at line 145 of file ntp_client.hpp.

◆ m_offset_us

std::atomic<int64_t> time_shield::NtpClient::m_offset_us
private

Definition at line 142 of file ntp_client.hpp.

◆ m_port

int time_shield::NtpClient::m_port
private

Definition at line 141 of file ntp_client.hpp.

◆ m_stratum

std::atomic<int> time_shield::NtpClient::m_stratum
private

Definition at line 144 of file ntp_client.hpp.


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