hotfix: old years are correctly displayed

This commit is contained in:
Loïc Riegel 2025-04-11 16:48:35 +02:00
parent d75aa7ed1b
commit 74b1bfff30
2 changed files with 23 additions and 4 deletions

View File

@ -14,6 +14,16 @@ fn into_datetime_from_record() {
assert_eq!(expected.out, actual.out);
}
#[test]
fn into_datetime_from_record_very_old() {
let actual = nu!(r#"{year: -100, timezone: '+02:00'} | into datetime | into record"#);
let expected = nu!(
r#"{year: -100, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: '+02:00'}"#
);
assert_eq!(expected.out, actual.out);
}
#[test]
fn into_datetime_from_record_defaults() {
let actual = nu!(r#"{year: 2025, timezone: '+02:00'} | into datetime | into record"#);

View File

@ -4020,10 +4020,19 @@ fn operator_type_error(
fn human_time_from_now(val: &DateTime<FixedOffset>) -> HumanTime {
let now = Local::now().with_timezone(val.offset());
let delta = *val - now;
let delta_seconds = delta.num_nanoseconds().unwrap_or(0) as f64 / 1_000_000_000.0;
let delta_seconds_rounded = delta_seconds.round() as i64;
HumanTime::from(Duration::seconds(delta_seconds_rounded))
match delta.num_nanoseconds() {
Some(num_nanoseconds) => {
let delta_seconds = num_nanoseconds as f64 / 1_000_000_000.0;
let delta_seconds_rounded = delta_seconds.round() as i64;
HumanTime::from(Duration::seconds(delta_seconds_rounded))
}
None => {
// Happens if the total number of nanoseconds exceeds what fits in an i64
// Note: not using delta.num_days() because it results is wrong for years before ~936: a extra year is added
let delta_years = val.year() - now.year();
HumanTime::from(Duration::days(delta_years as i64 * 365))
}
}
}
#[cfg(test)]