utc-cron-expr 11.2.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::*;

/// Reports whether Gregorian `year` is a leap year.
///
/// # Arguments
///
/// - `i32` - Full Gregorian year.
///
/// # Returns
///
/// - `bool` - `true` for leap years.
#[inline(always)]
pub const fn is_leap_year(year: i32) -> bool {
    (year & 3) == 0 && (year % 100 != 0 || year % 400 == 0)
}

/// Returns the number of days in Gregorian `(year, month)`.
///
/// # Arguments
///
/// - `i32` - Full Gregorian year.
/// - `u8` - Month in `1..=12`.
///
/// # Returns
///
/// - `u8` - Number of days (`28`, `29`, `30`, or `31`).
#[inline(always)]
pub const fn days_in_month(year: i32, month: u8) -> u8 {
    if month == 2 {
        return 28 + is_leap_year(year) as u8;
    }
    if month >= 1 && month <= 12 {
        DAYS_IN_MONTH_COMMON[month as usize]
    } else {
        30
    }
}

/// Converts a proleptic Gregorian date to days since `1970-01-01` (Howard Hinnant algorithm).
///
/// # Arguments
///
/// - `i32` - Full year.
/// - `u8` - Month (`1..=12`).
/// - `u8` - Day (`1..=31`).
///
/// # Returns
///
/// - `i64` - Signed days since `1970-01-01`.
#[inline(always)]
pub const fn days_from_civil(year: i32, month: u8, day: u8) -> i64 {
    let y: i64 = (year as i64) - if month <= 2 { 1 } else { 0 };
    let era: i64 = if y >= 0 { y } else { y - 399 } / 400;
    let yoe: u32 = (y - era * 400) as u32;
    let m: u32 = if month > 2 {
        (month - 3) as u32
    } else {
        (month + 9) as u32
    };
    let doy: u32 = (153 * m + 2) / 5 + (day as u32) - 1;
    let doe: u32 = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + (doe as i64) - 719_468
}

/// Converts days since `1970-01-01` back to `(year, month, day)` (Howard Hinnant algorithm).
///
/// # Arguments
///
/// - `i64` - Signed days since `1970-01-01`.
///
/// # Returns
///
/// - `(i32, u8, u8)` - `(year, month, day)`.
#[inline(always)]
pub const fn civil_from_days(days: i64) -> (i32, u8, u8) {
    let z: i64 = days + 719_468;
    let era: i64 = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe: u32 = (z - era * 146_097) as u32;
    let yoe: u32 = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let y: i64 = (yoe as i64) + era * 400;
    let doy: u32 = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp: u32 = (5 * doy + 2) / 153;
    let d: u8 = (doy - (153 * mp + 2) / 5 + 1) as u8;
    let m: u8 = if mp < 10 {
        (mp + 3) as u8
    } else {
        (mp - 9) as u8
    };
    let year: i32 = (y + if m <= 2 { 1 } else { 0 }) as i32;
    (year, m, d)
}

/// Computes the day of week (`0 = Sunday ..= 6 = Saturday`) from days since `1970-01-01`.
///
/// # Arguments
///
/// - `i64` - Days since `1970-01-01` (Thursday = 4).
///
/// # Returns
///
/// - `u8` - Weekday in `0..=6`.
#[inline(always)]
pub const fn weekday_from_days(days: i64) -> u8 {
    ((days + 4).rem_euclid(7)) as u8
}