From ef9b72d36008e33e327ff463fb77b22fd39e4345 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Sat, 25 Jun 2022 09:51:41 -0500 Subject: [PATCH] add ability to convert timestamp_millis() (#5876) * add ability to convert timestamp_millis() * add example test * add nanos too --- .../src/conversions/into/datetime.rs | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/src/conversions/into/datetime.rs b/crates/nu-command/src/conversions/into/datetime.rs index f39084c6e..3de969744 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(),