Rust
Rust
Get Current Date/Time from NIST Time Server
See more Socket/SSL/TLS Examples
Demonstrates using Chilkat Socket to connect to an NIST time server and (using the old Time Protocol (RFC 868)), will read the current GMT time.Note: This is not necessarily the very best means for getting the current date/time. The most commonly used time protocol is the Network Time Protocol (RFC-1305).
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let socket = chilkat::Socket::new();
// Connect to an NIST time server and read the current date/time
let mut max_wait_ms = 4000;
let use_tls = false;
if socket.connect("time-c.nist.gov", 37, use_tls, max_wait_ms).is_err() {
println!("{}", socket.last_error_text());
return;
}
// The time server will send a big-endian 32-bit integer representing
// the number of seconds since since 00:00 (midnight) 1 January 1900 GMT.
// The ReceiveInt32 method will receive a 4-byte integer, but returns
// true or false to indicate success. If successful, the integer
// is obtained via the ReceivedInt property.
let big_endian = true;
if socket.receive_int32(big_endian).is_err() {
println!("{}", socket.last_error_text());
return;
}
let dt = chilkat::DateTime::new();
let _ = dt.set_from_ntp_time(socket.received_int());
// Show the current local date/time
let b_local_time = true;
println!("Current local date/time: {}", dt.get_as_rfc822(b_local_time).unwrap_or_default());
max_wait_ms = 10;
let _ = socket.close(max_wait_ms);