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

Classes

struct  DueTimer
 Helper structure that represents a timer ready to run. More...
 
class  FakeNtpRunner
 Fake runner for tests without network access. More...
 
class  IUdpTransport
 Abstract UDP transport interface for NTP queries. More...
 
class  NtpClientCore
 Core NTP query logic that parses packets and computes offsets. More...
 
struct  NtpPacket
 NTP packet layout (48 bytes). More...
 
struct  RunningTimerScope
 
struct  ScheduledComparator
 Comparator that orders timers by earliest fire time. More...
 
struct  ScheduledTimer
 Data stored in the priority queue of scheduled timers. More...
 
struct  TimerState
 Internal state shared between Timer and TimerScheduler. More...
 
struct  UdpRequest
 UDP request parameters for NTP transactions. More...
 
class  UdpTransportPosix
 POSIX UDP transport for NTP queries. More...
 
class  UdpTransportWin
 Windows UDP transport for NTP queries. More...
 

Typedefs

using PlatformUdpTransport = UdpTransportWin
 
using RunnerAlias = detail::FakeNtpRunner
 
using TimerClock = std::chrono::steady_clock
 
using TimerCallback = std::function<void()>
 

Enumerations

enum  NtpProtoError {
  NTP_EPROTO_BASE = -10000 , NTP_E_BAD_MODE = NTP_EPROTO_BASE - 1 , NTP_E_BAD_VERSION = NTP_EPROTO_BASE - 2 , NTP_E_BAD_LI = NTP_EPROTO_BASE - 3 ,
  NTP_E_BAD_STRATUM = NTP_EPROTO_BASE - 4 , NTP_E_KOD = NTP_EPROTO_BASE - 5 , NTP_E_BAD_TS = NTP_EPROTO_BASE - 6
}
 Protocol-level error codes for NTP parsing. More...
 

Functions

static uint8_t ntp_li (uint8_t li_vn_mode) noexcept
 Extract leap indicator from LI/VN/Mode field.
 
static uint8_t ntp_vn (uint8_t li_vn_mode) noexcept
 Extract version number from LI/VN/Mode field.
 
static uint8_t ntp_mode (uint8_t li_vn_mode) noexcept
 Extract mode from LI/VN/Mode field.
 
static uint64_t ntp_frac_to_us (uint32_t frac_net) noexcept
 Convert NTP fractional seconds to microseconds.
 
static bool ntp_ts_to_unix_us (uint32_t sec_net, uint32_t frac_net, uint64_t &out_us) noexcept
 Convert NTP timestamp parts to Unix microseconds.
 
static void fill_client_packet (NtpPacket &pkt, uint64_t now_us)
 Fill an NTP client request packet using local time.
 
static bool parse_server_packet (const NtpPacket &pkt, uint64_t arrival_us, int64_t &offset_us, int64_t &delay_us, int &stratum, int &out_error_code) noexcept
 Parse server response and compute offset and delay.
 
std::string trim_copy_ascii (const std::string &s)
 Trim ASCII whitespace from both ends.
 
std::string_view trim_view_ascii (std::string_view v)
 Trim ASCII whitespace from both ends (string_view).
 
void normalise_month_token_lower (const std::string &month, std::string &output)
 Normalize month token to lower-case ASCII using current locale facet.
 
void normalise_month_token_lower (std::string_view month, std::string &output)
 Normalize month token to lower-case ASCII using current locale facet (string_view).
 
bool try_parse_month_index (const std::string &month, int &value)
 Try parse month name token into month index (1..12).
 
bool try_parse_month_index (std::string_view month, int &value)
 Try parse month name token into month index (1..12), string_view overload.
 
int parse_month_index (const std::string &month)
 Parse month name token into month index (1..12).
 
int parse_month_index (std::string_view month)
 Parse month name token into month index (1..12), string_view overload.
 
TIME_SHIELD_CONSTEXPR bool is_ascii_space (char c) noexcept
 Check whether character is ASCII whitespace.
 
