nushell/crates/nu-cli/src/commands/date.rs

125 lines
3.0 KiB
Rust
Raw Normal View History

2019-07-26 06:09:19 +02:00
use crate::prelude::*;
use chrono::{DateTime, Local, Utc};
use nu_errors::ShellError;
use nu_protocol::{Dictionary, Value};
2019-07-26 06:09:19 +02:00
2019-08-15 07:02:02 +02:00
use crate::commands::WholeStreamCommand;
2019-07-26 06:09:19 +02:00
use chrono::{Datelike, TimeZone, Timelike};
use core::fmt::Display;
use indexmap::IndexMap;
use nu_protocol::{Signature, UntaggedValue};
2019-07-26 06:09:19 +02:00
pub struct Date;
2020-05-29 10:22:52 +02:00
#[async_trait]
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for Date {
fn name(&self) -> &str {
"date"
}
fn signature(&self) -> Signature {
2019-10-28 06:15:35 +01:00
Signature::build("date")
.switch("utc", "use universal time (UTC)", Some('u'))
.switch("local", "use the local time", Some('l'))
}
fn usage(&self) -> &str {
"Get the current datetime."
}
2020-05-29 10:22:52 +02:00
async fn run(
2019-08-09 06:51:21 +02:00
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
date(args, registry).await
2019-07-26 06:09:19 +02:00
}
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,
},
]
}
2019-07-26 06:09:19 +02:00
}
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag) -> Value
2019-07-26 06:09:19 +02:00
where
T::Offset: Display,
{
let mut indexmap = IndexMap::new();
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),
);
2019-07-26 06:09:19 +02:00
let tz = dt.offset();
indexmap.insert(
"timezone".to_string(),
UntaggedValue::string(format!("{}", tz)).into_value(&tag),
2019-07-26 06:09:19 +02:00
);
UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag)
2019-07-26 06:09:19 +02:00
}
pub async fn date(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
2019-08-09 06:51:21 +02:00
let tag = args.call_info.name_tag.clone();
2019-07-26 06:09:19 +02:00
let value = if args.has("utc") {
let utc: DateTime<Utc> = Utc::now();
date_to_value(utc, tag)
} else {
let local: DateTime<Local> = Local::now();
date_to_value(local, tag)
};
2019-07-26 06:09:19 +02:00
Ok(OutputStream::one(value))
2019-07-26 06:09:19 +02:00
}
#[cfg(test)]
mod tests {
use super::Date;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Date {})
}
}