i hate comments
This commit is contained in:
parent
7fe8e30c89
commit
bbeab27f2e
1 changed files with 2 additions and 15 deletions
|
@ -13,35 +13,27 @@ void queryNTPServer(const std::string& server, int utc_offset) {
|
|||
try {
|
||||
asio::io_context io_context;
|
||||
|
||||
// Resolve the server using IPv4
|
||||
asio::ip::udp::resolver resolver(io_context);
|
||||
asio::ip::udp::resolver::query query(asio::ip::udp::v4(), server, std::to_string(NTP_PORT));
|
||||
asio::ip::udp::endpoint endpoint = *resolver.resolve(query).begin();
|
||||
|
||||
// Create a socket
|
||||
asio::ip::udp::socket socket(io_context);
|
||||
socket.open(asio::ip::udp::v4());
|
||||
|
||||
// Create NTP request packet
|
||||
std::array<unsigned char, NTP_PACKET_SIZE> request{};
|
||||
request[0] = 0b11100011; // LI, Version, Mode: (3-3-3 for client mode)
|
||||
request[0] = 0b11100011;
|
||||
|
||||
// Record start time for ping calculation
|
||||
auto start_time = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Send the request
|
||||
socket.send_to(asio::buffer(request), endpoint);
|
||||
|
||||
// Receive response
|
||||
std::array<unsigned char, NTP_PACKET_SIZE> response{};
|
||||
asio::ip::udp::endpoint sender_endpoint;
|
||||
socket.receive_from(asio::buffer(response), sender_endpoint);
|
||||
|
||||
// Record end time for ping calculation
|
||||
auto end_time = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> ping = end_time - start_time;
|
||||
|
||||
// Extract the timestamp
|
||||
const unsigned long long NTP_TIMESTAMP_DELTA = 2208988800ULL;
|
||||
unsigned long long seconds = (static_cast<unsigned long long>(response[40]) << 24) |
|
||||
(static_cast<unsigned long long>(response[41]) << 16) |
|
||||
|
@ -49,22 +41,17 @@ void queryNTPServer(const std::string& server, int utc_offset) {
|
|||
(static_cast<unsigned long long>(response[43]));
|
||||
seconds -= NTP_TIMESTAMP_DELTA;
|
||||
|
||||
// Convert timestamp to human-readable format
|
||||
std::time_t raw_time = static_cast<std::time_t>(seconds);
|
||||
std::tm* utc_time = std::gmtime(&raw_time);
|
||||
|
||||
// Calculate adjusted time for the UTC offset
|
||||
std::time_t adjusted_time = raw_time + (utc_offset * 3600);
|
||||
std::tm* local_time = std::gmtime(&adjusted_time);
|
||||
|
||||
// Print details
|
||||
std::cout << "Details of " << server << "\n";
|
||||
|
||||
if (utc_offset == 0) {
|
||||
// Print UTC time if no timezone is provided
|
||||
std::cout << "Time: " << std::put_time(utc_time, "%H:%M:%S") << " UTC\n";
|
||||
} else {
|
||||
// Print adjusted time for the provided UTC offset
|
||||
std::cout << "Time (utc" << (utc_offset >= 0 ? "+" : "") << utc_offset << "): "
|
||||
<< std::put_time(local_time, "%H:%M:%S") << "\n";
|
||||
}
|
||||
|
@ -83,7 +70,7 @@ int parseUTCOffset(const std::string& timezone) {
|
|||
if (std::regex_match(timezone, match, tz_regex)) {
|
||||
try {
|
||||
int offset = std::stoi(match[1]);
|
||||
if (offset < -12 || offset > 14) { // Limit UTC offsets to reasonable values
|
||||
if (offset < -12 || offset > 14) {
|
||||
throw std::out_of_range("UTC offset out of range (-12 to +14)");
|
||||
}
|
||||
return offset;
|
||||
|
|
Loading…
Add table
Reference in a new issue