TIME_SHIELD_CONSTEXPR bool is_ascii_digit (char c) noexcept
 Check whether character is ASCII digit.
 
TIME_SHIELD_CONSTEXPR void skip_spaces (const char *&p, const char *end) noexcept
 Skip ASCII whitespace.
 
TIME_SHIELD_CONSTEXPR bool parse_2digits (const char *&p, const char *end, int &out) noexcept
 Parse exactly 2 digits into int.
 
TIME_SHIELD_CONSTEXPR bool parse_4digits_year (const char *&p, const char *end, year_t &out) noexcept
 Parse exactly 4 digits into year_t (via int).
 
TIME_SHIELD_CONSTEXPR bool parse_fraction_to_ms (const char *&p, const char *end, int &ms_out) noexcept
 Parse fractional seconds (1..9 digits) and convert to milliseconds.
 
TimerState *& current_timer_state ()
 

Variables

NtpTimeServiceT< RunnerAliasg_ntp_time_service
 

Typedef Documentation

◆ PlatformUdpTransport

◆ RunnerAlias

◆ TimerCallback

using time_shield::detail::TimerCallback = std::function<void()>

Definition at line 41 of file TimerScheduler.hpp.

◆ TimerClock

using time_shield::detail::TimerClock = std::chrono::steady_clock

Definition at line 40 of file TimerScheduler.hpp.

Function Documentation

◆ current_timer_state()

TimerState *& time_shield::detail::current_timer_state ( )
inline

Definition at line 57 of file TimerScheduler.hpp.

◆ fill_client_packet()

static void time_shield::detail::fill_client_packet ( NtpPacket & pkt,
uint64_t now_us )
inlinestatic

Fill an NTP client request packet using local time.

Definition at line 83 of file ntp_packet.hpp.

◆ is_ascii_digit()

TIME_SHIELD_CONSTEXPR bool time_shield::detail::is_ascii_digit ( char c)
inlinenoexcept

Check whether character is ASCII digit.

Definition at line 219 of file time_parser.hpp.

◆ is_ascii_space()

TIME_SHIELD_CONSTEXPR bool time_shield::detail::is_ascii_space ( char c)
inlinenoexcept

Check whether character is ASCII whitespace.

Definition at line 214 of file time_parser.hpp.

◆ normalise_month_token_lower() [1/2]

void time_shield::detail::normalise_month_token_lower ( const std::string & month,
std::string & output )
inline

Normalize month token to lower-case ASCII using current locale facet.

Parameters
monthInput token.
outputOutput lower-case token (overwritten).

Definition at line 97 of file time_parser.hpp.

◆ normalise_month_token_lower() [2/2]

void time_shield::detail::normalise_month_token_lower ( std::string_view month,
std::string & output )
inline

Normalize month token to lower-case ASCII using current locale facet (string_view).

Parameters
monthInput token view.
outputOutput lower-case token (overwritten).

Definition at line 110 of file time_parser.hpp.

◆ ntp_frac_to_us()

static uint64_t time_shield::detail::ntp_frac_to_us ( uint32_t frac_net)
inlinestaticnoexcept

Convert NTP fractional seconds to microseconds.

Definition at line 68 of file ntp_packet.hpp.

◆ ntp_li()

static uint8_t time_shield::detail::ntp_li ( uint8_t li_vn_mode)
inlinestaticnoexcept

Extract leap indicator from LI/VN/Mode field.

Definition at line 53 of file ntp_packet.hpp.

◆ ntp_mode()

static uint8_t time_shield::detail::ntp_mode ( uint8_t li_vn_mode)
inlinestaticnoexcept

Extract mode from LI/VN/Mode field.

Definition at line 63 of file ntp_packet.hpp.

◆ ntp_ts_to_unix_us()

static bool time_shield::detail::ntp_ts_to_unix_us ( uint32_t sec_net,
uint32_t frac_net,
uint64_t & out_us )
inlinestaticnoexcept

Convert NTP timestamp parts to Unix microseconds.

Definition at line 74 of file ntp_packet.hpp.

