Some checks failed
Build Zenyx ⚡ / 🧪 Run Cargo Tests (pull_request) Failing after 6m9s
Build Zenyx ⚡ / 🏗️ Build aarch64-apple-darwin (pull_request) Has been skipped
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (pull_request) Has been skipped
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (pull_request) Has been skipped
Build Zenyx ⚡ / 🏗️ Build x86_64-apple-darwin (pull_request) Has been skipped
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (pull_request) Has been skipped
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (pull_request) Has been skipped
lib.rs is separated into src/modules now for cleaner code optimized error handling to -> (storage.rs, uptime.rs, meta.rs, memory.rs, os.rs, network.rs) optimized struct types to -> (storage.rs, uptime.rs, meta.rs, memory.rs, os.rs, network.rs)
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
use crate::{
|
|
custom_anyhow,
|
|
modules::{FmtDateTime, FmtOffsetTime, FmtRelativeTime},
|
|
};
|
|
use anyhow::Result;
|
|
use chrono::{DateTime, Local, Utc};
|
|
use serde::Serialize;
|
|
use serde_json::{Value, json};
|
|
use std::time::Duration as StdDuration;
|
|
use sysinfo::System;
|
|
|
|
#[derive(Debug, Serialize)]
|
|
struct DateInfo {
|
|
time_offset: FmtOffsetTime,
|
|
local_date_time: FmtDateTime<Local>,
|
|
utc_date_time: FmtDateTime<Utc>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct UptimeInfo {
|
|
boot: DateInfo,
|
|
now: DateInfo,
|
|
relative: FmtRelativeTime,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn get_struct() -> Result<UptimeInfo> {
|
|
let boot_time_utc = DateTime::<Utc>::from_timestamp(System::boot_time() as i64, 0).ok_or(
|
|
custom_anyhow!("Invalid, or out of range timestamp")
|
|
.context("Invalid timestamp: check seconds < 8_220_000_000_000"),
|
|
)?;
|
|
let boot_time_local: DateTime<Local> = boot_time_utc.with_timezone(&Local);
|
|
let relative_time =
|
|
StdDuration::from_secs((Local::now() - boot_time_local).num_seconds() as u64);
|
|
|
|
Ok(UptimeInfo {
|
|
boot: DateInfo {
|
|
time_offset: FmtOffsetTime(Local::now()),
|
|
local_date_time: FmtDateTime(boot_time_local),
|
|
utc_date_time: FmtDateTime(boot_time_utc),
|
|
},
|
|
now: DateInfo {
|
|
time_offset: FmtOffsetTime(Local::now()),
|
|
local_date_time: FmtDateTime(Local::now()),
|
|
utc_date_time: FmtDateTime(Utc::now()),
|
|
},
|
|
relative: FmtRelativeTime(relative_time),
|
|
})
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn get_json() -> Result<Value> {
|
|
Ok(json!(get_struct()?))
|
|
}
|