diff --git a/crates/nu-command/src/conversions/into/datetime.rs b/crates/nu-command/src/conversions/into/datetime.rs index f39084c6e5..3de9697442 100644 --- a/crates/nu-command/src/conversions/into/datetime.rs +++ b/crates/nu-command/src/conversions/into/datetime.rs @@ -1,3 +1,4 @@ +use crate::{generate_strftime_list, parse_date_from_string}; use chrono::{DateTime, FixedOffset, Local, TimeZone, Utc}; use nu_engine::CallExt; use nu_protocol::ast::Call; @@ -7,9 +8,6 @@ use nu_protocol::{ Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value, }; -use crate::generate_strftime_list; -use crate::parse_date_from_string; - struct Arguments { timezone: Option>, offset: Option>, @@ -148,6 +146,15 @@ impl Command for SubCommand { example: "1614434140 | into datetime -o +9", result: None, }, + Example { + description: + "Convert timestamps like the sqlite history t", + example: "1656165681720 | into datetime", + result: Some(Value::Date { + val: Utc.timestamp_millis(1656165681720).into(), + span: Span::test_data(), + }), + }, ] } } @@ -251,10 +258,19 @@ fn action( return match timezone { // default to UTC - None => Value::Date { - val: Utc.timestamp(ts, 0).into(), - span: head, - }, + None => { + // be able to convert chrono::Utc::now() + let dt = match ts.to_string().len() { + x if x > 13 => Utc.timestamp_nanos(ts).into(), + x if x > 10 => Utc.timestamp_millis(ts).into(), + _ => Utc.timestamp(ts, 0).into(), + }; + + Value::Date { + val: dt, + span: head, + } + } Some(Spanned { item, span }) => match item { Zone::Utc => Value::Date { val: Utc.timestamp(ts, 0).into(),