◆ ntp_vn()

static uint8_t time_shield::detail::ntp_vn ( uint8_t li_vn_mode)
inlinestaticnoexcept

Extract version number from LI/VN/Mode field.

Definition at line 58 of file ntp_packet.hpp.

◆ parse_2digits()

TIME_SHIELD_CONSTEXPR bool time_shield::detail::parse_2digits ( const char *& p,
const char * end,
int & out )
inlinenoexcept

Parse exactly 2 digits into int.

Returns
true on success.

Definition at line 232 of file time_parser.hpp.

◆ parse_4digits_year()

TIME_SHIELD_CONSTEXPR bool time_shield::detail::parse_4digits_year ( const char *& p,
const char * end,
year_t & out )
inlinenoexcept

Parse exactly 4 digits into year_t (via int).

Returns
true on success.

Definition at line 248 of file time_parser.hpp.

◆ parse_fraction_to_ms()

TIME_SHIELD_CONSTEXPR bool time_shield::detail::parse_fraction_to_ms ( const char *& p,
const char * end,
int & ms_out )
inlinenoexcept

Parse fractional seconds (1..9 digits) and convert to milliseconds.

Uses first 3 digits, scales if fewer.

Returns
true on success.

Definition at line 265 of file time_parser.hpp.

◆ parse_month_index() [1/2]

int time_shield::detail::parse_month_index ( const std::string & month)
inline

Parse month name token into month index (1..12).

Parameters
monthMonth token.
Returns
Month index [1..12].
Exceptions
std::invalid_argumentif token is invalid.

Definition at line 187 of file time_parser.hpp.

◆ parse_month_index() [2/2]

int time_shield::detail::parse_month_index ( std::string_view month)
inline

Parse month name token into month index (1..12), string_view overload.

Parameters
monthMonth token view.
Returns
Month index [1..12].
Exceptions
std::invalid_argumentif token is invalid.

Definition at line 200 of file time_parser.hpp.

◆ parse_server_packet()

static bool time_shield::detail::parse_server_packet ( const NtpPacket & pkt,
uint64_t arrival_us,
int64_t & offset_us,
int64_t & delay_us,
int & stratum,
int & out_error_code )
inlinestaticnoexcept

Parse server response and compute offset and delay.

Definition at line 95 of file ntp_packet.hpp.

◆ skip_spaces()

TIME_SHIELD_CONSTEXPR void time_shield::detail::skip_spaces ( const char *& p,
const char * end )
inlinenoexcept

Skip ASCII whitespace.

Definition at line 224 of file time_parser.hpp.

◆ trim_copy_ascii()

std::string time_shield::detail::trim_copy_ascii ( const std::string & s)
inline

Trim ASCII whitespace from both ends.

Definition at line 75 of file time_parser.hpp.

◆ trim_view_ascii()

std::string_view time_shield::detail::trim_view_ascii ( std::string_view v)
inline

Trim ASCII whitespace from both ends (string_view).

Definition at line 85 of file time_parser.hpp.

◆ try_parse_month_index() [1/2]

bool time_shield::detail::try_parse_month_index ( const std::string & month,
int & value )
inline

Try parse month name token into month index (1..12).

Parameters
monthMonth token (e.g. "Jan", "January", case-insensitive).
valueOutput month index in range [1..12].
Returns
True if token matches a supported month name, false otherwise.

Definition at line 125 of file time_parser.hpp.

◆ try_parse_month_index() [2/2]

bool time_shield::detail::try_parse_month_index ( std::string_view month,
int & value )
inline

Try parse month name token into month index (1..12), string_view overload.

Parameters
monthMonth token view (e.g. "Jan", "January", case-insensitive).
valueOutput month index in range [1..12].
Returns
True if token matches a supported month name, false otherwise.

Definition at line 156 of file time_parser.hpp.

Variable Documentation

◆ g_ntp_time_service

NtpTimeServiceT< RunnerAlias > time_shield::detail::g_ntp_time_service

Definition at line 540 of file ntp_time_service.hpp.