Skip to content

Commit

Permalink
manage the special case 'find %A+ (#454)
Browse files Browse the repository at this point in the history
* declare the variable where it is used

* manage the special case 'find %A+'

Closes: #451
  • Loading branch information
sylvestre authored Sep 22, 2024
1 parent 27d95aa commit 9d66be6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/find/matchers/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ enum TimeFormat {

impl TimeFormat {
fn apply(&self, time: SystemTime) -> Result<Cow<'static, str>, Box<dyn Error>> {
const CTIME_FORMAT: &str = "%a %b %d %H:%M:%S.%f0 %Y";

let formatted = match self {
TimeFormat::SinceEpoch => {
let duration = time.duration_since(SystemTime::UNIX_EPOCH)?;
format!("{}.{:09}0", duration.as_secs(), duration.subsec_nanos())
}
TimeFormat::Ctime => DateTime::<Local>::from(time)
.format(CTIME_FORMAT)
.to_string(),
TimeFormat::Ctime => {
const CTIME_FORMAT: &str = "%a %b %d %H:%M:%S.%f0 %Y";

DateTime::<Local>::from(time)
.format(CTIME_FORMAT)
.to_string()
}
TimeFormat::Strftime(format) => {
DateTime::<Local>::from(time).format(format).to_string()
// Handle a special case
let custom_format = format.replace("%+", "%Y-%m-%d+%H:%M:%S%.f0");
DateTime::<Local>::from(time)
.format(&custom_format)
.to_string()
}
};

Expand Down
23 changes: 23 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use assert_cmd::Command;
use predicates::prelude::*;
use regex::Regex;
use serial_test::serial;
use std::fs::{self, File};
use std::io::{Read, Write};
Expand Down Expand Up @@ -367,6 +368,28 @@ fn find_printf() {
./test_data/simple/subdir/ABBBC subdir/ABBBC f\n",
)));

fs::create_dir_all("a").expect("Failed to create directory 'a'");
let output = Command::cargo_bin("find")
.expect("found binary")
.args(["a", "-printf", "%A+"])
.assert()
.success()
.get_output()
.stdout
.clone();

let output_str = String::from_utf8(output).expect("Invalid UTF-8 in output");

println!("Actual output: '{}'", output_str.trim());

let re = Regex::new(r"^\d{4}-\d{2}-\d{2}\+\d{2}:\d{2}:\d{2}\.\d{9}0$")
.expect("Failed to compile regex");

assert!(
re.is_match(output_str.trim()),
"Output did not match expected timestamp format"
);

Command::cargo_bin("find")
.expect("found binary")
.args([
Expand Down

0 comments on commit 9d66be6

Please sign in to comment.