Просмотр исходного кода

script/research/clock_sync: simple check to verify system clock

aggstam 4 лет назад
Родитель
Сommit
4c1cf3cee6

+ 2 - 0
script/research/clock_sync/.gitignore

@@ -0,0 +1,2 @@
+/target
+Cargo.lock

+ 12 - 0
script/research/clock_sync/Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "clock_sync"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+ntp = "0.5.0"
+reqwest = "0.11.10"
+serde_json = "1.0.81"
+tokio = { version = "1.18.1", features = ["full"] }
+
+[workspace]

+ 37 - 0
script/research/clock_sync/src/main.rs

@@ -0,0 +1,37 @@
+use serde_json::Value;
+use std::{
+    error::Error,
+    thread,
+    time::{Duration, SystemTime, UNIX_EPOCH},
+};
+
+// This is a very simple check to verify that system time is correct.
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn Error>> {
+    // Poll worldtimeapi.org for current UTC timestamp
+    let response =
+        reqwest::get("https://worldtimeapi.org/api/timezone/Etc/UTC").await?.text().await?;
+    let worldtimeapi: Value = serde_json::from_str(&response).unwrap();
+    println!("worldtimeapi: {}", worldtimeapi["unixtime"].to_string());
+
+    // Poll ntp.org for current timestamp
+    let address = "0.pool.ntp.org:123";
+    let response: ntp::packet::Packet = ntp::request(address).unwrap();
+    // Remove 1900 epoch(2208988800) to reach UTC timestamp
+    let ntp_time = response.transmit_time.sec as u64 - 2208988800;
+    println!("ntp_time: {}", ntp_time);
+
+    // To simulate wrong clock, we sleep some time
+    //let one_sec = Duration::new(1, 0);
+    //thread::sleep(one_sec);
+
+    // Current system time
+    let system_time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
+    println!("SystemTime: {}", system_time);
+
+    // We verify that system time is equal to worldtimeapi or ntp
+    let check = (system_time == worldtimeapi) || (system_time == ntp_time);
+    assert!(check, "System clock is not correct!");
+
+    Ok(())
+}