Date utility commands (#2780)

* updated & added date related commands based on the new design

* added proper error handling when date format string is invalid

* fixed format issue

* fixed an issue caused due to the change in primitive Date type

* added `date list-timezone` command to list all supported time zones and updated `date to-timezone` accordingly
This commit is contained in:
Stan Zhang
2020-12-13 02:18:03 +08:00
committed by GitHub
parent e000ed47cd
commit 83c874666a
21 changed files with 538 additions and 187 deletions

View File

@ -1,7 +1,7 @@
pub(crate) mod shape;
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset, Utc};
use derive_new::new;
use nu_errors::ShellError;
use nu_protocol::{
@ -75,8 +75,8 @@ pub enum CompareValues {
Ints(BigInt, BigInt),
Decimals(BigDecimal, BigDecimal),
String(String, String),
Date(DateTime<Utc>, DateTime<Utc>),
DateDuration(DateTime<Utc>, BigInt),
Date(DateTime<FixedOffset>, DateTime<FixedOffset>),
DateDuration(DateTime<FixedOffset>, BigInt),
Booleans(bool, bool),
}
@ -94,9 +94,10 @@ impl CompareValues {
Span::unknown(),
)
.expect("Could not convert nushell Duration into chrono Duration.");
let right: DateTime<Utc> = Utc::now()
let right: DateTime<FixedOffset> = Utc::now()
.checked_sub_signed(duration)
.expect("Data overflow");
.expect("Data overflow")
.into();
right.cmp(left)
}
CompareValues::Booleans(left, right) => left.cmp(right),

View File

@ -1,6 +1,6 @@
// use crate::config::{Conf, NuConfig};
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset};
use indexmap::map::IndexMap;
use nu_protocol::RangeInclusion;
use nu_protocol::{format_primitive, ColumnPath, Dictionary, Primitive, UntaggedValue, Value};
@ -31,7 +31,7 @@ pub enum InlineShape {
ColumnPath(ColumnPath),
Pattern(String),
Boolean(bool),
Date(DateTime<Utc>),
Date(DateTime<FixedOffset>),
Duration(BigInt),
Path(PathBuf),
Binary(usize),

View File

@ -25,7 +25,7 @@ impl Date {
let date = date.with_timezone(&chrono::offset::Utc);
Ok(UntaggedValue::Primitive(Primitive::Date(date)))
Ok(UntaggedValue::Primitive(Primitive::Date(date.into())))
}
pub fn naive_from_str(s: Tagged<&str>) -> Result<UntaggedValue, ShellError> {
@ -38,7 +38,7 @@ impl Date {
})?;
Ok(UntaggedValue::Primitive(Primitive::Date(
DateTime::<Utc>::from_utc(date.and_hms(12, 34, 56), Utc),
DateTime::<Utc>::from_utc(date.and_hms(12, 34, 56), Utc).into(),
)))
}
}