Skip to main content

kine_core/tz/
utc.rs

1use core::{fmt::Display, str::FromStr};
2
3use crate::{Calendar, CalendarTime, OffsetTime, Sigil, TimeZone};
4
5use super::InvalidSigilError;
6
7/// The UTC timezone
8#[derive(Clone, Copy, PartialEq, Eq)]
9pub struct Utc;
10
11/// Sigil for the UTC timezone
12///
13/// This is only exposed so that it is possible to write the `OffsetTime<UtcSigil>` struct.
14#[derive(Clone, Copy, PartialEq, Eq)]
15pub struct UtcSigil;
16
17impl TimeZone for Utc {
18    type Sigil = UtcSigil;
19}
20
21impl Calendar for Utc {
22    type Time = OffsetTime<UtcSigil>;
23
24    fn write(&self, t: &crate::Time) -> crate::Result<Self::Time> {
25        let t = crate::providers::UTC.write(t)?;
26        Ok(Self::Time::from_pseudo_nanos_since_posix_epoch(
27            UtcSigil,
28            t.as_pseudo_nanos_since_posix_epoch(),
29            t.extra_nanos(),
30        ))
31    }
32}
33
34impl Sigil for UtcSigil {
35    fn read(&self, t: &OffsetTime<Self>) -> crate::Result<crate::TimeResult> {
36        OffsetTime::<crate::providers::UtcSigil>::from_pseudo_nanos_since_posix_epoch(
37            crate::providers::UTC_SIGIL,
38            t.as_pseudo_nanos_since_posix_epoch(),
39            t.extra_nanos(),
40        )
41        .read()
42    }
43}
44
45impl Display for UtcSigil {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        f.write_str("Z")
48    }
49}
50
51impl FromStr for UtcSigil {
52    type Err = InvalidSigilError;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        match s {
56            "Z" => Ok(UtcSigil),
57            " UTC" => Ok(UtcSigil),
58            _ => Err(InvalidSigilError),
59        }
60    }
61}