-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
41 lines (27 loc) · 983 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "ntp_client.hpp"
#include <iostream>
#include <iomanip>
#include "time.h"
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " \"Address of NTP server pool\" \"port\" \n";
std::cout << "Example: \n\t";
std::cout << argv[0] << " 0.pool.ntp.org 123\n";
return EXIT_FAILURE;
}
uint16_t port = std::stoi(argv[2]);
NTPClient client{argv[1], port};
auto epoch_server_ms = client.request_time();
if (0 == epoch_server_ms)
return EXIT_FAILURE;
// The function ctime receives the timestamps in seconds.
time_t epoch_server = (uint32_t)(epoch_server_ms / 1000);
std::cout << "Server time: " << ctime(&epoch_server);
std::cout << "Timestamp server: " << (uint32_t)epoch_server << "\n\n";
time_t local_time;
local_time = time(0);
std::cout << "System time is " << (epoch_server - local_time) << " seconds off\n";
return EXIT_SUCCESS;
}