Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterators for Ranges of Dates with day steps #5875

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions components/calendar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ name = "iso_date_manipulations"

[[example]]
name = "iso_datetime_manipulations"

[[example]]
name = "iso_date_range_iter"
46 changes: 46 additions & 0 deletions components/calendar/examples/iso_date_range_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 ).

// An example application which uses date range iteration.

#![no_main] // https://github.com/unicode-org/icu4x/issues/395
icu_benchmark_macros::instrument!();
use icu_benchmark_macros::println;

use icu_calendar::range::ToDateIter;
use icu_calendar::{Date, Iso};

fn print_date(date: Date<Iso>) {
let year = date.year().era_year_or_extended();
let mon = date.month().ordinal;
let day = date.day_of_month().0;
println!("Date: {day:02} {mon:02} {year}");
}

fn print_space() {
println!("");
}

fn main() {
let date_from = Date::try_new_iso(2024, 11, 29).unwrap();
let date_to = Date::try_new_iso(2025, 3, 31).unwrap();

// Let's print 10 dates from infinity date range:
let inf_range = date_from..;
inf_range.to_date_iter().take(10).for_each(print_date);

print_space();

// Let's print each 13th date from non-infinity range:
let range = date_from..date_to;
let iter = range.to_date_iter().step_by(13);
iter.for_each(print_date);

print_space();

// Let's print each 13th date from reversed non-infinity range:
let range_inclusive = date_from..=date_to;
let iter = range_inclusive.to_date_iter().rev().step_by(13);
iter.for_each(print_date);
}
2 changes: 1 addition & 1 deletion components/calendar/src/chinese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct Chinese {

/// The inner date type used for representing [`Date`]s of [`Chinese`]. See [`Date`] and [`Chinese`] for more details.
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct ChineseDateInner(ChineseBasedDateInner<Chinese>);
pub struct ChineseDateInner(pub(crate) ChineseBasedDateInner<Chinese>);

type Inner = ChineseBasedDateInner<Chinese>;

Expand Down
18 changes: 11 additions & 7 deletions components/calendar/src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,21 @@ impl Iso {
Date::try_new_iso(year, month, day).unwrap()
}
pub(crate) fn iso_from_fixed(date: RataDie) -> Date<Iso> {
let (year, month, day) = match calendrical_calculations::iso::iso_from_fixed(date) {
match Self::iso_try_from_fixed(date) {
Ok(date) => date,
Err(I32CastError::BelowMin) => {
return Date::from_raw(IsoDateInner(ArithmeticDate::min_date()), Iso)
Date::from_raw(IsoDateInner(ArithmeticDate::min_date()), Iso)
}
Err(I32CastError::AboveMax) => {
return Date::from_raw(IsoDateInner(ArithmeticDate::max_date()), Iso)
Date::from_raw(IsoDateInner(ArithmeticDate::max_date()), Iso)
}
Ok(ymd) => ymd,
};
#[allow(clippy::unwrap_used)] // valid day and month
Date::try_new_iso(year, month, day).unwrap()
}
}
pub(crate) fn iso_try_from_fixed(date: RataDie) -> Result<Date<Iso>, I32CastError> {
calendrical_calculations::iso::iso_from_fixed(date).map(|(year, month, day)| {
#[allow(clippy::unwrap_used)] // valid day and month
Date::try_new_iso(year, month, day).unwrap()
})
}

pub(crate) fn day_of_year(date: IsoDateInner) -> u16 {
Expand Down
3 changes: 3 additions & 0 deletions components/calendar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,6 @@ pub use gregorian::Gregorian;
#[doc(no_inline)]
pub use iso::Iso;
pub use types::Time;

/// Ranges for `Date` struct
pub mod range;
Loading
Loading