Skip to content

Commit 64e4160

Browse files
author
wangjiahan
committed
feature: support time command to convert unix to string.
1 parent a4d20c4 commit 64e4160

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

Cargo.lock

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ description = "a useful command line interface"
1111
anyhow = "1.0.82"
1212
base64 = "0.22.0"
1313
blake3 = "1.5.1"
14+
chrono = "0.4.38"
1415
clap = { version = "4.5.4", features = ["derive"] }
1516
colored = "2.1.0"
1617
csv = "1.3.0"

src/cli/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
pub mod base64;
22
pub mod csv;
33
pub mod gen_pass;
4+
pub mod time;
45

56
use clap::{Parser, Subcommand};
67

78
pub use self::base64::Base64SubCommand;
89
pub use self::csv::CsvOpts;
910
pub use self::gen_pass::GenPassOpts;
11+
use self::time::TimeOpts;
1012

1113
#[derive(Debug, Parser)]
1214
#[command(version, about, long_about = None)]
@@ -24,6 +26,8 @@ pub enum SubCommand {
2426
GenPass(GenPassOpts),
2527
#[command(subcommand, about = "Base64 encode & decode")]
2628
Base64(Base64SubCommand),
29+
#[command(about = "Time utils")]
30+
Time(TimeOpts),
2731
}
2832

2933
fn verify_file(filename: &str) -> Result<String, &'static str> {

src/cli/time.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use clap::Parser;
2+
3+
#[derive(Debug, Parser)]
4+
pub struct TimeOpts {
5+
/// Specify the unix to format, if 0, means now.
6+
#[arg(default_value_t = 0)]
7+
pub unix: u64,
8+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rcli::{
55
process::{
66
self,
77
b64::{process_decode, process_encode},
8+
time::process_unix_to_string,
89
},
910
};
1011
use zxcvbn::zxcvbn;
@@ -43,6 +44,7 @@ fn main() -> anyhow::Result<()> {
4344
process_encode(&opts.input, opts.format, opts.no_padding)?
4445
}
4546
},
47+
cli::SubCommand::Time(opts) => process_unix_to_string(opts.unix as i64)?,
4648
}
4749
Ok(())
4850
}

src/process/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod b64;
22
pub mod csv_convert;
33
pub mod gen_pass;
4+
pub mod time;

src/process/time.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use anyhow::{anyhow, Ok};
2+
use chrono::{prelude::Local, TimeZone};
3+
use colored::*;
4+
5+
pub fn process_unix_to_string(unix: i64) -> anyhow::Result<()> {
6+
if unix == 0 {
7+
let now = Local::now();
8+
println!(
9+
"{}: {}",
10+
now.timestamp().to_string().yellow(),
11+
now.format("%Y-%m-%d %H:%M:%S %Z").to_string().purple()
12+
);
13+
return Ok(());
14+
}
15+
16+
let t = Local.timestamp_opt(unix, 0).single();
17+
if t.is_none() {
18+
return Err(anyhow!(format!("invalid unix: {}", unix)));
19+
}
20+
println!(
21+
"{}: {}",
22+
unix.to_string().yellow(),
23+
t.unwrap().to_string().purple()
24+
);
25+
Ok(())
26+
}

0 commit comments

Comments
 (0)