pr: Switch to uucore::time formatting

This commit is contained in:
Nicolas Boichat
2025-07-31 15:45:31 +08:00
parent f877f64362
commit 149529a494
3 changed files with 23 additions and 22 deletions
Generated
-1
View File
@@ -3592,7 +3592,6 @@ dependencies = [
name = "uu_pr"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"fluent",
"itertools 0.14.0",
+1 -2
View File
@@ -19,10 +19,9 @@ path = "src/pr.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true, features = ["entries"] }
uucore = { workspace = true, features = ["entries", "time"] }
itertools = { workspace = true }
regex = { workspace = true }
chrono = { workspace = true }
thiserror = { workspace = true }
fluent = { workspace = true }
+22 -19
View File
@@ -6,7 +6,6 @@
// spell-checker:ignore (ToDO) adFfmprt, kmerge
use chrono::{DateTime, Local};
use clap::{Arg, ArgAction, ArgMatches, Command};
use itertools::Itertools;
use regex::Regex;
@@ -14,12 +13,14 @@ use std::fs::{File, metadata};
use std::io::{BufRead, BufReader, Lines, Read, Write, stdin, stdout};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
use std::time::SystemTime;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::UResult;
use uucore::format_usage;
use uucore::translate;
use uucore::time::{FormatSystemTimeFallback, format_system_time};
const TAB: char = '\t';
const LINES_PER_PAGE: usize = 66;
@@ -487,11 +488,26 @@ fn build_options(
let line_separator = "\n".to_string();
let last_modified_time = if is_merge_mode || paths[0].eq(FILE_STDIN) {
let date_time = Local::now();
date_time.format(DATE_TIME_FORMAT).to_string()
} else {
file_last_modified_time(paths.first().unwrap())
let last_modified_time = {
let mut v = Vec::new();
let time = if is_merge_mode || paths[0].eq(FILE_STDIN) {
Some(SystemTime::now())
} else {
metadata(paths.first().unwrap())
.ok()
.and_then(|i| i.modified().ok())
};
time.and_then(|time| {
format_system_time(
&mut v,
time,
DATE_TIME_FORMAT,
FormatSystemTimeFallback::Integer,
)
.ok()
})
.map(|()| String::from_utf8_lossy(&v).to_string())
.unwrap_or_default()
};
// +page option is less priority than --pages
@@ -1126,19 +1142,6 @@ fn header_content(options: &OutputOptions, page: usize) -> Vec<String> {
}
}
fn file_last_modified_time(path: &str) -> String {
metadata(path)
.map(|i| {
i.modified()
.map(|x| {
let date_time: DateTime<Local> = x.into();
date_time.format(DATE_TIME_FORMAT).to_string()
})
.unwrap_or_default()
})
.unwrap_or_default()
}
/// Returns five empty lines as trailer content if displaying trailer
/// is not disabled by using `NO_HEADER_TRAILER_OPTION`option.
fn trailer_content(options: &OutputOptions) -> Vec<String> {