vector_config/external/
datetime.rs

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