icu_calendar 2.2.0

Date APIs for Gregorian and non-Gregorian calendars
Documentation
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{types::*, *};

fn check_continuity<A: AsCalendar + Clone>(mut date: Date<A>, years_to_check: usize) {
    let duration = DateDuration::for_days(1);

    let mut rata_die = date.to_rata_die();
    let mut weekday = date.weekday();
    let mut year = date.year();
    let mut is_in_leap_year = date.is_in_leap_year();

    for _ in 0..(366 * years_to_check) {
        let next_date = date
            .try_added_with_options(duration, Default::default())
            .unwrap();
        let next_rata_die = next_date.to_rata_die();
        assert_eq!(next_rata_die, rata_die + 1, "{next_date:?}");
        let next_weekday = next_date.weekday();
        let next_year = next_date.year();
        let next_is_in_leap_year = next_date.is_in_leap_year();
        assert_eq!(
            (next_weekday as usize) % 7,
            (weekday as usize + 1) % 7,
            "{next_date:?}"
        );
        if year == next_year {
            assert_eq!(is_in_leap_year, next_is_in_leap_year, "{next_date:?}");
        }
        date = next_date;
        rata_die = next_rata_die;
        weekday = next_weekday;
        year = next_year;
        is_in_leap_year = next_is_in_leap_year;
    }
}

fn check_every_250_days<C: Calendar + Copy>(mut date: Date<C>, iters: usize) {
    let duration = DateDuration::for_days(250);

    let mut rata_die = date.to_rata_die();

    for _ in 0..iters {
        let next_date = date
            .try_added_with_options(duration, Default::default())
            .unwrap();
        let next_rata_die = next_date.to_rata_die();
        assert_eq!(next_rata_die, rata_die + 250, "{next_date:?}");
        let next_date_roundtrip = Date::from_rata_die(next_rata_die, *next_date.calendar());
        assert_eq!(next_date, next_date_roundtrip, "{next_date:?}");
        date = next_date;
        rata_die = next_rata_die;
    }
}

#[test]
fn test_buddhist_continuity() {
    let date = Date::try_new_buddhist(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_buddhist(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_chinese_continuity() {
    let date = Date::try_new_chinese_traditional(-10, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_chinese_traditional(-300, Month::new(1), 1);
    check_every_250_days(date.unwrap(), 2000);
    let date = Date::try_new_chinese_traditional(-9999, Month::new(1), 1);
    check_every_250_days(date.unwrap(), 2000);

    let date = Date::try_new_chinese_traditional(1899, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);

    let date = Date::try_new_chinese_traditional(2099, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);
}

#[test]
fn test_coptic_continuity() {
    let date = Date::try_new_coptic(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_coptic(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_korean_continuity() {
    let date = Date::try_new_korean_traditional(-10, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_korean_traditional(-300, Month::new(1), 1);
    check_every_250_days(date.unwrap(), 2000);

    let date = Date::try_new_korean_traditional(1900, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);

    let date = Date::try_new_korean_traditional(2100, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);
}

#[test]
fn test_ethiopian_continuity() {
    use cal::EthiopianEraStyle::*;
    let date = Date::try_new_ethiopian(AmeteMihret, -10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_ethiopian(AmeteMihret, -300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_ethiopian_amete_alem_continuity() {
    use cal::EthiopianEraStyle::*;
    let date = Date::try_new_ethiopian(AmeteAlem, -10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_ethiopian(AmeteAlem, -300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_gregorian_continuity() {
    let date = Date::try_new_gregorian(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_gregorian(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_hebrew_continuity() {
    let date = Date::try_new_hebrew_v2(-10, Month::new(1), 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hebrew_v2(-300, Month::new(1), 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_indian_continuity() {
    let date = Date::try_new_indian(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_indian(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_hijri_civil_continuity() {
    let cal = cal::Hijri::new_tabular(
        cal::hijri::TabularAlgorithmLeapYears::TypeII,
        cal::hijri::TabularAlgorithmEpoch::Friday,
    );
    let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
#[allow(deprecated)]
fn test_hijri_simulated_mecca_continuity() {
    #[cfg(feature = "logging")]
    let _ = simple_logger::SimpleLogger::new().env().init();
    let cal = cal::Hijri::new_simulated_mecca();
    let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(1290, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(1590, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_hijri_tabular_continuity() {
    let cal = cal::Hijri::new_tabular(
        cal::hijri::TabularAlgorithmLeapYears::TypeII,
        cal::hijri::TabularAlgorithmEpoch::Thursday,
    );
    let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_hijri_umm_al_qura_continuity() {
    #[cfg(feature = "logging")]
    let _ = simple_logger::SimpleLogger::new().env().init();
    let cal = cal::Hijri::new_umm_al_qura();
    let date = Date::try_new_hijri_with_calendar(-10, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(1290, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(1590, 1, 1, cal);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_hijri_with_calendar(-300, 1, 1, cal);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_iso_continuity() {
    let date = Date::try_new_iso(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_iso(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_japanese_continuity() {
    let date = Date::try_new_japanese_with_calendar("heisei", 20, 1, 1, cal::Japanese::new());
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_japanese_with_calendar("bce", 500, 1, 1, cal::Japanese::new());
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_persian_continuity() {
    let date = Date::try_new_persian(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_persian(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}

#[test]
fn test_roc_continuity() {
    let date = Date::try_new_roc(-10, 1, 1);
    check_continuity(date.unwrap(), 20);
    let date = Date::try_new_roc(-300, 1, 1);
    check_every_250_days(date.unwrap(), 2000);
}