mz_pgtz/
abbrev.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use chrono::FixedOffset;
11use chrono_tz::Tz;
12use uncased::UncasedStr;
13
14use crate::timezone::Timezone;
15
16/// An abbreviation for a time zone.
17pub struct TimezoneAbbrev {
18    /// The name of the abbreviation.
19    pub abbrev: &'static str,
20    /// What the abbreviation specifies.
21    pub spec: TimezoneAbbrevSpec,
22}
23
24impl TimezoneAbbrev {
25    /// Returns the [`Timezone`] that the abbreviation specifies.
26    pub fn timezone(&self) -> Timezone {
27        match &self.spec {
28            TimezoneAbbrevSpec::FixedOffset { offset, .. } => Timezone::FixedOffset(*offset),
29            TimezoneAbbrevSpec::Tz(tz) => Timezone::Tz(*tz),
30        }
31    }
32}
33
34/// What a [`TimezoneAbbrev`] can specify.
35pub enum TimezoneAbbrevSpec {
36    /// A fixed offset from UTC.
37    FixedOffset {
38        /// The offset from UTC.
39        offset: FixedOffset,
40        /// Whether this is abbreviation represents a Daylight Saving Time
41        /// offset.
42        is_dst: bool,
43    },
44    /// A reference to a time zone in the IANA Time Zone Database.
45    Tz(Tz),
46}
47
48const fn make_fixed_offset(utc_offset_secs: i32) -> FixedOffset {
49    // `unwrap()` is not stable in `const` contexts, so manually match.
50    match FixedOffset::east_opt(utc_offset_secs) {
51        None => panic!("invalid fixed offset"),
52        Some(fixed_offset) => fixed_offset,
53    }
54}
55
56include!(concat!(env!("OUT_DIR"), "/abbrev.gen.rs"));
57
58/// The SQL definition of the contents of the `mz_timezone_abbreviations` view.
59pub const MZ_CATALOG_TIMEZONE_ABBREVIATIONS_SQL: &str =
60    include_str!(concat!(env!("OUT_DIR"), "/abbrev.gen.sql"));