56 lines
1.9 KiB
Rust
56 lines
1.9 KiB
Rust
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeDelta, Utc};
|
|
use clap::Parser;
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct Args {
|
|
time: Option<String>,
|
|
// date: Option<String>,
|
|
// time: Option<String>,
|
|
// time: Option<NaiveTime>,
|
|
// #[arg(long)]
|
|
// zone: Ti
|
|
}
|
|
|
|
fn main() {
|
|
let args = Args::parse();
|
|
let stamp = if let Some(time) = args.time {
|
|
let time = DateTime::parse_from_rfc2822(&time).unwrap();
|
|
println!("time: {}", time);
|
|
println!("time: {}", time.to_rfc2822());
|
|
time.with_timezone(&Utc)
|
|
} else {
|
|
let now = Utc::now();
|
|
println!("Now: {}", now.with_timezone(&chrono::Local));
|
|
now
|
|
};
|
|
let unix = stamp.timestamp();
|
|
let stamp = stamp.with_timezone(&chrono::Local);
|
|
println!("<t:{}:F> {}", unix, stamp.format("%A, %B %-d, %Y at %-H:%M %p"));
|
|
println!("<t:{}:f> {}", unix, stamp.format("%B %-d, %Y at %-H:%M %p"));
|
|
println!("<t:{}:D> {}", unix, stamp.format("%B %-d, %Y"));
|
|
println!("<t:{}:d> {}", unix, stamp.format("%-m/%-d/%y"));
|
|
println!("<t:{}:T> {}", unix, stamp.format("%-H:%M:%S %A"));
|
|
println!("<t:{}:t> {}", unix, stamp.format("%-H:%M %A"));
|
|
let diff = stamp.with_timezone(&Utc) - Utc::now();
|
|
let wrap =
|
|
if diff > TimeDelta::zero() {
|
|
|num: i64, word: &str| format!("In {num} {word}")
|
|
} else {
|
|
|num: i64, word: &str| format!("{num} {word} ago")
|
|
};
|
|
let abs_diff = diff.abs();
|
|
let words = if abs_diff.num_days() > 0 {
|
|
wrap(abs_diff.num_days() + i64::from(abs_diff.num_hours() >= 12), "days")
|
|
} else if abs_diff.num_hours() > 0 {
|
|
wrap(abs_diff.num_hours(), "hours")
|
|
} else if abs_diff.num_minutes() > 0 {
|
|
wrap(abs_diff.num_minutes(), "minutes")
|
|
} else if abs_diff.num_seconds() > 0 {
|
|
wrap(abs_diff.num_seconds(), "seconds")
|
|
} else {
|
|
format!("now")
|
|
};
|
|
println!("<t:{}:R> {}", unix, words);
|
|
}
|