fdai7374
2 years ago
7 changed files with 124 additions and 36 deletions
-
4src/builtins/fetch.rs
-
29src/builtins/help.rs
-
41src/builtins/ls.rs
-
32src/builtins/mod.rs
-
36src/builtins/time.rs
-
10src/error.rs
-
6src/execute.rs
@ -0,0 +1,36 @@ |
|||
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(),
|
|||
}
|
|||
}
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
use super::*;
|
|||
|
|||
#[test]
|
|||
fn test_time() {
|
|||
assert_eq!(Time.execute(&mut BuiltinConfig::new(), vec![]), Ok(()))
|
|||
}
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue