mirror of
https://github.com/nushell/nushell.git
synced 2025-04-19 02:38:20 +02:00
added ability to request the date in a format (#2073)
This commit is contained in:
parent
94a1968a88
commit
a30901ff7d
@ -7,7 +7,7 @@ use crate::commands::WholeStreamCommand;
|
|||||||
use chrono::{Datelike, TimeZone, Timelike};
|
use chrono::{Datelike, TimeZone, Timelike};
|
||||||
use core::fmt::Display;
|
use core::fmt::Display;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use nu_protocol::{Signature, UntaggedValue};
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||||
|
|
||||||
pub struct Date;
|
pub struct Date;
|
||||||
|
|
||||||
@ -21,6 +21,12 @@ impl WholeStreamCommand for Date {
|
|||||||
Signature::build("date")
|
Signature::build("date")
|
||||||
.switch("utc", "use universal time (UTC)", Some('u'))
|
.switch("utc", "use universal time (UTC)", Some('u'))
|
||||||
.switch("local", "use the local time", Some('l'))
|
.switch("local", "use the local time", Some('l'))
|
||||||
|
.named(
|
||||||
|
"format",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"report datetime in supplied strftime format",
|
||||||
|
Some('f'),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
@ -47,16 +53,22 @@ impl WholeStreamCommand for Date {
|
|||||||
example: "date --utc",
|
example: "date --utc",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Get the current time and date and reports in raw format",
|
||||||
|
example: "date --format '%Y-%m-%d %H:%M:%S.%f %z'",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag) -> Value
|
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag, dt_format: String) -> Value
|
||||||
where
|
where
|
||||||
T::Offset: Display,
|
T::Offset: Display,
|
||||||
{
|
{
|
||||||
let mut indexmap = IndexMap::new();
|
let mut indexmap = IndexMap::new();
|
||||||
|
|
||||||
|
if dt_format.is_empty() {
|
||||||
indexmap.insert(
|
indexmap.insert(
|
||||||
"year".to_string(),
|
"year".to_string(),
|
||||||
UntaggedValue::int(dt.year()).into_value(&tag),
|
UntaggedValue::int(dt.year()).into_value(&tag),
|
||||||
@ -87,6 +99,13 @@ where
|
|||||||
"timezone".to_string(),
|
"timezone".to_string(),
|
||||||
UntaggedValue::string(format!("{}", tz)).into_value(&tag),
|
UntaggedValue::string(format!("{}", tz)).into_value(&tag),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
let result = dt.format(&dt_format);
|
||||||
|
indexmap.insert(
|
||||||
|
"formatted".to_string(),
|
||||||
|
UntaggedValue::string(format!("{}", result)).into_value(&tag),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag)
|
UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag)
|
||||||
}
|
}
|
||||||
@ -100,12 +119,22 @@ pub async fn date(
|
|||||||
|
|
||||||
let tag = args.call_info.name_tag.clone();
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
|
||||||
|
let dt_fmt = if args.has("format") {
|
||||||
|
if let Some(dt_fmt) = args.get("format") {
|
||||||
|
dt_fmt.convert_to_string()
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
let value = if args.has("utc") {
|
let value = if args.has("utc") {
|
||||||
let utc: DateTime<Utc> = Utc::now();
|
let utc: DateTime<Utc> = Utc::now();
|
||||||
date_to_value(utc, tag)
|
date_to_value(utc, tag, dt_fmt)
|
||||||
} else {
|
} else {
|
||||||
let local: DateTime<Local> = Local::now();
|
let local: DateTime<Local> = Local::now();
|
||||||
date_to_value(local, tag)
|
date_to_value(local, tag, dt_fmt)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(OutputStream::one(value))
|
Ok(OutputStream::one(value))
|
||||||
|
Loading…
Reference in New Issue
Block a user