-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring ls and date #7194
Refactoring ls and date #7194
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -27,14 +27,12 @@ use std::{ | |||
use std::{collections::HashSet, io::IsTerminal}; | ||||
|
||||
use ansi_width::ansi_width; | ||||
use chrono::{DateTime, Local, TimeDelta, TimeZone, Utc}; | ||||
use chrono_tz::{OffsetName, Tz}; | ||||
use chrono::{DateTime, Local, TimeDelta}; | ||||
use clap::{ | ||||
builder::{NonEmptyStringValueParser, PossibleValue, ValueParser}, | ||||
crate_version, Arg, ArgAction, Command, | ||||
}; | ||||
use glob::{MatchOptions, Pattern}; | ||||
use iana_time_zone::get_timezone; | ||||
use lscolors::LsColors; | ||||
use term_grid::{Direction, Filling, Grid, GridOptions}; | ||||
|
||||
|
@@ -60,6 +58,7 @@ use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR}; | |||
use uucore::line_ending::LineEnding; | ||||
use uucore::quoting_style::{self, escape_name, QuotingStyle}; | ||||
use uucore::{ | ||||
custom_tz_fmt, | ||||
display::Quotable, | ||||
error::{set_exit_code, UError, UResult}, | ||||
format_usage, | ||||
|
@@ -345,31 +344,6 @@ fn is_recent(time: DateTime<Local>) -> bool { | |||
time + TimeDelta::try_seconds(31_556_952 / 2).unwrap() > Local::now() | ||||
} | ||||
|
||||
/// Get the alphabetic abbreviation of the current timezone. | ||||
/// | ||||
/// For example, "UTC" or "CET" or "PDT". | ||||
fn timezone_abbrev() -> String { | ||||
let tz = match std::env::var("TZ") { | ||||
// TODO Support other time zones... | ||||
Ok(s) if s == "UTC0" || s.is_empty() => Tz::Etc__UTC, | ||||
_ => match get_timezone() { | ||||
Ok(tz_str) => tz_str.parse().unwrap(), | ||||
Err(_) => Tz::Etc__UTC, | ||||
}, | ||||
}; | ||||
let offset = tz.offset_from_utc_date(&Utc::now().date_naive()); | ||||
offset.abbreviation().unwrap_or("UTC").to_string() | ||||
} | ||||
|
||||
/// Format the given time according to a custom format string. | ||||
fn custom_time_format(fmt: &str, time: DateTime<Local>) -> String { | ||||
// TODO Refactor the common code from `ls` and `date` for rendering dates. | ||||
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970 | ||||
// GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`. | ||||
let fmt = fmt.replace("%N", "%f").replace("%Z", &timezone_abbrev()); | ||||
time.format(&fmt).to_string() | ||||
} | ||||
|
||||
impl TimeStyle { | ||||
/// Format the given time according to this time format style. | ||||
fn format(&self, time: DateTime<Local>) -> String { | ||||
|
@@ -386,7 +360,11 @@ impl TimeStyle { | |||
//So it's not yet implemented | ||||
(Self::Locale, true) => time.format("%b %e %H:%M").to_string(), | ||||
(Self::Locale, false) => time.format("%b %e %Y").to_string(), | ||||
(Self::Format(e), _) => custom_time_format(e, time), | ||||
(Self::Format(fmt), _) => { | ||||
// this line can be replaced with the one in timzone_date.rs | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this comment is no longer needed?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right |
||||
time.format(custom_tz_fmt::custom_time_format(fmt).as_str()) | ||||
.to_string() | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,13 +18,16 @@ edition = "2021" | |||||
path = "src/lib/lib.rs" | ||||||
|
||||||
[dependencies] | ||||||
chrono = { workspace = true } | ||||||
chrono-tz = {workspace = true} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A detail that should make the
Suggested change
|
||||||
clap = { workspace = true } | ||||||
uucore_procs = { workspace = true } | ||||||
number_prefix = { workspace = true } | ||||||
dns-lookup = { workspace = true, optional = true } | ||||||
dunce = { version = "1.0.4", optional = true } | ||||||
wild = "2.2.1" | ||||||
glob = { workspace = true } | ||||||
iana-time-zone = { workspace = true } | ||||||
lazy_static = "1.4.0" | ||||||
# * optional | ||||||
itertools = { workspace = true, optional = true } | ||||||
|
@@ -114,4 +117,5 @@ utf8 = [] | |||||
utmpx = ["time", "time/macros", "libc", "dns-lookup"] | ||||||
version-cmp = [] | ||||||
wide = [] | ||||||
custom-tz-fmt = [] | ||||||
tty = [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This file is part of the uutils coreutils package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use chrono::{TimeZone, Utc}; | ||
use chrono_tz::{OffsetName, Tz}; | ||
use iana_time_zone::get_timezone; | ||
|
||
/// Get the alphabetic abbreviation of the current timezone. | ||
/// | ||
/// For example, "UTC" or "CET" or "PDT" | ||
fn timezone_abbreviation() -> String { | ||
let tz = match std::env::var("TZ") { | ||
// TODO Support other time zones... | ||
Ok(s) if s == "UTC0" || s.is_empty() => Tz::Etc__UTC, | ||
_ => match get_timezone() { | ||
Ok(tz_str) => tz_str.parse().unwrap(), | ||
Err(_) => Tz::Etc__UTC, | ||
}, | ||
}; | ||
|
||
let offset = tz.offset_from_utc_date(&Utc::now().date_naive()); | ||
offset.abbreviation().unwrap_or("UTC").to_string() | ||
} | ||
|
||
/// Adapt the given string to be accepted by the chrono library crate. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// fmt: the format of the string | ||
/// | ||
/// # Return | ||
/// | ||
/// A string that can be used as parameter of the chrono functions that use formats | ||
pub fn custom_time_format(fmt: &str) -> String { | ||
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970 | ||
// GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`. | ||
fmt.replace("%N", "%f") | ||
.replace("%Z", timezone_abbreviation().as_ref()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{custom_time_format, timezone_abbreviation}; | ||
|
||
#[test] | ||
fn test_custom_time_format() { | ||
assert_eq!(custom_time_format("%Y-%m-%d %H-%M-%S"), "%Y-%m-%d %H-%M-%S"); | ||
assert_eq!(custom_time_format("%d-%m-%Y %H-%M-%S"), "%d-%m-%Y %H-%M-%S"); | ||
assert_eq!(custom_time_format("%Y-%m-%d %H-%M-%S"), "%Y-%m-%d %H-%M-%S"); | ||
assert_eq!( | ||
custom_time_format("%Y-%m-%d %H-%M-%S.%N"), | ||
"%Y-%m-%d %H-%M-%S.%f" | ||
); | ||
assert_eq!(custom_time_format("%Z"), timezone_abbreviation()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy complains about this line: