vector_config/external/
datetime.rs

1use std::cell::RefCell;
2
3use chrono_tz::Tz;
4use serde_json::Value;
5use vector_config_common::{attributes::CustomAttribute, constants};
6use vrl::compiler::TimeZone;
7
8use crate::{
9    Configurable, GenerateError, Metadata, ToValue,
10    schema::{
11        SchemaGenerator, SchemaObject, apply_base_metadata, generate_const_string_schema,
12        generate_one_of_schema, get_or_generate_schema,
13    },
14};
15
16// TODO: Consider an approach for generating schema of "fixed string value, or remainder" structure
17// used by this type.
18impl Configurable for TimeZone {
19    fn referenceable_name() -> Option<&'static str> {
20        Some(std::any::type_name::<Self>())
21    }
22
23    fn metadata() -> Metadata {
24        let mut metadata = Metadata::default();
25        metadata.set_title("Timezone to use for any date specifiers in template strings.");
26        metadata.set_description(r#"This can refer to any valid timezone as defined in the [TZ database][tzdb], or "local" which refers to the system local timezone. It will default to the [globally configured timezone](https://vector.dev/docs/reference/configuration/global-options/#timezone).
27
28[tzdb]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"#);
29        metadata.add_custom_attribute(CustomAttribute::kv(
30            constants::DOCS_META_ENUM_TAGGING,
31            "untagged",
32        ));
33        metadata.add_custom_attribute(CustomAttribute::kv(constants::DOCS_META_EXAMPLES, "local"));
34        metadata.add_custom_attribute(CustomAttribute::kv(
35            constants::DOCS_META_EXAMPLES,
36            "America/New_York",
37        ));
38        metadata.add_custom_attribute(CustomAttribute::kv(
39            constants::DOCS_META_EXAMPLES,
40            "EST5EDT",
41        ));
42        metadata
43    }
44
45    fn generate_schema(
46        generator: &RefCell<SchemaGenerator>,
47    ) -> Result<SchemaObject, GenerateError> {
48        let mut local_schema = generate_const_string_schema("local".to_string());
49        let mut local_metadata = Metadata::with_description("System local timezone.");
50        local_metadata.add_custom_attribute(CustomAttribute::kv("logical_name", "Local"));
51        apply_base_metadata(&mut local_schema, local_metadata);
52
53        let mut tz_metadata = Metadata::with_title("A named timezone.");
54        tz_metadata.set_description(
55            r#"Must be a valid name in the [TZ database][tzdb].
56
57[tzdb]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"#,
58        );
59        tz_metadata.add_custom_attribute(CustomAttribute::kv("logical_name", "Named"));
60        let tz_schema =
61            get_or_generate_schema(&Tz::as_configurable_ref(), generator, Some(tz_metadata))?;
62
63        Ok(generate_one_of_schema(&[local_schema, tz_schema]))
64    }
65}
66
67impl ToValue for TimeZone {
68    fn to_value(&self) -> Value {
69        serde_json::to_value(self).expect("Could not convert time zone to JSON")
70    }
71}