|
Time Shield Library
C++ library for working with time
|
Facilities for retrieving time using the Network Time Protocol. More...
Files | |
| file | ntp_client.hpp |
| Simple NTP client for querying time offset from NTP servers. | |
| file | wsa_guard.hpp |
| Singleton guard for WinSock initialization. | |
Classes | |
| class | time_shield::NtpClient |
| NTP client for measuring time offset. More... | |
| struct | time_shield::detail::NtpPacket |
| NTP packet layout (48 bytes). More... | |
| class | time_shield::WsaGuard |
| Singleton guard for WinSock initialization. More... | |
| struct | time_shield::NtpSample |
| NTP measurement sample (one server response). More... | |
| struct | time_shield::NtpServerConfig |
| Per-server configuration. More... | |
| struct | time_shield::NtpPoolConfig |
| Pool configuration. More... | |
| class | time_shield::NtpClientPoolT< ClientT > |
| Pool of NTP servers: rate-limited multi-server offset estimation. More... | |
| class | time_shield::BasicPoolRunner< PoolT > |
| Background runner that periodically measures NTP offsets using a pool. More... | |
| class | time_shield::NtpTimeServiceT< RunnerT > |
| Singleton service for background NTP measurements. More... | |
Typedefs | |
| using | time_shield::NtpTimeService = NtpTimeServiceT<detail::FakeNtpRunner> |
| NTP time service alias that uses a fake runner for tests. | |
Enumerations | |
| enum | time_shield::detail::NtpProtoError { time_shield::detail::NTP_EPROTO_BASE = -10000 , time_shield::detail::NTP_E_BAD_MODE = NTP_EPROTO_BASE - 1 , time_shield::detail::NTP_E_BAD_VERSION = NTP_EPROTO_BASE - 2 , time_shield::detail::NTP_E_BAD_LI = NTP_EPROTO_BASE - 3 , time_shield::detail::NTP_E_BAD_STRATUM = NTP_EPROTO_BASE - 4 , time_shield::detail::NTP_E_KOD = NTP_EPROTO_BASE - 5 , time_shield::detail::NTP_E_BAD_TS = NTP_EPROTO_BASE - 6 } |
| Protocol-level error codes for NTP parsing. More... | |
Functions | |
| bool | time_shield::ntp::init (std::chrono::milliseconds interval=std::chrono::seconds(30), bool measure_immediately=true) |
| Initialize NTP time service and start background measurements. | |
| bool | time_shield::ntp::init (int interval_ms, bool measure_immediately=true) |
| Initialize NTP time service using milliseconds. | |
| void | time_shield::ntp::shutdown () |
| Stop NTP time service. | |
| int64_t | time_shield::ntp::offset_us () noexcept |
| Return last estimated offset in microseconds. | |
| int64_t | time_shield::ntp::utc_time_us () noexcept |
| Return current UTC time in microseconds based on offset. | |
| int64_t | time_shield::ntp::utc_time_ms () noexcept |
| Return current UTC time in milliseconds based on offset. | |
| int64_t | time_shield::ntp::utc_time_sec () noexcept |
| Return current UTC time in seconds based on offset. | |
| bool | time_shield::ntp::last_measure_ok () noexcept |
| Return whether last measurement updated the offset. | |
| uint64_t | time_shield::ntp::measure_count () noexcept |
| Return total number of measurement attempts. | |
| uint64_t | time_shield::ntp::fail_count () noexcept |
| Return number of failed measurement attempts. | |
| int64_t | time_shield::ntp::last_update_realtime_us () noexcept |
| Return realtime timestamp of last measurement attempt. | |
| int64_t | time_shield::ntp::last_success_realtime_us () noexcept |
| Return realtime timestamp of last successful measurement. | |
| bool | time_shield::ntp::stale (std::chrono::milliseconds max_age) noexcept |
| Return true when last measurement is older than max_age. | |
Facilities for retrieving time using the Network Time Protocol.
This module contains a minimal client and optional pool/service pipeline capable of querying remote NTP servers to measure the offset between local realtime and network time. It uses WinSock via the WsaGuard helper on Windows and POSIX sockets on Unix platforms.
NtpClient performs a single request/response exchange with one server.NtpClientPool samples multiple servers with rate limiting and backoff.NtpClientPoolRunner runs the pool periodically in a background thread.NtpTimeService wraps the runner in a singleton interface for easy access.For each response, offsets and delays are computed using the standard NTP four-timestamp method:
t1 — client transmit timet2 — server receive timet3 — server transmit timet4 — client receive timeOffset and delay are derived as: offset = ((t2 - t1) + (t3 - t4)) / 2, delay = (t4 - t1) - (t3 - t2).
Pools aggregate per-server offsets using the configured strategy (median, best-delay sample, or median with MAD trimming) and optional exponential smoothing.
WsaGuard (Windows build).| typedef NtpTimeServiceT< NtpClientPoolRunner > time_shield::NtpTimeService = NtpTimeServiceT<detail::FakeNtpRunner> |
NTP time service alias that uses a fake runner for tests.
NTP time service alias that uses the real pool runner.
Definition at line 556 of file ntp_time_service.hpp.
Protocol-level error codes for NTP parsing.
| Enumerator | |
|---|---|
| NTP_EPROTO_BASE | |
| NTP_E_BAD_MODE | |
| NTP_E_BAD_VERSION | |
| NTP_E_BAD_LI | |
| NTP_E_BAD_STRATUM | |
| NTP_E_KOD | |
| NTP_E_BAD_TS | |
Definition at line 42 of file ntp_packet.hpp.
|
inlinenoexcept |
Return number of failed measurement attempts.
Definition at line 636 of file ntp_time_service.hpp.
|
inline |
Initialize NTP time service using milliseconds.
| interval_ms | Measurement interval in milliseconds. |
| measure_immediately | Measure before first sleep if true. |
Definition at line 580 of file ntp_time_service.hpp.
|
inline |
Initialize NTP time service and start background measurements.
| interval | Measurement interval. |
| measure_immediately | Measure before first sleep if true. |
Definition at line 570 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return whether last measurement updated the offset.
Definition at line 622 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return realtime timestamp of last successful measurement.
Definition at line 650 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return realtime timestamp of last measurement attempt.
Definition at line 643 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return total number of measurement attempts.
Definition at line 629 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return last estimated offset in microseconds.
Definition at line 594 of file ntp_time_service.hpp.
|
inline |
Stop NTP time service.
Definition at line 587 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return true when last measurement is older than max_age.
| max_age | Maximum allowed age. |
Definition at line 658 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in milliseconds based on offset.
Definition at line 608 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in seconds based on offset.
Definition at line 615 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in microseconds based on offset.
Definition at line 601 of file ntp_time_service.hpp.