1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

ls: attempt to fix windows sorting issues

This commit is contained in:
Terts Diepraam 2021-03-15 12:07:10 +01:00
parent a4c79c92ae
commit f28d5f4a73

View file

@ -27,6 +27,7 @@ use std::os::unix::fs::MetadataExt;
#[cfg(windows)] #[cfg(windows)]
use std::os::windows::fs::MetadataExt; use std::os::windows::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use time::{strftime, Timespec}; use time::{strftime, Timespec};
@ -532,8 +533,8 @@ fn sort_entries(entries: &mut Vec<PathBuf>, config: &Config) {
Reverse( Reverse(
get_metadata(k, config) get_metadata(k, config)
.ok() .ok()
.and_then(|md| get_time(&md, config)) .and_then(|md| get_system_time(&md, config))
.unwrap_or(0), .unwrap_or(UNIX_EPOCH),
) )
}), }),
Sort::Size => entries Sort::Size => entries
@ -756,34 +757,35 @@ fn display_group(_metadata: &Metadata, _config: &Config) -> String {
// The implementations for get_time are separated because some options, such // The implementations for get_time are separated because some options, such
// as ctime will not be available // as ctime will not be available
#[cfg(unix)] #[cfg(unix)]
fn get_time(md: &Metadata, config: &Config) -> Option<i64> { fn get_system_time(md: &Metadata, config: &Config) -> Option<SystemTime> {
Some(match config.time { match config.time {
Time::Change => md.ctime(), Time::Change => Some(UNIX_EPOCH + Duration::new(md.ctime() as u64, md.ctime_nsec() as u32)),
Time::Modification => md.mtime(), Time::Modification => md.modified().ok(),
Time::Access => md.atime(), Time::Access => md.accessed().ok(),
}) }
} }
#[cfg(not(unix))] #[cfg(not(unix))]
fn get_time(md: &Metadata, config: &Config) -> Option<i64> { fn get_system_time(md: &Metadata, config: &Config) -> Option<SystemTime> {
let time = match config.time { match config.time {
Time::Modification => md.modified().ok()?, Time::Modification => md.modified().ok(),
Time::Access => md.accessed().ok()?, Time::Access => md.accessed().ok(),
_ => return None, _ => None,
}; }
Some( }
time.duration_since(std::time::UNIX_EPOCH)
.unwrap() fn get_time(md: &Metadata, config: &Config) -> Option<time::Tm> {
.as_secs() as i64, let duration = get_system_time(md, config)?
) .duration_since(UNIX_EPOCH)
.ok()?;
let secs = duration.as_secs() as i64;
let nsec = duration.subsec_nanos() as i32;
Some(time::at(Timespec::new(secs, nsec)))
} }
fn display_date(metadata: &Metadata, config: &Config) -> String { fn display_date(metadata: &Metadata, config: &Config) -> String {
match get_time(metadata, config) { match get_time(metadata, config) {
Some(secs) => { Some(time) => strftime("%F %R", &time).unwrap(),
let time = time::at(Timespec::new(secs, 0));
strftime("%F %R", &time).unwrap()
}
None => "???".into(), None => "???".into(),
} }
} }