Sample code for 30+ languages & platforms
Rust

Query NTP Server for Current Date/Time

Demonstrates how to query an NTP server to get the current date/time.

Note: This feature was added in Chilkat v9.5.0.96.

Chilkat Rust Downloads

Rust

// Specify the NTP server domain.
// Information is passed in a JSON object to allow for any unanticipated future optional information that might need to be provided.
let json = chilkat::JsonObject::new();
let _ = json.update_string("ntp_server", "pool.ntp.org");

// Query the NTP server for the current date/time.
let dt = chilkat::DateTime::new();
if dt.set_from_ntp_server(&json.emit().unwrap_or_default()).is_err() {
    println!("{}", dt.last_error_text());
    return;
}

// Show the date/time in various formats..
let b_local_time = true;
println!("{}", dt.get_as_timestamp(b_local_time).unwrap_or_default());
println!("{}", dt.get_as_rfc822(b_local_time).unwrap_or_default());
println!("{}", dt.get_as_unix_time(b_local_time));

// Sample output:

// 2023-10-31T09:43:27-05:00
// Tue, 31 Oct 2023 09:43:27 -0500
// 1698745407