nushell/src/commands/date.rs

93 lines
2.4 KiB
Rust
Raw Normal View History

2019-07-26 06:09:19 +02:00
use crate::errors::ShellError;
use crate::object::{Dictionary, Value};
use crate::prelude::*;
use chrono::{DateTime, Local, Utc};
use crate::parser::registry::{CommandConfig, NamedType};
use chrono::{Datelike, TimeZone, Timelike};
use core::fmt::Display;
use indexmap::IndexMap;
pub struct Date;
impl Command for Date {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
date(args)
}
fn name(&self) -> &str {
"date"
}
fn config(&self) -> CommandConfig {
let mut named: IndexMap<String, NamedType> = IndexMap::new();
named.insert("utc".to_string(), NamedType::Switch);
named.insert("local".to_string(), NamedType::Switch);
CommandConfig {
name: self.name().to_string(),
positional: vec![],
rest_positional: false,
named,
is_sink: true,
is_filter: false,
}
}
}
2019-08-01 03:58:42 +02:00
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, span: Span) -> Tagged<Value>
2019-07-26 06:09:19 +02:00
where
T::Offset: Display,
{
let mut indexmap = IndexMap::new();
indexmap.insert(
"year".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.year()), span),
2019-07-26 06:09:19 +02:00
);
indexmap.insert(
"month".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.month()), span),
2019-07-26 06:09:19 +02:00
);
indexmap.insert(
"day".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.day()), span),
2019-07-26 06:09:19 +02:00
);
indexmap.insert(
"hour".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.hour()), span),
2019-07-26 06:09:19 +02:00
);
indexmap.insert(
"minute".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.minute()), span),
2019-07-26 06:09:19 +02:00
);
indexmap.insert(
"second".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::int(dt.second()), span),
2019-07-26 06:09:19 +02:00
);
let tz = dt.offset();
indexmap.insert(
"timezone".to_string(),
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::string(format!("{}", tz)), span),
2019-07-26 06:09:19 +02:00
);
2019-08-01 03:58:42 +02:00
Tagged::from_item(Value::Object(Dictionary::from(indexmap)), span)
2019-07-26 06:09:19 +02:00
}
pub fn date(args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut date_out = VecDeque::new();
let span = args.call_info.name_span.unwrap();
let value = if args.has("utc") {
let utc: DateTime<Utc> = Utc::now();
date_to_value(utc, span)
} else {
let local: DateTime<Local> = Local::now();
date_to_value(local, span)
};
date_out.push_back(value);
Ok(date_out.to_output_stream())
}