1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #3918 from niyaznigmatullin/pr_chrono_to_time

pr: migrate from `chrono` to `time`
This commit is contained in:
Sylvestre Ledru 2022-09-10 09:54:37 +02:00 committed by GitHub
commit 70714bc3cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 23 deletions

2
Cargo.lock generated
View file

@ -2620,11 +2620,11 @@ dependencies = [
name = "uu_pr"
version = "0.0.15"
dependencies = [
"chrono",
"clap",
"itertools",
"quick-error",
"regex",
"time 0.3.14",
"uucore",
]

View file

@ -16,8 +16,8 @@ path = "src/pr.rs"
[dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
time = { version = "0.3", features = ["local-offset", "macros", "formatting"] }
uucore = { version=">=0.0.15", package="uucore", path="../../uucore", features=["entries"] }
chrono = { version="^0.4.19", default-features=false, features=["std", "alloc", "clock"]}
quick-error = "2.0.1"
itertools = "0.10.0"
regex = "1.6"

View file

@ -9,8 +9,6 @@
#[macro_use]
extern crate quick_error;
use chrono::offset::Local;
use chrono::DateTime;
use clap::{AppSettings, Arg, ArgMatches, Command};
use itertools::Itertools;
use quick_error::ResultExt;
@ -20,6 +18,8 @@ use std::fs::{metadata, File};
use std::io::{stdin, stdout, BufRead, BufReader, Lines, Read, Write};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
use time::macros::format_description;
use time::OffsetDateTime;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UResult};
@ -62,6 +62,8 @@ const DEFAULT_COLUMN_WIDTH: usize = 72;
const DEFAULT_COLUMN_WIDTH_WITH_S_OPTION: usize = 512;
const DEFAULT_COLUMN_SEPARATOR: &char = &TAB;
const FF: u8 = 0x0C_u8;
const DATE_TIME_FORMAT: &[time::format_description::FormatItem] =
format_description!("[month repr:short] [day] [hour]:[minute] [year]");
mod options {
pub const HEADER: &str = "header";
@ -575,8 +577,10 @@ 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("%b %d %H:%M %Y").to_string()
// let date_time = Local::now();
// date_time.format("%b %d %H:%M %Y").to_string()
let date_time = OffsetDateTime::now_local().unwrap();
date_time.format(&DATE_TIME_FORMAT).unwrap()
} else {
file_last_modified_time(paths.first().unwrap())
};
@ -1218,8 +1222,12 @@ fn file_last_modified_time(path: &str) -> String {
.map(|i| {
i.modified()
.map(|x| {
let date_time: DateTime<Local> = x.into();
date_time.format("%b %d %H:%M %Y").to_string()
let date_time: OffsetDateTime = x.into();
let offset = OffsetDateTime::now_local().unwrap().offset();
date_time
.to_offset(offset)
.format(&DATE_TIME_FORMAT)
.unwrap()
})
.unwrap_or_default()
})

View file

@ -1,10 +1,14 @@
// spell-checker:ignore (ToDO) Sdivide
extern crate time;
use crate::common::util::*;
use chrono::offset::Local;
use chrono::DateTime;
use chrono::Duration;
use std::fs::metadata;
use time::macros::format_description;
use time::Duration;
use time::OffsetDateTime;
const DATE_TIME_FORMAT: &[time::format_description::FormatItem] =
format_description!("[month repr:short] [day] [hour]:[minute] [year]");
fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
let tmp_dir_path = ucmd.get_full_fixture_path(path);
@ -13,28 +17,32 @@ fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
.map(|i| {
i.modified()
.map(|x| {
let date_time: DateTime<Local> = x.into();
date_time.format("%b %d %H:%M %Y").to_string()
let date_time: OffsetDateTime = x.into();
let offset = OffsetDateTime::now_local().unwrap().offset();
date_time
.to_offset(offset)
.format(&DATE_TIME_FORMAT)
.unwrap()
})
.unwrap_or_default()
})
.unwrap_or_default()
}
fn all_minutes(from: DateTime<Local>, to: DateTime<Local>) -> Vec<String> {
fn all_minutes(from: OffsetDateTime, to: OffsetDateTime) -> Vec<String> {
let to = to + Duration::minutes(1);
const FORMAT: &str = "%b %d %H:%M %Y";
// const FORMAT: &str = "%b %d %H:%M %Y";
let mut vec = vec![];
let mut current = from;
while current < to {
vec.push(current.format(FORMAT).to_string());
vec.push(current.format(&DATE_TIME_FORMAT).unwrap());
current += Duration::minutes(1);
}
vec
}
fn valid_last_modified_template_vars(from: DateTime<Local>) -> Vec<Vec<(String, String)>> {
all_minutes(from, Local::now())
fn valid_last_modified_template_vars(from: OffsetDateTime) -> Vec<Vec<(String, String)>> {
all_minutes(from, OffsetDateTime::now_local().unwrap())
.into_iter()
.map(|time| vec![("{last_modified_time}".to_string(), time)])
.collect()
@ -250,7 +258,7 @@ fn test_with_suppress_error_option() {
fn test_with_stdin() {
let expected_file_path = "stdin.log.expected";
let mut scenario = new_ucmd!();
let start = Local::now();
let start = OffsetDateTime::now_local().unwrap();
scenario
.pipe_in_fixture("stdin.log")
.args(&["--pages=1:2", "-n", "-"])
@ -313,7 +321,7 @@ fn test_with_mpr() {
let expected_test_file_path = "mpr.log.expected";
let expected_test_file_path1 = "mpr1.log.expected";
let expected_test_file_path2 = "mpr2.log.expected";
let start = Local::now();
let start = OffsetDateTime::now_local().unwrap();
new_ucmd!()
.args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1])
.succeeds()
@ -322,7 +330,7 @@ fn test_with_mpr() {
&valid_last_modified_template_vars(start),
);
let start = Local::now();
let start = OffsetDateTime::now_local().unwrap();
new_ucmd!()
.args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1])
.succeeds()
@ -331,7 +339,7 @@ fn test_with_mpr() {
&valid_last_modified_template_vars(start),
);
let start = Local::now();
let start = OffsetDateTime::now_local().unwrap();
new_ucmd!()
.args(&[
"--pages=1:2",
@ -439,7 +447,7 @@ fn test_with_join_lines_option() {
let test_file_2 = "test.log";
let expected_file_path = "joined.log.expected";
let mut scenario = new_ucmd!();
let start = Local::now();
let start = OffsetDateTime::now_local().unwrap();
scenario
.args(&["+1:2", "-J", "-m", test_file_1, test_file_2])
.run()