use std::time::{Instant, Duration};
use chrono::{DateTime, Utc};
use std::thread;
//use std::env;
static THRESHOLD_NS: u32 = 1_500_000;
static SLEEP_TIME_NS: u32 = 1_000_000;

fn main() {
    println!("Starting to monitor real time");
    let time_out = Duration::new(0, THRESHOLD_NS);
    let sleep_duration = Duration::new(0,  SLEEP_TIME_NS);
    let mut last = Instant::now();
    loop {
        thread::sleep(sleep_duration);
        let now = Instant::now();
        match now.checked_duration_since(last) {
            Some(d) => {
                if d > time_out {
                    let datetime: DateTime<Utc> = Utc::now();
                    println!("{} Time deviation detect! Diff={:?}", datetime , d - sleep_duration)
                }
            },
            None => println!("Went back in time!"),
        }

        last = now;
    }
}
