23 std::cout <<
"Querying NTP server..." << std::endl;
24 if (!client.
query()) {
25 std::cerr <<
"Failed to query NTP server. Error code: "
33 auto now = std::chrono::system_clock::now();
34 auto now_time_t = std::chrono::system_clock::to_time_t(
now);
35 std::cout <<
"Local time: "
36 << std::put_time(std::gmtime(&now_time_t),
"%Y-%m-%d %H:%M:%S")
40 auto corrected = std::chrono::system_clock::time_point(
42 auto corrected_time_t = std::chrono::system_clock::to_time_t(corrected);
43 std::cout <<
"Corrected time: "
44 << std::put_time(std::gmtime(&corrected_time_t),
"%Y-%m-%d %H:%M:%S")
47 std::cout <<
"Offset (us): " << offset_us << std::endl;
49 std::cout <<
"Press Enter to exit..." << std::endl;
56 std::cout <<
"NtpClient is supported only on Windows." << std::endl;
Simple Windows-only NTP client for measuring time offset.
int64_t get_utc_time_us() const noexcept
Returns current UTC time in microseconds based on last NTP offset.
int64_t get_offset_us() const noexcept
Returns the last measured offset in microseconds.
int get_last_error_code() const noexcept
Returns last WinSock error code (if any).
bool query()
Queries the NTP server and updates the local offset.
void init()
Initializes the Time Shield library.
const ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
Initialization helpers for the Time Shield library.
Main namespace for the Time Shield library.
Simple NTP client for querying time offset from NTP servers.