1use chrono::Local;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct DateNow;
6
7impl Command for DateNow {
8 fn name(&self) -> &str {
9 "date now"
10 }
11
12 fn signature(&self) -> Signature {
13 Signature::build("date now")
14 .input_output_types(vec![(Type::Nothing, Type::Date)])
15 .category(Category::Date)
16 }
17
18 fn description(&self) -> &str {
19 "Get the current date."
20 }
21
22 fn search_terms(&self) -> Vec<&str> {
23 vec!["present", "current-time"]
24 }
25
26 fn run(
27 &self,
28 _engine_state: &EngineState,
29 _stack: &mut Stack,
30 call: &Call,
31 _input: PipelineData,
32 ) -> Result<PipelineData, ShellError> {
33 let head = call.head;
34 let dt = Local::now();
35 Ok(Value::date(dt.with_timezone(dt.offset()), head).into_pipeline_data())
36 }
37
38 fn examples(&self) -> Vec<Example<'_>> {
39 vec![
40 Example {
41 description: "Get the current date and format it in a given format string.",
42 example: r#"date now | format date "%Y-%m-%d %H:%M:%S""#,
43 result: None,
44 },
45 Example {
46 description: "Get the current date and format it according to the RFC 3339 standard.",
47 example: r#"date now | format date "%+""#,
48 result: None,
49 },
50 Example {
51 description: "Get the time duration since 2019-04-30.",
52 example: r#"(date now) - 2019-05-01"#,
53 result: None,
54 },
55 Example {
56 description: "Get the time duration since a more specific time.",
57 example: r#"(date now) - 2019-05-01T04:12:05.20+08:00"#,
58 result: None,
59 },
60 Example {
61 description: "Get current time and format it in the debug format (RFC 2822 with timezone)",
62 example: r#"date now | debug"#,
63 result: None,
64 },
65 ]
66 }
67}