13#if TIME_SHIELD_ENABLE_NTP_CLIENT
24 std::cout <<
"Querying NTP server..." << std::endl;
25 if (!client.
query()) {
26 std::cerr <<
"Failed to query NTP server. Error code: "
31 const int64_t offset_us = client.
offset_us();
34 auto now = std::chrono::system_clock::now();
35 auto now_time_t = std::chrono::system_clock::to_time_t(
now);
36 std::cout <<
"Local time: "
37 << std::put_time(std::gmtime(&now_time_t),
"%Y-%m-%d %H:%M:%S")
41 auto corrected = std::chrono::system_clock::time_point(
43 auto corrected_time_t = std::chrono::system_clock::to_time_t(corrected);
44 std::cout <<
"Corrected time: "
45 << std::put_time(std::gmtime(&corrected_time_t),
"%Y-%m-%d %H:%M:%S")
48 std::cout <<
"Offset (us): " << offset_us << std::endl;
50 std::cout <<
"Press Enter to exit..." << std::endl;
57 std::cout <<
"NtpClient is disabled in this build." << std::endl;
NTP client for measuring time offset.
int64_t offset_us() const noexcept
Returns the last measured offset in microseconds.
int last_error_code() const noexcept
Returns last socket error code (if any).
int64_t utc_time_us() const noexcept
Returns current UTC time in microseconds based on last NTP offset.
bool query()
Queries the NTP server and updates the local offset.
Configuration macros for the library.
void init()
Initializes the Time Shield library.
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.