before/
before.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3use chrono::Utc;
4use clock_bound_c::ClockBoundClient;
5use std::env;
6use std::thread::sleep;
7use std::time::Duration;
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    // Get the current time in nanoseconds since the Unix Epoch
23    let timestamp = Utc::now().timestamp_nanos() as u64;
24
25    // Checks if a point in time is before the current time's error bounds
26    let response = match client.before(timestamp) {
27        Ok(response) => response,
28        Err(e) => {
29            println!("Couldn't complete before request: {}", e);
30            return;
31        }
32    };
33
34    // With a before request done using the current time for comparison, it is likely that the
35    // request is processed faster than the local Clock Error Bound. This means that generally the
36    // current time will not be before the earliest bound and therefore return false.
37    if response.before == false {
38        println!(
39            "{} nanoseconds since the Unix Epoch is not before the current time's error bounds.",
40            timestamp
41        )
42    } else if response.before == true {
43        println!(
44            "{} nanoseconds since the Unix Epoch is before the current time's error bounds.",
45            timestamp
46        )
47    }
48
49    println!("Waiting 1 second...");
50
51    // Checking again after a brief period of time (a pessimistic 1 second for this example's sake)
52    // the timestamp should be before the earliest error bound and return true.
53    sleep(Duration::from_secs(1));
54
55    // Checks if a point in time is before the current time's error bounds
56    let response = match client.before(timestamp) {
57        Ok(response) => response,
58        Err(e) => {
59            println!("Couldn't complete before request: {}", e);
60            return;
61        }
62    };
63
64    if response.before == false {
65        println!(
66            "{} nanoseconds since the Unix Epoch is not before the current time's error bounds.",
67            timestamp
68        )
69    } else if response.before == true {
70        println!(
71            "{} nanoseconds since the Unix Epoch is before the current time's error bounds.",
72            timestamp
73        )
74    }
75}