mirror of
https://github.com/nushell/nushell
synced 2025-01-10 12:19:14 +00:00
f70c6d5d48
This commit extracts Tag, Span, Text, as well as source-related debug facilities into a new crate called nu_source. This change is much bigger than one might have expected because the previous code relied heavily on implementing inherent methods on `Tagged<T>` and `Spanned<T>`, which is no longer possible. As a result, this change creates more concrete types instead of using `Tagged<T>`. One notable example: Tagged<Value> became Value, and Value became UntaggedValue. This change clarifies the intent of the code in many places, but it does make it a big change.
95 lines
2.4 KiB
Rust
95 lines
2.4 KiB
Rust
use crate::data::{Dictionary, Value};
|
|
use crate::errors::ShellError;
|
|
use crate::prelude::*;
|
|
use chrono::{DateTime, Local, Utc};
|
|
|
|
use crate::commands::WholeStreamCommand;
|
|
use crate::parser::registry::Signature;
|
|
use chrono::{Datelike, TimeZone, Timelike};
|
|
use core::fmt::Display;
|
|
use indexmap::IndexMap;
|
|
|
|
pub struct Date;
|
|
|
|
impl WholeStreamCommand for Date {
|
|
fn name(&self) -> &str {
|
|
"date"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("date")
|
|
.switch("utc", "use universal time (UTC)")
|
|
.switch("local", "use the local time")
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Get the current datetime."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
date(args, registry)
|
|
}
|
|
}
|
|
|
|
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, tag: Tag) -> Value
|
|
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),
|
|
);
|
|
|
|
let tz = dt.offset();
|
|
indexmap.insert(
|
|
"timezone".to_string(),
|
|
UntaggedValue::string(format!("{}", tz)).into_value(&tag),
|
|
);
|
|
|
|
UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag)
|
|
}
|
|
|
|
pub fn date(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let mut date_out = VecDeque::new();
|
|
let tag = args.call_info.name_tag.clone();
|
|
|
|
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)
|
|
};
|
|
|
|
date_out.push_back(value);
|
|
|
|
Ok(date_out.to_output_stream())
|
|
}
|