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

  1. use chrono::{DateTime, Local};
  2. use crate::error::ShellError;
  3. use super::{Builtin, BuiltinConfig};
  4. pub struct Time;
  5. impl Builtin for Time {
  6. fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
  7. let time = Local::now();
  8. let formatting = args.get(0).cloned().unwrap_or_default();
  9. let time_string = format_time(time, &formatting);
  10. println!("{time_string}");
  11. Ok(())
  12. }
  13. }
  14. fn format_time(time: DateTime<Local>, formatting: &str) -> String {
  15. match formatting {
  16. "RFC2822" => time.to_rfc2822(),
  17. "RFC3339" => time.to_rfc3339(),
  18. _ => time.format("%T").to_string(),
  19. }
  20. }