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

@ -19,7 +19,7 @@ use crate::value::range::{Range, RangeInclusion};
use crate::ColumnPath;
use bigdecimal::BigDecimal;
use bigdecimal::FromPrimitive;
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset, Utc};
use indexmap::IndexMap;
use nu_errors::ShellError;
use nu_source::{AnchorLocation, HasSpan, Span, Spanned, SpannedItem, Tag};
@ -248,10 +248,11 @@ impl UntaggedValue {
/// Helper for creating datatime values
pub fn system_date(s: SystemTime) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Date(s.into()))
let utc: DateTime<Utc> = s.into();
UntaggedValue::Primitive(Primitive::Date(utc.into()))
}
pub fn date(d: impl Into<DateTime<Utc>>) -> UntaggedValue {
pub fn date(d: impl Into<DateTime<FixedOffset>>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Date(d.into()))
}
@ -924,7 +925,7 @@ pub trait DateTimeExt {
fn to_value_create_tag(&self) -> Value;
}
impl DateTimeExt for DateTime<Utc> {
impl DateTimeExt for DateTime<FixedOffset> {
fn to_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Date(*self)),

View File

@ -3,7 +3,7 @@ use crate::value::column_path::ColumnPath;
use crate::value::range::{Range, RangeInclusion};
use crate::value::{serde_bigdecimal, serde_bigint};
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use chrono::{DateTime, FixedOffset, Utc};
use nu_errors::{ExpectedRange, ShellError};
use nu_source::{PrettyDebug, Span, SpannedItem};
use num_bigint::BigInt;
@ -42,8 +42,8 @@ pub enum Primitive {
Pattern(String),
/// A boolean value
Boolean(bool),
/// A date value, in UTC
Date(DateTime<Utc>),
/// A date value
Date(DateTime<FixedOffset>),
/// A count in the number of nanoseconds
#[serde(with = "serde_bigint")]
Duration(BigInt),
@ -385,8 +385,8 @@ pub fn format_duration(duration: &BigInt) -> String {
}
#[allow(clippy::cognitive_complexity)]
/// Format a UTC date value into a humanized string (eg "1 week ago" instead of a formal date string)
pub fn format_date(d: &DateTime<Utc>) -> String {
/// Format a date value into a humanized string (eg "1 week ago" instead of a formal date string)
pub fn format_date(d: &DateTime<FixedOffset>) -> String {
let utc: DateTime<Utc> = Utc::now();
let duration = utc.signed_duration_since(*d);