From 9120a64cfbc8685e82e48b7bbb40f53161f9e4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sun, 25 Jul 2021 01:38:45 -0700 Subject: [PATCH] use chrono_humanize for datetime formatting (#3834) * use chrono_humanize for datetime formatting * fix tests --- Cargo.lock | 10 ++ .../nu-command/src/commands/formats/to/md.rs | 8 +- crates/nu-protocol/Cargo.toml | 1 + crates/nu-protocol/src/value/primitive.rs | 125 +----------------- 4 files changed, 18 insertions(+), 126 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d737411bd..882c6ba3bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -746,6 +746,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "chrono-humanize" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eddc119501d583fd930cb92144e605f44e0252c38dd89d9247fffa1993375cb" +dependencies = [ + "chrono", +] + [[package]] name = "chrono-tz" version = "0.5.3" @@ -3499,6 +3508,7 @@ dependencies = [ "bigdecimal", "byte-unit", "chrono", + "chrono-humanize", "derive-new", "getset", "indexmap", diff --git a/crates/nu-command/src/commands/formats/to/md.rs b/crates/nu-command/src/commands/formats/to/md.rs index ad302ecd57..49444b76ad 100644 --- a/crates/nu-command/src/commands/formats/to/md.rs +++ b/crates/nu-command/src/commands/formats/to/md.rs @@ -51,8 +51,8 @@ impl WholeStreamCommand for Command { |-|-|-|-| |Andres.txt|File|10|2 years ago| |Jonathan|Dir|5|2 years ago| - |Darren.txt|File|20|1 year ago| - |Yehuda|Dir|4|1 year ago| + |Darren.txt|File|20|2 years ago| + |Yehuda|Dir|4|2 years ago| "#))]), }, Example { @@ -63,8 +63,8 @@ impl WholeStreamCommand for Command { | ---------- | ---- | -------- | ----------- | | Andres.txt | File | 10 | 2 years ago | | Jonathan | Dir | 5 | 2 years ago | - | Darren.txt | File | 20 | 1 year ago | - | Yehuda | Dir | 4 | 1 year ago | + | Darren.txt | File | 20 | 2 years ago | + | Yehuda | Dir | 4 | 2 years ago | "#))]), }, Example { diff --git a/crates/nu-protocol/Cargo.toml b/crates/nu-protocol/Cargo.toml index c75dc31525..7fe28f64c0 100644 --- a/crates/nu-protocol/Cargo.toml +++ b/crates/nu-protocol/Cargo.toml @@ -13,6 +13,7 @@ doctest = false bigdecimal = { version="0.2.0", features=["serde"] } byte-unit = "4.0.9" chrono = { version="0.4.19", features=["serde"] } +chrono-humanize = "0.2.1" derive-new = "0.5.8" getset = "0.1.1" indexmap = { version="1.6.1", features=["serde-1"] } diff --git a/crates/nu-protocol/src/value/primitive.rs b/crates/nu-protocol/src/value/primitive.rs index f325cffe4f..4186e02240 100644 --- a/crates/nu-protocol/src/value/primitive.rs +++ b/crates/nu-protocol/src/value/primitive.rs @@ -3,7 +3,8 @@ use crate::value::column_path::ColumnPath; use crate::value::range::{Range, RangeInclusion}; use crate::value::{serde_bigdecimal, serde_bigint}; use bigdecimal::BigDecimal; -use chrono::{DateTime, FixedOffset, Utc}; +use chrono::{DateTime, FixedOffset}; +use chrono_humanize::HumanTime; use nu_errors::{ExpectedRange, ShellError}; use nu_source::{PrettyDebug, Span, SpannedItem}; use num_bigint::BigInt; @@ -592,127 +593,7 @@ pub fn format_duration(duration: &BigInt) -> String { ) } -#[allow(clippy::cognitive_complexity)] /// Format a date value into a humanized string (eg "1 week ago" instead of a formal date string) pub fn format_date(d: &DateTime) -> String { - let utc: DateTime = Utc::now(); - - let duration = utc.signed_duration_since(*d); - - if duration.num_seconds() < 0 { - // Our duration is negative, so we need to speak about the future - if -duration.num_weeks() >= 52 { - let num_years = -duration.num_weeks() / 52; - - format!( - "{} year{} from now", - num_years, - if num_years == 1 { "" } else { "s" } - ) - } else if -duration.num_weeks() >= 4 { - let num_months = -duration.num_weeks() / 4; - - format!( - "{} month{} from now", - num_months, - if num_months == 1 { "" } else { "s" } - ) - } else if -duration.num_weeks() >= 1 { - let num_weeks = -duration.num_weeks(); - - format!( - "{} week{} from now", - num_weeks, - if num_weeks == 1 { "" } else { "s" } - ) - } else if -duration.num_days() >= 1 { - let num_days = -duration.num_days(); - - format!( - "{} day{} from now", - num_days, - if num_days == 1 { "" } else { "s" } - ) - } else if -duration.num_hours() >= 1 { - let num_hours = -duration.num_hours(); - - format!( - "{} hour{} from now", - num_hours, - if num_hours == 1 { "" } else { "s" } - ) - } else if -duration.num_minutes() >= 1 { - let num_minutes = -duration.num_minutes(); - - format!( - "{} min{} from now", - num_minutes, - if num_minutes == 1 { "" } else { "s" } - ) - } else { - let num_seconds = -duration.num_seconds(); - - format!( - "{} sec{} from now", - num_seconds, - if num_seconds == 1 { "" } else { "s" } - ) - } - } else if duration.num_weeks() >= 52 { - let num_years = duration.num_weeks() / 52; - - format!( - "{} year{} ago", - num_years, - if num_years == 1 { "" } else { "s" } - ) - } else if duration.num_weeks() >= 4 { - let num_months = duration.num_weeks() / 4; - - format!( - "{} month{} ago", - num_months, - if num_months == 1 { "" } else { "s" } - ) - } else if duration.num_weeks() >= 1 { - let num_weeks = duration.num_weeks(); - - format!( - "{} week{} ago", - num_weeks, - if num_weeks == 1 { "" } else { "s" } - ) - } else if duration.num_days() >= 1 { - let num_days = duration.num_days(); - - format!( - "{} day{} ago", - num_days, - if num_days == 1 { "" } else { "s" } - ) - } else if duration.num_hours() >= 1 { - let num_hours = duration.num_hours(); - - format!( - "{} hour{} ago", - num_hours, - if num_hours == 1 { "" } else { "s" } - ) - } else if duration.num_minutes() >= 1 { - let num_minutes = duration.num_minutes(); - - format!( - "{} min{} ago", - num_minutes, - if num_minutes == 1 { "" } else { "s" } - ) - } else { - let num_seconds = duration.num_seconds(); - - format!( - "{} sec{} ago", - num_seconds, - if num_seconds == 1 { "" } else { "s" } - ) - } + format!("{}", HumanTime::from(*d)) }