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

Namespaces

namespace  anonymous_namespace{fast_date.hpp}
 

Classes

struct  DaySplit
 
struct  DueTimer
 Helper structure that represents a timer ready to run. More...
 
class  FakeNtpRunner
 Fake runner for tests without network access. More...
 
struct  FastDate
 
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

TIME_SHIELD_CONSTEXPR DaySplit split_unix_day (ts_t p_ts) noexcept
 Split UNIX seconds into whole days and seconds-of-day.
 
TIME_SHIELD_CONSTEXPR int64_t fast_days_from_date_constexpr (int64_t p_year, int p_month, int p_day) noexcept
 Convert date to days since Unix epoch using a fast constexpr algorithm.
 
int64_t fast_days_from_date (int64_t p_year, int p_month, int p_day) noexcept
 Convert date to days since Unix epoch using a fast algorithm.
 
TIME_SHIELD_CONSTEXPR FastDate fast_date_from_days_constexpr (int64_t p_days) noexcept
 Convert days since Unix epoch to date using a fast constexpr algorithm.
 
FastDate fast_date_from_days (int64_t p_days) noexcept
 Convert days since Unix epoch to date using a fast algorithm.
 
TIME_SHIELD_CONSTEXPR int64_t fast_year_from_days_constexpr (int64_t p_days) noexcept
 Convert days since Unix epoch to year using a fast constexpr algorithm.
 
int64_t fast_year_from_days (int64_t p_days) noexcept
 Convert days since Unix epoch to year using a fast algorithm.
 
template<class T>
TIME_SHIELD_CONSTEXPR T floor_div (T a, T b) noexcept
 Floor division for positive divisor.
 
template<class T>
TIME_SHIELD_CONSTEXPR T floor_mod (T a, T b) noexcept
 Floor-mod for positive modulus (returns r in [0..b)).
 
TIME_SHIELD_CONSTEXPR uint64_t mul_hi_u64_constexpr (uint64_t p_a, uint64_t p_b) noexcept
 Return the high 64 bits of a 64x64-bit multiplication (constexpr variant).
 
uint64_t mul_hi_u64 (uint64_t p_a, uint64_t p_b) noexcept
 Return the high 64 bits of a 64x64-bit multiplication.
 
uint64_t mul_shift_u64 (uint64_t p_x, uint64_t p_c) noexcept
 Alias for mul_hi_u64 used for shift-by-64 operations.
 
TIME_SHIELD_CONSTEXPR uint64_t mul_shift_u64_constexpr (uint64_t p_x, uint64_t p_c) noexcept
 Alias for mul_hi_u64_constexpr used for shift-by-64 operations.
 
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.

◆ fast_date_from_days()

FastDate time_shield::detail::fast_date_from_days ( int64_t p_days)
inlinenoexcept

Convert days since Unix epoch to date using a fast algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 156 of file fast_date.hpp.

◆ fast_date_from_days_constexpr()

TIME_SHIELD_CONSTEXPR FastDate time_shield::detail::fast_date_from_days_constexpr ( int64_t p_days)
inlinenoexcept

Convert days since Unix epoch to date using a fast constexpr algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 118 of file fast_date.hpp.

◆ fast_days_from_date()

int64_t time_shield::detail::fast_days_from_date ( int64_t p_year,
int p_month,
int p_day )
inlinenoexcept

Convert date to days since Unix epoch using a fast algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 90 of file fast_date.hpp.

◆ fast_days_from_date_constexpr()

TIME_SHIELD_CONSTEXPR int64_t time_shield::detail::fast_days_from_date_constexpr ( int64_t p_year,
int p_month,
int p_day )
inlinenoexcept

Convert date to days since Unix epoch using a fast constexpr algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 59 of file fast_date.hpp.

◆ fast_year_from_days()

int64_t time_shield::detail::fast_year_from_days ( int64_t p_days)
inlinenoexcept

Convert days since Unix epoch to year using a fast algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 220 of file fast_date.hpp.

◆ fast_year_from_days_constexpr()

TIME_SHIELD_CONSTEXPR int64_t time_shield::detail::fast_year_from_days_constexpr ( int64_t p_days)
inlinenoexcept

Convert days since Unix epoch to year using a fast constexpr algorithm.

Note
Inspired by the algorithm described in: https://www.benjoffe.com/fast-date-64 This implementation is written from scratch (no code copied).

Definition at line 194 of file fast_date.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.

◆ floor_div()

template<class T>
TIME_SHIELD_CONSTEXPR T time_shield::detail::floor_div ( T a,
T b )
inlinenoexcept

Floor division for positive divisor.

Definition at line 14 of file floor_math.hpp.

◆ floor_mod()

template<class T>
TIME_SHIELD_CONSTEXPR T time_shield::detail::floor_mod ( T a,
T b )
inlinenoexcept

Floor-mod for positive modulus (returns r in [0..b)).

Definition at line 23 of file floor_math.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.

◆ mul_hi_u64()

uint64_t time_shield::detail::mul_hi_u64 ( uint64_t p_a,
uint64_t p_b )
inlinenoexcept

Return the high 64 bits of a 64x64-bit multiplication.

Definition at line 35 of file mul_hi.hpp.

◆ mul_hi_u64_constexpr()

TIME_SHIELD_CONSTEXPR uint64_t time_shield::detail::mul_hi_u64_constexpr ( uint64_t p_a,
uint64_t p_b )
inlinenoexcept

Return the high 64 bits of a 64x64-bit multiplication (constexpr variant).

Definition at line 19 of file mul_hi.hpp.

◆ mul_shift_u64()

uint64_t time_shield::detail::mul_shift_u64 ( uint64_t p_x,
uint64_t p_c )
inlinenoexcept

Alias for mul_hi_u64 used for shift-by-64 operations.

Definition at line 47 of file mul_hi.hpp.

◆ mul_shift_u64_constexpr()

TIME_SHIELD_CONSTEXPR uint64_t time_shield::detail::mul_shift_u64_constexpr ( uint64_t p_x,
uint64_t p_c )
inlinenoexcept

Alias for mul_hi_u64_constexpr used for shift-by-64 operations.

Definition at line 52 of file mul_hi.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.

◆ split_unix_day()

TIME_SHIELD_CONSTEXPR DaySplit time_shield::detail::split_unix_day ( ts_t p_ts)
inlinenoexcept

Split UNIX seconds into whole days and seconds-of-day.

Definition at line 39 of file fast_date.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 548 of file ntp_time_service.hpp.