pub fn get_time<A, U, T>(
pool_addrs: A,
socket: U,
context: NtpContext<T>
) -> Result<NtpResult>
Expand description
Send request to a NTP server with the given address and process the response in a single call
May be useful under an environment with std
networking implementation, where all
network stuff is hidden within system’s kernel. For environment with custom
Uses NtpUdpSocket
and NtpTimestampGenerator
trait bounds to allow generic specification
of objects that can be used with the library
Args:
pool_addrs
- Server’s name or IP address with port specification as a stringsocket
- UDP socket object that will be used during NTP request-response communicationcontext
- SNTP client context to provide timestamp generation feature
Example
use sntpc::{self, NtpContext, NtpTimestampGenerator, Result};
use std::time::Duration;
// implement required trait on network objects
// implement required trait on the timestamp generator object
#[derive(Copy, Clone, Default)]
struct StdTimestampGen {
duration: Duration,
}
impl NtpTimestampGenerator for StdTimestampGen {
fn init(&mut self) {
self.duration = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap();
}
fn timestamp_sec(&self) -> u64 {
self.duration.as_secs()
}
fn timestamp_subsec_micros(&self) -> u32 {
self.duration.subsec_micros()
}
}
let ntp_context = NtpContext::new(StdTimestampGen::default());
let socket = UdpSocketWrapper(UdpSocket::bind("0.0.0.0:0").expect("something"));
let result = sntpc::get_time("time.google.com:123", socket, ntp_context);
// OR
// let result = sntpc::get_time("83.168.200.199:123", socket, context);
// .. process the result