From 149529a494ecab26ffad4b1cba957d8b02553879 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Tue, 29 Jul 2025 02:51:04 +0800 Subject: [PATCH] pr: Switch to uucore::time formatting --- Cargo.lock | 1 - src/uu/pr/Cargo.toml | 3 +-- src/uu/pr/src/pr.rs | 41 ++++++++++++++++++++++------------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8c738ac5..b854e8ab8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3592,7 +3592,6 @@ dependencies = [ name = "uu_pr" version = "0.1.0" dependencies = [ - "chrono", "clap", "fluent", "itertools 0.14.0", diff --git a/src/uu/pr/Cargo.toml b/src/uu/pr/Cargo.toml index 3fb128133..d8b5b2791 100644 --- a/src/uu/pr/Cargo.toml +++ b/src/uu/pr/Cargo.toml @@ -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 } diff --git a/src/uu/pr/src/pr.rs b/src/uu/pr/src/pr.rs index 2ea418647..453c90423 100644 --- a/src/uu/pr/src/pr.rs +++ b/src/uu/pr/src/pr.rs @@ -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 { } } -fn file_last_modified_time(path: &str) -> String { - metadata(path) - .map(|i| { - i.modified() - .map(|x| { - let date_time: DateTime = 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 {