mirror of
https://github.com/nushell/nushell.git
synced 2025-05-02 17:14:27 +02:00
parent
eb2ba470c7
commit
2ea42f296c
@ -261,7 +261,9 @@ pub fn create_default_context(
|
|||||||
whole_stream_command(Save),
|
whole_stream_command(Save),
|
||||||
whole_stream_command(Touch),
|
whole_stream_command(Touch),
|
||||||
whole_stream_command(Cpy),
|
whole_stream_command(Cpy),
|
||||||
whole_stream_command(Date),
|
whole_stream_command(DateNow),
|
||||||
|
whole_stream_command(DateUTC),
|
||||||
|
whole_stream_command(DateFormat),
|
||||||
whole_stream_command(Cal),
|
whole_stream_command(Cal),
|
||||||
whole_stream_command(Mkdir),
|
whole_stream_command(Mkdir),
|
||||||
whole_stream_command(Mv),
|
whole_stream_command(Mv),
|
||||||
|
@ -147,7 +147,7 @@ pub(crate) use config::{
|
|||||||
};
|
};
|
||||||
pub(crate) use count::Count;
|
pub(crate) use count::Count;
|
||||||
pub(crate) use cp::Cpy;
|
pub(crate) use cp::Cpy;
|
||||||
pub(crate) use date::Date;
|
pub(crate) use date::{DateFormat, DateNow, DateUTC};
|
||||||
pub(crate) use debug::Debug;
|
pub(crate) use debug::Debug;
|
||||||
pub(crate) use default::Default;
|
pub(crate) use default::Default;
|
||||||
pub(crate) use do_::Do;
|
pub(crate) use do_::Do;
|
||||||
|
@ -1,175 +0,0 @@
|
|||||||
use crate::prelude::*;
|
|
||||||
use chrono::{DateTime, Local, Utc};
|
|
||||||
use nu_errors::ShellError;
|
|
||||||
use nu_protocol::{Dictionary, Value};
|
|
||||||
|
|
||||||
use crate::commands::WholeStreamCommand;
|
|
||||||
use chrono::{Datelike, TimeZone, Timelike};
|
|
||||||
use core::fmt::Display;
|
|
||||||
use indexmap::IndexMap;
|
|
||||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
|
||||||
|
|
||||||
pub struct Date;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl WholeStreamCommand for Date {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
"date"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
|
||||||
Signature::build("date")
|
|
||||||
.switch("utc", "use universal time (UTC)", Some('u'))
|
|
||||||
.switch("local", "use the local time", Some('l'))
|
|
||||||
.named(
|
|
||||||
"format",
|
|
||||||
SyntaxShape::String,
|
|
||||||
"report datetime in supplied strftime format",
|
|
||||||
Some('f'),
|
|
||||||
)
|
|
||||||
.switch("raw", "print date without tables", Some('r'))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
|
||||||
"Get the current datetime."
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run(
|
|
||||||
&self,
|
|
||||||
args: CommandArgs,
|
|
||||||
registry: &CommandRegistry,
|
|
||||||
) -> Result<OutputStream, ShellError> {
|
|
||||||
date(args, registry).await
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
|
||||||
vec![
|
|
||||||
Example {
|
|
||||||
description: "Get the current local time and date",
|
|
||||||
example: "date",
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
Example {
|
|
||||||
description: "Get the current UTC time and date",
|
|
||||||
example: "date --utc",
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
Example {
|
|
||||||
description: "Get the current time and date and report it based on format",
|
|
||||||
example: "date --format '%Y-%m-%d %H:%M:%S.%f %z'",
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
Example {
|
|
||||||
description: "Get the current time and date and report it without a table",
|
|
||||||
example: "date --format '%Y-%m-%d %H:%M:%S.%f %z' --raw",
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn date_to_value_raw<T: TimeZone>(dt: DateTime<T>, dt_format: String) -> String
|
|
||||||
where
|
|
||||||
T::Offset: Display,
|
|
||||||
{
|
|
||||||
let result = dt.format(&dt_format);
|
|
||||||
format!("{}", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag, dt_format: String) -> Value
|
|
||||||
where
|
|
||||||
T::Offset: Display,
|
|
||||||
{
|
|
||||||
let mut indexmap = IndexMap::new();
|
|
||||||
|
|
||||||
if dt_format.is_empty() {
|
|
||||||
indexmap.insert(
|
|
||||||
"year".to_string(),
|
|
||||||
UntaggedValue::int(dt.year()).into_value(&tag),
|
|
||||||
);
|
|
||||||
indexmap.insert(
|
|
||||||
"month".to_string(),
|
|
||||||
UntaggedValue::int(dt.month()).into_value(&tag),
|
|
||||||
);
|
|
||||||
indexmap.insert(
|
|
||||||
"day".to_string(),
|
|
||||||
UntaggedValue::int(dt.day()).into_value(&tag),
|
|
||||||
);
|
|
||||||
indexmap.insert(
|
|
||||||
"hour".to_string(),
|
|
||||||
UntaggedValue::int(dt.hour()).into_value(&tag),
|
|
||||||
);
|
|
||||||
indexmap.insert(
|
|
||||||
"minute".to_string(),
|
|
||||||
UntaggedValue::int(dt.minute()).into_value(&tag),
|
|
||||||
);
|
|
||||||
indexmap.insert(
|
|
||||||
"second".to_string(),
|
|
||||||
UntaggedValue::int(dt.second()).into_value(&tag),
|
|
||||||
);
|
|
||||||
|
|
||||||
let tz = dt.offset();
|
|
||||||
indexmap.insert(
|
|
||||||
"timezone".to_string(),
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn date(
|
|
||||||
args: CommandArgs,
|
|
||||||
registry: &CommandRegistry,
|
|
||||||
) -> Result<OutputStream, ShellError> {
|
|
||||||
let registry = registry.clone();
|
|
||||||
let args = args.evaluate_once(®istry).await?;
|
|
||||||
let tag = args.call_info.name_tag.clone();
|
|
||||||
let raw = args.has("raw");
|
|
||||||
|
|
||||||
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 utc: DateTime<Utc> = Utc::now();
|
|
||||||
if raw {
|
|
||||||
UntaggedValue::string(date_to_value_raw(utc, dt_fmt)).into_untagged_value()
|
|
||||||
} else {
|
|
||||||
date_to_value(utc, tag, dt_fmt)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let local: DateTime<Local> = Local::now();
|
|
||||||
if raw {
|
|
||||||
UntaggedValue::string(date_to_value_raw(local, dt_fmt)).into_untagged_value()
|
|
||||||
} else {
|
|
||||||
date_to_value(local, tag, dt_fmt)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(OutputStream::one(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::Date;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn examples_work_as_expected() {
|
|
||||||
use crate::examples::test as test_examples;
|
|
||||||
|
|
||||||
test_examples(Date {})
|
|
||||||
}
|
|
||||||
}
|
|
63
crates/nu-cli/src/commands/date/format.rs
Normal file
63
crates/nu-cli/src/commands/date/format.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
|
||||||
|
use crate::commands::date::utils::{date_to_value, date_to_value_raw};
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
|
||||||
|
pub struct Date;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct FormatArgs {
|
||||||
|
format: Tagged<String>,
|
||||||
|
raw: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl WholeStreamCommand for Date {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"date format"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("date format")
|
||||||
|
.required("format", SyntaxShape::String, "strftime format")
|
||||||
|
.switch("raw", "print date without tables", Some('r'))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"format the current date using the given format string."
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
format(args, registry).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn format(
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let registry = registry.clone();
|
||||||
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
let (FormatArgs { format, raw }, _) = args.process(®istry).await?;
|
||||||
|
|
||||||
|
let dt_fmt = format.to_string();
|
||||||
|
|
||||||
|
let value = {
|
||||||
|
let local: DateTime<Local> = Local::now();
|
||||||
|
if let Some(true) = raw {
|
||||||
|
UntaggedValue::string(date_to_value_raw(local, dt_fmt)).into_untagged_value()
|
||||||
|
} else {
|
||||||
|
date_to_value(local, tag, dt_fmt)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(OutputStream::one(value))
|
||||||
|
}
|
9
crates/nu-cli/src/commands/date/mod.rs
Normal file
9
crates/nu-cli/src/commands/date/mod.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
pub mod format;
|
||||||
|
pub mod now;
|
||||||
|
pub mod utc;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
pub use format::Date as DateFormat;
|
||||||
|
pub use now::Date as DateNow;
|
||||||
|
pub use utc::Date as DateUTC;
|
50
crates/nu-cli/src/commands/date/now.rs
Normal file
50
crates/nu-cli/src/commands/date/now.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
|
||||||
|
use crate::commands::date::utils::date_to_value;
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use nu_protocol::Signature;
|
||||||
|
|
||||||
|
pub struct Date;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl WholeStreamCommand for Date {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"date now"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("date now")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"return the current date."
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
now(args, registry).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn now(
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let registry = registry.clone();
|
||||||
|
let args = args.evaluate_once(®istry).await?;
|
||||||
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
|
||||||
|
let no_fmt = "".to_string();
|
||||||
|
|
||||||
|
let value = {
|
||||||
|
let local: DateTime<Local> = Local::now();
|
||||||
|
date_to_value(local, tag, no_fmt)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(OutputStream::one(value))
|
||||||
|
}
|
50
crates/nu-cli/src/commands/date/utc.rs
Normal file
50
crates/nu-cli/src/commands/date/utc.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
|
||||||
|
use crate::commands::date::utils::date_to_value;
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use nu_protocol::Signature;
|
||||||
|
|
||||||
|
pub struct Date;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl WholeStreamCommand for Date {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"date utc"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("date utc")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"return the current date in utc."
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
utc(args, registry).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn utc(
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let registry = registry.clone();
|
||||||
|
let args = args.evaluate_once(®istry).await?;
|
||||||
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
|
||||||
|
let no_fmt = "".to_string();
|
||||||
|
|
||||||
|
let value = {
|
||||||
|
let local: DateTime<Utc> = Utc::now();
|
||||||
|
date_to_value(local, tag, no_fmt)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(OutputStream::one(value))
|
||||||
|
}
|
64
crates/nu-cli/src/commands/date/utils.rs
Normal file
64
crates/nu-cli/src/commands/date/utils.rs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use chrono::DateTime;
|
||||||
|
use nu_protocol::{Dictionary, Value};
|
||||||
|
|
||||||
|
use chrono::{Datelike, TimeZone, Timelike};
|
||||||
|
use core::fmt::Display;
|
||||||
|
use indexmap::IndexMap;
|
||||||
|
use nu_protocol::UntaggedValue;
|
||||||
|
|
||||||
|
pub fn date_to_value_raw<T: TimeZone>(dt: DateTime<T>, dt_format: String) -> String
|
||||||
|
where
|
||||||
|
T::Offset: Display,
|
||||||
|
{
|
||||||
|
let result = dt.format(&dt_format);
|
||||||
|
format!("{}", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag, dt_format: String) -> Value
|
||||||
|
where
|
||||||
|
T::Offset: Display,
|
||||||
|
{
|
||||||
|
let mut indexmap = IndexMap::new();
|
||||||
|
|
||||||
|
if dt_format.is_empty() {
|
||||||
|
indexmap.insert(
|
||||||
|
"year".to_string(),
|
||||||
|
UntaggedValue::int(dt.year()).into_value(&tag),
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"month".to_string(),
|
||||||
|
UntaggedValue::int(dt.month()).into_value(&tag),
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"day".to_string(),
|
||||||
|
UntaggedValue::int(dt.day()).into_value(&tag),
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"hour".to_string(),
|
||||||
|
UntaggedValue::int(dt.hour()).into_value(&tag),
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"minute".to_string(),
|
||||||
|
UntaggedValue::int(dt.minute()).into_value(&tag),
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"second".to_string(),
|
||||||
|
UntaggedValue::int(dt.second()).into_value(&tag),
|
||||||
|
);
|
||||||
|
|
||||||
|
let tz = dt.offset();
|
||||||
|
indexmap.insert(
|
||||||
|
"timezone".to_string(),
|
||||||
|
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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user