feat: support fmt
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
//! ```cargo
|
||||
//! [dependencies]
|
||||
//! clap = { version = "4.5.23", features = ["derive"] }
|
||||
//! simpledateformat = "0.1.4"
|
||||
//! ```
|
||||
|
||||
use clap::Parser;
|
||||
use std::time::SystemTime;
|
||||
use std::ops::Add;
|
||||
use std::process::exit;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
/// Simple program to greet a person
|
||||
#[derive(Parser, Debug)]
|
||||
@@ -21,6 +24,12 @@ struct Args {
|
||||
/// Output format (e.g. millis (default), s|secs)
|
||||
#[arg(short, long)]
|
||||
output_format: Option<String>,
|
||||
/// Local time format
|
||||
#[arg(long)]
|
||||
local_time_format: Option<String>,
|
||||
/// UTF time format
|
||||
#[arg(long)]
|
||||
utc_time_format: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -39,10 +48,32 @@ fn main() {
|
||||
_ => final_time_millis,
|
||||
};
|
||||
|
||||
let mut print_time = format!("{}", final_time);
|
||||
let final_system_time =
|
||||
SystemTime::UNIX_EPOCH.add(Duration::from_millis(final_time_millis as u64));
|
||||
|
||||
if let Some(local_time_format) = &args.local_time_format {
|
||||
print_time = match simpledateformat::fmt(local_time_format) {
|
||||
Ok(fmt) => fmt.format_local(final_system_time),
|
||||
Err(_) => {
|
||||
eprintln!("Invalid local time format.");
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
} else if let Some(utc_time_format) = &args.utc_time_format {
|
||||
print_time = match simpledateformat::fmt(utc_time_format) {
|
||||
Ok(fmt) => fmt.format_local(final_system_time),
|
||||
Err(_) => {
|
||||
eprintln!("Invalid UTC time format.");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if args.new_line {
|
||||
println!("{}", final_time);
|
||||
println!("{}", print_time);
|
||||
} else {
|
||||
print!("{}", final_time);
|
||||
print!("{}", print_time);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user