utc-cron-expr 11.1.0

Zero-dependency 5-field and 6-field POSIX/Quartz cron expression parser, O(1) bitmask matcher, and fast civil-calendar next-occurrence calculator.
Documentation
use super::*;

/// Construction and conversion methods for [`UtcDateTime`].
impl UtcDateTime {
    /// Decomposes Unix epoch seconds into a [`UtcDateTime`].
    ///
    /// # Arguments
    ///
    /// - `i64` - Signed Unix timestamp in seconds.
    ///
    /// # Returns
    ///
    /// - `Self` - Decomposed UTC civil date and time.
    #[inline(always)]
    pub const fn from_unix_secs(secs: i64) -> Self {
        let days: i64 = secs.div_euclid(86_400);
        let sod: u32 = secs.rem_euclid(86_400) as u32;
        let (year, month, day): (i32, u8, u8) = civil_from_days(days);
        let weekday: u8 = weekday_from_days(days);
        let hour: u8 = (sod / 3600) as u8;
        let minute: u8 = ((sod % 3600) / 60) as u8;
        let second: u8 = (sod % 60) as u8;
        Self {
            year,
            month,
            day,
            weekday,
            hour,
            minute,
            second,
        }
    }

    /// Converts this [`UtcDateTime`] back to Unix epoch seconds.
    ///
    /// # Returns
    ///
    /// - `i64` - Signed Unix timestamp in seconds.
    #[inline(always)]
    pub const fn to_unix_secs(self) -> i64 {
        let Self {
            year,
            month,
            day,
            hour,
            minute,
            second,
            ..
        } = self;
        let days: i64 = days_from_civil(year, month, day);
        days * 86_400 + (hour as i64) * 3600 + (minute as i64) * 60 + (second as i64)
    }

    /// Returns the full Gregorian year.
    ///
    /// # Returns
    ///
    /// - `i32` - Year.
    #[inline(always)]
    pub const fn get_year(self) -> i32 {
        let Self { year, .. } = self;
        year
    }

    /// Returns the month (`1..=12`).
    ///
    /// # Returns
    ///
    /// - `u8` - Month.
    #[inline(always)]
    pub const fn get_month(self) -> u8 {
        let Self { month, .. } = self;
        month
    }

    /// Returns the day of month (`1..=31`).
    ///
    /// # Returns
    ///
    /// - `u8` - Day of month.
    #[inline(always)]
    pub const fn get_day(self) -> u8 {
        let Self { day, .. } = self;
        day
    }

    /// Returns the weekday (`0 = Sunday ..= 6 = Saturday`).
    ///
    /// # Returns
    ///
    /// - `u8` - Weekday.
    #[inline(always)]
    pub const fn get_weekday(self) -> u8 {
        let Self { weekday, .. } = self;
        weekday
    }

    /// Returns the hour (`0..=23`).
    ///
    /// # Returns
    ///
    /// - `u8` - Hour.
    #[inline(always)]
    pub const fn get_hour(self) -> u8 {
        let Self { hour, .. } = self;
        hour
    }

    /// Returns the minute (`0..=59`).
    ///
    /// # Returns
    ///
    /// - `u8` - Minute.
    #[inline(always)]
    pub const fn get_minute(self) -> u8 {
        let Self { minute, .. } = self;
        minute
    }

    /// Returns the second (`0..=59`).
    ///
    /// # Returns
    ///
    /// - `u8` - Second.
    #[inline(always)]
    pub const fn get_second(self) -> u8 {
        let Self { second, .. } = self;
        second
    }
}

/// Formats [`UtcDateTime`] as an ISO-8601 UTC timestamp (`YYYY-MM-DDTHH:MM:SSZ`).
impl fmt::Display for UtcDateTime {
    /// Writes the UTC timestamp to the formatter.
    ///
    /// # Arguments
    ///
    /// - `&mut fmt::Formatter<'_>` - Output formatter.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Formatting result.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
            self.get_year(),
            self.get_month(),
            self.get_day(),
            self.get_hour(),
            self.get_minute(),
            self.get_second(),
        )
    }
}