mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
Add date command
This commit is contained in:
parent
ed040f2715
commit
96f26b30a7
4 changed files with 124 additions and 3 deletions
|
@ -122,6 +122,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
|
||||||
| cd path | Change to a new path |
|
| cd path | Change to a new path |
|
||||||
| cp source path | Copy files |
|
| cp source path | Copy files |
|
||||||
| ls (path) | View the contents of the current or given path |
|
| ls (path) | View the contents of the current or given path |
|
||||||
|
| date (--utc) | Get the current datetime |
|
||||||
| ps | View current processes |
|
| ps | View current processes |
|
||||||
| sysinfo | View information about the current system |
|
| sysinfo | View information about the current system |
|
||||||
| open {filename or url} | Load a file into a cell, convert to table if possible (avoid by appending '--raw') |
|
| open {filename or url} | Load a file into a cell, convert to table if possible (avoid by appending '--raw') |
|
||||||
|
|
|
@ -180,6 +180,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||||
Arc::new(Remove),
|
Arc::new(Remove),
|
||||||
Arc::new(Copycp),
|
Arc::new(Copycp),
|
||||||
Arc::new(Open),
|
Arc::new(Open),
|
||||||
|
Arc::new(Date),
|
||||||
Arc::new(Where),
|
Arc::new(Where),
|
||||||
Arc::new(Config),
|
Arc::new(Config),
|
||||||
Arc::new(SkipWhile),
|
Arc::new(SkipWhile),
|
||||||
|
|
|
@ -4,12 +4,12 @@ crate mod macros;
|
||||||
crate mod args;
|
crate mod args;
|
||||||
crate mod autoview;
|
crate mod autoview;
|
||||||
crate mod cd;
|
crate mod cd;
|
||||||
crate mod cp;
|
|
||||||
crate mod rm;
|
|
||||||
crate mod classified;
|
crate mod classified;
|
||||||
crate mod clip;
|
crate mod clip;
|
||||||
crate mod command;
|
crate mod command;
|
||||||
crate mod config;
|
crate mod config;
|
||||||
|
crate mod cp;
|
||||||
|
crate mod date;
|
||||||
crate mod exit;
|
crate mod exit;
|
||||||
crate mod first;
|
crate mod first;
|
||||||
crate mod from_csv;
|
crate mod from_csv;
|
||||||
|
@ -26,6 +26,7 @@ crate mod pick;
|
||||||
crate mod plugin;
|
crate mod plugin;
|
||||||
crate mod ps;
|
crate mod ps;
|
||||||
crate mod reject;
|
crate mod reject;
|
||||||
|
crate mod rm;
|
||||||
crate mod save;
|
crate mod save;
|
||||||
crate mod size;
|
crate mod size;
|
||||||
crate mod skip_while;
|
crate mod skip_while;
|
||||||
|
@ -46,7 +47,8 @@ crate mod where_;
|
||||||
crate use command::command;
|
crate use command::command;
|
||||||
crate use config::Config;
|
crate use config::Config;
|
||||||
crate use cp::Copycp;
|
crate use cp::Copycp;
|
||||||
crate use rm::Remove;
|
crate use date::Date;
|
||||||
crate use open::Open;
|
crate use open::Open;
|
||||||
|
crate use rm::Remove;
|
||||||
crate use skip_while::SkipWhile;
|
crate use skip_while::SkipWhile;
|
||||||
crate use where_::Where;
|
crate use where_::Where;
|
||||||
|
|
117
src/commands/date.rs
Normal file
117
src/commands/date.rs
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
use crate::errors::ShellError;
|
||||||
|
use crate::object::{Dictionary, Value};
|
||||||
|
use crate::parser::Spanned;
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn date_to_value<T: TimeZone>(dt: DateTime<T>, span: Span) -> Spanned<Value>
|
||||||
|
where
|
||||||
|
T::Offset: Display,
|
||||||
|
{
|
||||||
|
let mut indexmap = IndexMap::new();
|
||||||
|
|
||||||
|
indexmap.insert(
|
||||||
|
"year".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.year()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"month".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.month()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"day".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.day()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"hour".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.hour()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"minute".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.minute()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
indexmap.insert(
|
||||||
|
"second".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::int(dt.second()),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let tz = dt.offset();
|
||||||
|
indexmap.insert(
|
||||||
|
"timezone".to_string(),
|
||||||
|
Spanned {
|
||||||
|
item: Value::string(format!("{}", tz)),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
Spanned {
|
||||||
|
item: Value::Object(Dictionary::from(indexmap)),
|
||||||
|
span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
Loading…
Reference in a new issue