now/
now.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3use chrono::prelude::DateTime;
4use chrono::Utc;
5use clock_bound_c::ClockBoundClient;
6use std::env;
7use std::time::{Duration, UNIX_EPOCH};
8
9fn main() {
10    let args: Vec<String> = env::args().collect();
11    let clock_bound_d_socket = &args[1];
12
13    let client =
14        match ClockBoundClient::new_with_path(std::path::PathBuf::from(clock_bound_d_socket)) {
15            Ok(client) => client,
16            Err(e) => {
17                println!("Could not create client: {}", e);
18                return;
19            }
20        };
21
22    let response = match client.now() {
23        Ok(response) => response,
24        Err(e) => {
25            println!("Could not complete now request: {}", e);
26            return;
27        }
28    };
29
30    let earliest_d = UNIX_EPOCH + Duration::from_nanos(response.bound.earliest);
31    let latest_d = UNIX_EPOCH + Duration::from_nanos(response.bound.latest);
32    let timestamp_d = UNIX_EPOCH + Duration::from_nanos(response.timestamp);
33    let datetime_earliest = DateTime::<Utc>::from(earliest_d);
34    let datetime_latest = DateTime::<Utc>::from(latest_d);
35    let datetime_timestamp = DateTime::<Utc>::from(timestamp_d);
36    let datetime_str_earliest = datetime_earliest.format("%Y-%m-%d %H:%M:%S.%f").to_string();
37    let datetime_str_latest = datetime_latest.format("%Y-%m-%d %H:%M:%S.%f").to_string();
38    let datetime_str_timestamp = datetime_timestamp
39        .format("%Y-%m-%d %H:%M:%S.%f")
40        .to_string();
41
42    println!(
43        "The UTC timestamp {} has the following error bounds.",
44        datetime_str_timestamp
45    );
46    println!(
47        "In nanoseconds since the Unix epoch: ({:?},{:?})",
48        response.bound.earliest, response.bound.latest
49    );
50    println!(
51        "In UTC in date/time format: ({}, {})",
52        datetime_str_earliest, datetime_str_latest
53    );
54}