Skip to content
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

Move locale based date behind a flag #840

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ pub struct Cli {
fn validate_date_argument(arg: &str) -> Result<String, String> {
if arg.starts_with('+') {
validate_time_format(arg)
} else if arg == "date" || arg == "relative" {
} else if arg == "date" || arg == "relative" || arg == "locale" {
Result::Ok(arg.to_owned())
} else {
Result::Err("possible values: date, relative, +date-time-format".to_owned())
Result::Err("possible values: date, locale, relative, +date-time-format".to_owned())
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/flags/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::print_error;
pub enum DateFlag {
#[default]
Date,
Locale,
Relative,
Iso,
Formatted(String),
Expand All @@ -33,6 +34,7 @@ impl DateFlag {
let value = value.as_ref();
match value {
"date" => Some(Self::Date),
"locale" => Some(Self::Locale),
"relative" => Some(Self::Relative),
_ if value.starts_with('+') => Self::from_format_string(value),
_ => {
Expand Down Expand Up @@ -77,6 +79,7 @@ impl Configurable<Self> for DateFlag {
match value.as_str() {
"full-iso" => Some(Self::Formatted("%F %T.%f %z".into())),
"long-iso" => Some(Self::Formatted("%F %R".into())),
"locale" => Some(Self::Locale),
"iso" => Some(Self::Iso),
_ if value.starts_with('+') => Self::from_format_string(&value),
_ => {
Expand Down Expand Up @@ -114,6 +117,13 @@ mod test {
assert_eq!(Some(DateFlag::Date), DateFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_locale() {
let argv = ["lsd", "--date", "locale"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(DateFlag::Locale), DateFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_relative() {
let argv = ["lsd", "--date", "relative"];
Expand Down
39 changes: 35 additions & 4 deletions src/meta/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl Date {

if let Date::Date(val) = self {
match &flags.date {
DateFlag::Date => val.format_localized("%c", locale).to_string(),
DateFlag::Date => val.format("%c").to_string(),
DateFlag::Locale => val.format_localized("%c", locale).to_string(),
DateFlag::Relative => HumanTime::from(*val - Local::now()).to_string(),
DateFlag::Iso => {
// 365.2425 * 24 * 60 * 60 = 31556952 seconds per year
Expand Down Expand Up @@ -131,7 +132,7 @@ mod test {

assert_eq!(
creation_date
.format_localized("%c", current_locale())
.format("%c")
.to_string()
.with(Color::AnsiValue(40)),
date.render(&colors, &flags)
Expand All @@ -158,7 +159,7 @@ mod test {

assert_eq!(
creation_date
.format_localized("%c", current_locale())
.format("%c")
.to_string()
.with(Color::AnsiValue(42)),
date.render(&colors, &flags)
Expand All @@ -185,7 +186,7 @@ mod test {

assert_eq!(
creation_date
.format_localized("%c", current_locale())
.format("%c")
.to_string()
.with(Color::AnsiValue(36)),
date.render(&colors, &flags)
Expand Down Expand Up @@ -309,6 +310,36 @@ mod test {
fs::remove_file(file_path).unwrap();
}

#[test]
fn test_locale_format_now() {
let mut file_path = env::temp_dir();
file_path.push("test_locale_format_now.tmp");

let creation_date = Local::now();
let success = cross_platform_touch(&file_path, &creation_date)
.unwrap()
.success();
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());

let flags = Flags {
date: DateFlag::Locale,
..Default::default()
};

assert_eq!(
creation_date
.format_localized("%c", current_locale())
.to_string()
.with(Color::AnsiValue(40)),
date.render(&colors, &flags)
);

fs::remove_file(file_path).unwrap();
}

#[test]
#[cfg(all(not(windows), target_arch = "x86_64"))]
fn test_bad_date() {
Expand Down