in mini-sntp/src/lib.rs [113:153]
fn create_client_request() -> ([u8; 48], chrono::DateTime<chrono::Utc>) {
let sntp_epoch = sntp_epoch();
let mut buf = [0_u8; 48];
buf[0] = 0b00_011_011; // version_number: 3, mode: 3 (client)
let transmit_timestamp = chrono::Utc::now();
#[cfg(test)]
let transmit_timestamp = transmit_timestamp - chrono::Duration::seconds(30); // simulate unsynced local clock
let mut duration_since_sntp_epoch = transmit_timestamp - sntp_epoch;
let integral_part = duration_since_sntp_epoch.num_seconds();
duration_since_sntp_epoch =
duration_since_sntp_epoch - chrono::Duration::seconds(integral_part);
assert!(integral_part >= 0 && integral_part < i64::from(u32::max_value()));
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let integral_part = (integral_part as u32).to_be_bytes();
buf[40..44].copy_from_slice(&integral_part[..]);
let fractional_part = duration_since_sntp_epoch
.num_nanoseconds()
.expect("can't overflow nanoseconds");
let fractional_part = (fractional_part << 32) / 1_000_000_000;
assert!(fractional_part >= 0 && fractional_part < i64::from(u32::max_value()));
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let fractional_part = (fractional_part as u32).to_be_bytes();
buf[44..48].copy_from_slice(&fractional_part[..]);
let packet = Packet::parse(buf, sntp_epoch);
#[cfg(test)]
let packet = dbg!(packet);
// Re-extract transmit timestamp from the packet. This may not be the same as the original `transmit_timestamp`
// that was serialized into the packet due to rounding. Specifically, it's usually off by 1ns.
let transmit_timestamp = packet.transmit_timestamp;
(buf, transmit_timestamp)
}