use chrono crate for human-readable times
This commit is contained in:
parent
3e1d36ded2
commit
b926b55846
|
@ -148,6 +148,17 @@ version = "0.1.10"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cookie"
|
||||
version = "0.11.3"
|
||||
|
@ -179,6 +190,7 @@ name = "device-battery-timer"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"rocket",
|
||||
]
|
||||
|
||||
|
@ -389,6 +401,25 @@ dependencies = [
|
|||
"log 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.13.0"
|
||||
|
|
|
@ -9,3 +9,4 @@ edition = "2018"
|
|||
[dependencies]
|
||||
rocket = "0.4.5"
|
||||
anyhow = "1.0"
|
||||
chrono = "0.4"
|
19
src/main.rs
19
src/main.rs
|
@ -6,6 +6,9 @@ extern crate rocket;
|
|||
#[macro_use]
|
||||
extern crate anyhow;
|
||||
|
||||
extern crate chrono;
|
||||
|
||||
use chrono::prelude::*;
|
||||
use rocket::{http::RawStr, State};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
|
@ -21,26 +24,24 @@ struct LoggedAccesses {
|
|||
|
||||
#[derive(Default)]
|
||||
struct DeviceLog {
|
||||
access_times: Vec<SystemTime>,
|
||||
access_times: Vec<DateTime<Local>>,
|
||||
}
|
||||
|
||||
impl LoggedAccesses {
|
||||
pub fn log_device_access(&mut self, device_name: &str) {
|
||||
let device = self.devices.entry(String::from(device_name)).or_default();
|
||||
|
||||
let now = SystemTime::now();
|
||||
let now = Local::now();
|
||||
device.access_times.push(now)
|
||||
}
|
||||
}
|
||||
|
||||
impl DeviceLog {
|
||||
pub fn get_duration(&self) -> Result<(Duration, &SystemTime, &SystemTime), anyhow::Error> {
|
||||
pub fn get_duration(&self) -> Result<(i64, &DateTime<Local>, &DateTime<Local>), anyhow::Error> {
|
||||
let start = self.access_times.first().ok_or(anyhow!("No start time"))?;
|
||||
let end = self.access_times.last().ok_or(anyhow!("No end time"))?;
|
||||
|
||||
let duration = end
|
||||
.duration_since(start.clone())
|
||||
.map_err(|_| anyhow!("Times could not be diffed"))?;
|
||||
let duration = end.timestamp() - start.timestamp();
|
||||
|
||||
Ok((duration, &start, &end))
|
||||
}
|
||||
|
@ -59,10 +60,8 @@ impl Display for DeviceLog {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.get_duration() {
|
||||
Ok((duration, start, end)) => f.write_fmt(format_args!(
|
||||
"{} seconds between first ({:?}) and last ({:?}) call",
|
||||
duration.as_secs(),
|
||||
start,
|
||||
end
|
||||
"{} seconds between first ({}) and last ({}) call",
|
||||
duration, start, end
|
||||
)),
|
||||
Err(e) => f.write_fmt(format_args!("Error getting duration: {}", e)),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue