From a007cab99a7473ec3f31ac3d82257453d23ac0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Riegel?= Date: Fri, 11 Apr 2025 18:04:24 +0200 Subject: [PATCH] bugfix: fallback to rfc3339 instead of rfc2822 for nagative years --- crates/nu-command/src/formats/to/text.rs | 13 ++++++++++- crates/nu-command/src/strings/format/date.rs | 24 +++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/formats/to/text.rs b/crates/nu-command/src/formats/to/text.rs index 858c5fce29..393de80ac3 100644 --- a/crates/nu-command/src/formats/to/text.rs +++ b/crates/nu-command/src/formats/to/text.rs @@ -1,3 +1,4 @@ +use chrono::Datelike; use chrono_humanize::HumanTime; use nu_engine::command_prelude::*; use nu_protocol::{format_duration, shell_error::io::IoError, ByteStream, PipelineMetadata}; @@ -167,7 +168,17 @@ fn local_into_string( Value::Filesize { val, .. } => val.to_string(), Value::Duration { val, .. } => format_duration(val), Value::Date { val, .. } => { - format!("{} ({})", val.to_rfc2822(), HumanTime::from(val)) + format!( + "{} ({})", + { + if val.year() >= 0 { + val.to_rfc2822() + } else { + val.to_rfc3339() + } + }, + HumanTime::from(val) + ) } Value::Range { val, .. } => val.to_string(), Value::String { val, .. } => val, diff --git a/crates/nu-command/src/strings/format/date.rs b/crates/nu-command/src/strings/format/date.rs index fa7911361c..ad668ca510 100644 --- a/crates/nu-command/src/strings/format/date.rs +++ b/crates/nu-command/src/strings/format/date.rs @@ -1,5 +1,5 @@ use crate::{generate_strftime_list, parse_date_from_string}; -use chrono::{DateTime, Locale, TimeZone}; +use chrono::{DateTime, Datelike, Locale, TimeZone}; use nu_engine::command_prelude::*; use nu_utils::locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR}; @@ -228,11 +228,29 @@ fn format_helper( fn format_helper_rfc2822(value: Value, span: Span) -> Value { let val_span = value.span(); match value { - Value::Date { val, .. } => Value::string(val.to_rfc2822(), span), + Value::Date { val, .. } => Value::string( + { + if val.year() >= 0 { + val.to_rfc2822() + } else { + val.to_rfc3339() + } + }, + span, + ), Value::String { val, .. } => { let dt = parse_date_from_string(&val, val_span); match dt { - Ok(x) => Value::string(x.to_rfc2822(), span), + Ok(x) => Value::string( + { + if x.year() >= 0 { + x.to_rfc2822() + } else { + x.to_rfc3339() + } + }, + span, + ), Err(e) => e, } }