-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,067 additions
and
264 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "mymy" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2021" | ||
authors = ["Théo Crevon <[email protected]>"] | ||
description = "Access the most common information about your system using a single command" | ||
|
@@ -17,10 +17,35 @@ path = "src/main.rs" | |
|
||
[dependencies] | ||
anyhow = "1.0.70" | ||
chrono = { default-features = false, version = "0.4.24" } | ||
clap = { version = "4.2.1", features = ["std", "derive"], default-features = false } | ||
get_if_addrs = "0.5.3" | ||
hostname = "0.3.1" | ||
local-ip-address = "0.5.1" | ||
rsntp = "3.0.2" | ||
serde = { version = "1.0.159", features = ["derive"], default-features = false } | ||
serde_json = "1.0.95" | ||
tokio = { version = "1.27.0", default-features = false, features = ["macros"] } | ||
trust-dns-resolver = { version = "0.22.0", features = ["tokio-runtime", "system-config"], default-features = false } | ||
|
||
[dependencies.chrono] | ||
version = "0.4.24" | ||
default-features = false | ||
|
||
[dependencies.clap] | ||
version = "4.2.1" | ||
features = ["derive"] | ||
|
||
[dependencies.serde] | ||
version = "1.0.159" | ||
features = ["serde_derive"] | ||
default-features = false | ||
|
||
[dependencies.tokio] | ||
version = "1.27.0" | ||
default-features = false | ||
features = ["macros"] | ||
|
||
[dependencies.trust-dns-resolver] | ||
version = "0.22.0" | ||
features = ["tokio-runtime", "system-config"] | ||
default-features = false | ||
|
||
[dependencies.whoami] | ||
version = "1.4.0" | ||
default-features = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use std::fmt::{Display, Formatter}; | ||
|
||
use anyhow::Result; | ||
use chrono::{DateTime, Local}; | ||
use rsntp::AsyncSntpClient; | ||
use serde::Serialize; | ||
|
||
/// Returns the system date. | ||
pub async fn date() -> Result<Date> { | ||
let dt = Local::now(); | ||
let now_with_tz = dt.with_timezone(&Local); | ||
|
||
Ok(now_with_tz.into()) | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct Date { | ||
day_name: String, | ||
day_number: u8, | ||
month_name: String, | ||
year: i32, | ||
week_number: u8, | ||
} | ||
|
||
impl Display for Date { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.day_name)?; | ||
write!(f, ", {} {}", self.day_number, self.month_name)?; | ||
write!(f, ", {}", self.year)?; | ||
write!(f, ", week {}", self.week_number) | ||
} | ||
} | ||
|
||
impl From<DateTime<Local>> for Date { | ||
fn from(dt: DateTime<Local>) -> Self { | ||
Date { | ||
day_name: dt.format("%A").to_string(), | ||
day_number: dt.format("%d").to_string().parse::<u8>().unwrap(), | ||
month_name: dt.format("%B").to_string(), | ||
year: dt.format("%Y").to_string().parse::<i32>().unwrap(), | ||
week_number: dt.format("%U").to_string().parse::<u8>().unwrap(), | ||
} | ||
} | ||
} | ||
|
||
/// Returns the system time. | ||
pub async fn time() -> Result<Time> { | ||
let sntp_client = AsyncSntpClient::new(); | ||
let sntp_time = sntp_client.synchronize("pool.ntp.org").await?; | ||
let now = sntp_time.datetime().into_chrono_datetime()?; | ||
let now_with_tz = now.with_timezone(&Local); | ||
|
||
let mut t = Time::from(now_with_tz); | ||
t.offset = sntp_time.clock_offset().as_secs_f64(); | ||
|
||
Ok(t) | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct Time { | ||
hour: u8, | ||
minute: u8, | ||
second: u8, | ||
timezone: String, | ||
offset: f64, | ||
} | ||
|
||
impl Display for Time { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.hour)?; | ||
write!(f, ":{}", self.minute)?; | ||
write!(f, ":{}", self.second)?; | ||
write!(f, " UTC {}", self.timezone)?; | ||
write!(f, "\n±{:.4} seconds", self.offset) | ||
} | ||
} | ||
|
||
impl From<DateTime<Local>> for Time { | ||
fn from(dt: DateTime<Local>) -> Self { | ||
Time { | ||
hour: dt.format("%H").to_string().parse::<u8>().unwrap(), | ||
minute: dt.format("%M").to_string().parse::<u8>().unwrap(), | ||
second: dt.format("%S").to_string().parse::<u8>().unwrap(), | ||
timezone: dt.format("%Z").to_string(), | ||
offset: 0.0, | ||
} | ||
} | ||
} | ||
|
||
/// Returns the system date and time. | ||
pub async fn datetime() -> Result<Datetime> { | ||
let date = date().await?; | ||
let time = time().await?; | ||
|
||
Ok(Datetime { date, time }) | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct Datetime { | ||
date: Date, | ||
time: Time, | ||
} | ||
|
||
impl Display for Datetime { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.date)?; | ||
write!(f, "\n{}", self.time) | ||
} | ||
} |
Oops, something went wrong.