1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::time::chrono::{Offset, Time};

impl Time {
    /// Returns `true` if _time_ occurs during Daylight Saving Time in its time
    /// zone.
    ///
    /// # Implementation notes
    ///
    /// This function is not implemented and always returns `false`.
    #[inline]
    #[must_use]
    pub fn is_dst(self) -> bool {
        // TODO: compute whether a `Time` is daylight time.
        // `chrono` does not expose this.
        let _ = self;
        false
    }

    /// Returns `true` if time represents a time in UTC (GMT).
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let local_time = Time::now();
    /// assert!(!local_time.is_utc());
    /// let utc_time = local_time.to_utc();
    /// assert!(utc_time.is_utc());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_utc(self) -> bool {
        let Self { offset, .. } = self;
        matches!(offset, Offset::Utc)
    }

    /// Returns a new `Time` object representing time in local time (using the
    /// local time zone in effect for this process).
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let local_time = Time::now();
    /// assert!(!local_time.is_utc());
    ///
    /// let local_time2 = local_time.to_local();
    /// assert!(!local_time2.is_utc());
    ///
    /// let utc_time = local_time.to_utc();
    /// assert!(utc_time.is_utc());
    /// ```
    #[inline]
    #[must_use]
    pub const fn to_local(self) -> Self {
        let Self {
            timestamp,
            sub_second_nanos,
            ..
        } = self;
        Self {
            timestamp,
            sub_second_nanos,
            offset: Offset::Local,
        }
    }

    /// Returns a new `Time` object representing time in UTC.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let local_time = Time::now();
    /// assert!(!local_time.is_utc());
    ///
    /// let utc_time = local_time.to_utc();
    /// assert!(utc_time.is_utc());
    ///
    /// let utc_time2 = utc_time.to_utc();
    /// assert!(utc_time2.is_utc());
    /// ```
    #[inline]
    #[must_use]
    pub const fn to_utc(self) -> Self {
        let Self {
            timestamp,
            sub_second_nanos,
            ..
        } = self;
        Self {
            timestamp,
            sub_second_nanos,
            offset: Offset::Utc,
        }
    }

    /// Returns the name of the time zone used for _time_.
    #[inline]
    #[must_use]
    #[allow(clippy::match_same_arms)]
    pub fn timezone(self) -> Option<&'static str> {
        match self.offset {
            Offset::Utc => Some("UTC"),
            // TODO: Should be a zone string.
            Offset::Local => None,
            // TODO: Should be a zone string.
            Offset::Tz(_) => None,
            Offset::Fixed(_) => None,
        }
    }
}