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

Simple Windows-only NTP client for measuring time offset. More...

#include <ntp_client.hpp>

Classes

struct  ntp_packet
 Структура пакета NTP Total: 384 bits or 48 bytes. More...
 

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 get_offset_us () const noexcept
 Returns the last measured offset in microseconds.
 
int64_t get_utc_time_us () const noexcept
 Returns current UTC time in microseconds based on last NTP offset.
 
int64_t get_utc_time_ms () const noexcept
 Returns current UTC time in milliseconds based on last NTP offset.
 
time_t get_utc_time () const noexcept
 Returns current UTC time as time_t (seconds since Unix epoch).
 
int get_last_error_code () const noexcept
 Returns last WinSock error code (if any).
 

Private Member Functions

void fill_packet (ntp_packet &pkt) const
 Converts local time to NTP timestamp format.
 
bool parse_packet (const ntp_packet &pkt, int64_t &result_offset_us) const
 Parses response and calculates offset.
 

Private Attributes

std::string m_host
 
int m_port = 123
 
std::atomic< int64_t > m_offset_us {0}
 
std::atomic< bool > m_is_success {false}
 

Static Private Attributes

static constexpr int64_t NTP_TIMESTAMP_DELTA = 2208988800ll
 Seconds between 1900 and 1970 epochs.
 
static thread_local int s_last_error_code = 0
 

Detailed Description

Simple Windows-only NTP client for measuring time offset.

Definition at line 34 of file ntp_client.hpp.

Constructor & Destructor Documentation

◆ NtpClient()

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

Constructs NTP client with specified host and port.

Definition at line 37 of file ntp_client.hpp.

Member Function Documentation

◆ fill_packet()

void time_shield::NtpClient::fill_packet ( ntp_packet & pkt) const
inlineprivate

Converts local time to NTP timestamp format.

Definition at line 182 of file ntp_client.hpp.

◆ get_last_error_code()

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

Returns last WinSock error code (if any).

Definition at line 145 of file ntp_client.hpp.

◆ get_offset_us()

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

Returns the last measured offset in microseconds.

Definition at line 120 of file ntp_client.hpp.

◆ get_utc_time()

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

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

Returns
UTC time in seconds.

Definition at line 139 of file ntp_client.hpp.

◆ get_utc_time_ms()

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

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

Returns
Current UTC time in ms.

Definition at line 133 of file ntp_client.hpp.

◆ get_utc_time_us()

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

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

Returns
Current UTC time in µs.

Definition at line 126 of file ntp_client.hpp.

◆ parse_packet()

bool time_shield::NtpClient::parse_packet ( const ntp_packet & pkt,
int64_t & result_offset_us ) const
inlineprivate

Parses response and calculates offset.

Definition at line 195 of file ntp_client.hpp.

◆ query()

bool time_shield::NtpClient::query ( )
inline

Queries the NTP server and updates the local offset.

Returns
true if successful.

Definition at line 44 of file ntp_client.hpp.

◆ success()

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

Returns whether the last NTP query was successful.

Returns
True if the last offset measurement succeeded.

Definition at line 115 of file ntp_client.hpp.

Member Data Documentation

◆ m_host

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

Definition at line 175 of file ntp_client.hpp.

◆ m_is_success

std::atomic<bool> time_shield::NtpClient::m_is_success {false}
private

Definition at line 178 of file ntp_client.hpp.

◆ m_offset_us

std::atomic<int64_t> time_shield::NtpClient::m_offset_us {0}
private

Definition at line 177 of file ntp_client.hpp.

◆ m_port

int time_shield::NtpClient::m_port = 123
private

Definition at line 176 of file ntp_client.hpp.

◆ NTP_TIMESTAMP_DELTA

int64_t time_shield::NtpClient::NTP_TIMESTAMP_DELTA = 2208988800ll
staticconstexprprivate

Seconds between 1900 and 1970 epochs.

Definition at line 150 of file ntp_client.hpp.

◆ s_last_error_code

thread_local int time_shield::NtpClient::s_last_error_code = 0
staticprivate

Definition at line 179 of file ntp_client.hpp.


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