You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

26 lines
680 B

use chrono::{DateTime, Local};
use crate::error::ShellError;
use super::{Builtin, BuiltinConfig};
pub struct Time;
impl Builtin for Time {
fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
let time = Local::now();
let formatting = args.get(0).cloned().unwrap_or_default();
let time_string = format_time(time, &formatting);
println!("{time_string}");
Ok(())
}
}
fn format_time(time: DateTime<Local>, formatting: &str) -> String {
match formatting {
"RFC2822" => time.to_rfc2822(),
"RFC3339" => time.to_rfc3339(),
_ => time.format("%T").to_string(),
}
}