mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 23:23:12 +01:00
use chrono_humanize for datetime formatting (#3834)
* use chrono_humanize for datetime formatting * fix tests
This commit is contained in:
parent
fcd624a722
commit
9120a64cfb
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -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",
|
||||
|
@ -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 {
|
||||
|
@ -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"] }
|
||||
|
@ -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<FixedOffset>) -> String {
|
||||
let utc: DateTime<Utc> = 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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user