add ability to convert timestamp_millis() (#5876)

* add ability to convert timestamp_millis()

* add example test

* add nanos too
This commit is contained in:
Darren Schroeder 2022-06-25 09:51:41 -05:00 committed by GitHub
parent 25349a1eac
commit ef9b72d360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Spanned<String>>,
offset: Option<Spanned<i64>>,
@ -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(),