Time Shield Library
C++ library for working with time
Toggle main menu visibility
Loading...
Searching...
No Matches
ntp_client_example.cpp
Go to the documentation of this file.
1
7
8
#include <iostream>
9
#include <iomanip>
10
#include <chrono>
11
#include <ctime>
12
#include <
time_shield/config.hpp
>
13
#if TIME_SHIELD_ENABLE_NTP_CLIENT
14
# include <
time_shield/initialization.hpp
>
15
# include <
time_shield/ntp_client.hpp
>
16
17
int
main
() {
18
using namespace
time_shield
;
19
20
init
();
21
22
NtpClient
client;
// uses pool.ntp.org by default
23
24
std::cout <<
"Querying NTP server..."
<< std::endl;
25
if
(!client.
query
()) {
26
std::cerr <<
"Failed to query NTP server. Error code: "
27
<< client.
last_error_code
() << std::endl;
28
return
1;
29
}
30
31
const
int64_t offset_us = client.
offset_us
();
32
33
// Current local system time
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"
)
38
<< std::endl;
39
40
// Corrected time using the offset from the NTP server
41
auto
corrected = std::chrono::system_clock::time_point(
42
std::chrono::microseconds(client.
utc_time_us
()));
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"
)
46
<< std::endl;
47
48
std::cout <<
"Offset (us): "
<< offset_us << std::endl;
49
50
std::cout <<
"Press Enter to exit..."
<< std::endl;
51
std::cin.get();
52
return
0;
53
}
54
55
#else
56
int
main
() {
57
std::cout <<
"NtpClient is disabled in this build."
<< std::endl;
58
return
0;
59
}
60
#endif
61
time_shield::NtpClient
NTP client for measuring time offset.
Definition
ntp_client.hpp:43
time_shield::NtpClient::offset_us
int64_t offset_us() const noexcept
Returns the last measured offset in microseconds.
Definition
ntp_client.hpp:113
time_shield::NtpClient::last_error_code
int last_error_code() const noexcept
Returns last socket error code (if any).
Definition
ntp_client.hpp:137
time_shield::NtpClient::utc_time_us
int64_t utc_time_us() const noexcept
Returns current UTC time in microseconds based on last NTP offset.
Definition
ntp_client.hpp:125
time_shield::NtpClient::query
bool query()
Queries the NTP server and updates the local offset.
Definition
ntp_client.hpp:61
config.hpp
Configuration macros for the library.
time_shield::init
void init()
Initializes the Time Shield library.
Definition
initialization.hpp:23
time_shield::now
ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
Definition
time_utils.hpp:258
initialization.hpp
Initialization helpers for the Time Shield library.
time_shield
Main namespace for the Time Shield library.
ntp_client.hpp
Simple NTP client for querying time offset from NTP servers.
main
int main()
Definition
ntp_client_example.cpp:17
examples
ntp_client_example.cpp
Generated by
1.17.0