nu_command/date/
list_timezone.rs

1use chrono_tz::TZ_VARIANTS;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct DateListTimezones;
6
7impl Command for DateListTimezones {
8    fn name(&self) -> &str {
9        "date list-timezone"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("date list-timezone")
14            .input_output_types(vec![(Type::Nothing, Type::table())])
15            .category(Category::Date)
16    }
17
18    fn description(&self) -> &str {
19        "List supported time zones."
20    }
21
22    fn search_terms(&self) -> Vec<&str> {
23        vec!["UTC", "GMT", "tz"]
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
35        Ok(TZ_VARIANTS
36            .iter()
37            .map(move |x| {
38                Value::record(
39                    record! { "timezone" => Value::string(x.name(), head) },
40                    head,
41                )
42            })
43            .into_pipeline_data(head, engine_state.signals().clone()))
44    }
45
46    fn examples(&self) -> Vec<Example<'_>> {
47        vec![Example {
48            example: "date list-timezone | where timezone =~ Shanghai",
49            description: "Show time zone(s) that contains 'Shanghai'",
50            result: Some(Value::test_list(vec![Value::test_record(record! {
51                "timezone" => Value::test_string("Asia/Shanghai"),
52            })])),
53        }]
54    }
55}