Skip to content

Commit b354578

Browse files
committed
Change NaiveDate::from_of to take ordinal and year flags
1 parent 482317b commit b354578

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/naive/date.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,30 @@ impl NaiveDate {
241241
pub(crate) fn weeks_from(&self, day: Weekday) -> i32 {
242242
(self.ordinal() as i32 - self.weekday().num_days_from(day) as i32 + 6) / 7
243243
}
244-
/// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
245-
fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
246-
if (MIN_YEAR..=MAX_YEAR).contains(&year) {
247-
let of = of.inner();
248-
Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
249-
} else {
250-
None
244+
245+
/// Makes a new `NaiveDate` from year, ordinal and flags.
246+
/// Does not check whether the flags are correct for the provided year.
247+
const fn from_of(year: i32, ordinal: u32, flags: YearFlags) -> Option<NaiveDate> {
248+
if year < MIN_YEAR || year > MAX_YEAR {
249+
return None; // Out-of-range
250+
}
251+
debug_assert!(YearFlags::from_year(year).0 == flags.0);
252+
match Of::new(ordinal, flags) {
253+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
254+
None => None, // Invalid: Ordinal outside of the nr of days in a year with those flags.
251255
}
252256
}
253257

254-
/// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
255-
fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
256-
NaiveDate::from_of(year, mdf.to_of()?)
258+
/// Makes a new `NaiveDate` from year and packed month-day-flags.
259+
/// Does not check whether the flags are correct for the provided year.
260+
const fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
261+
if year < MIN_YEAR || year > MAX_YEAR {
262+
return None; // Out-of-range
263+
}
264+
match mdf.to_of() {
265+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
266+
None => None, // Non-existing date
267+
}
257268
}
258269

259270
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -324,7 +335,7 @@ impl NaiveDate {
324335
#[must_use]
325336
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
326337
let flags = YearFlags::from_year(year);
327-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
338+
NaiveDate::from_of(year, ordinal, flags)
328339
}
329340

330341
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -393,20 +404,17 @@ impl NaiveDate {
393404
if weekord <= delta {
394405
// ordinal < 1, previous year
395406
let prevflags = YearFlags::from_year(year - 1);
396-
NaiveDate::from_of(
397-
year - 1,
398-
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
399-
)
407+
NaiveDate::from_of(year - 1, weekord + prevflags.ndays() - delta, prevflags)
400408
} else {
401409
let ordinal = weekord - delta;
402410
let ndays = flags.ndays();
403411
if ordinal <= ndays {
404412
// this year
405-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
413+
NaiveDate::from_of(year, ordinal, flags)
406414
} else {
407415
// ordinal > ndays, next year
408416
let nextflags = YearFlags::from_year(year + 1);
409-
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags)?)
417+
NaiveDate::from_of(year + 1, ordinal - ndays, nextflags)
410418
}
411419
}
412420
} else {
@@ -451,7 +459,7 @@ impl NaiveDate {
451459
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
452460
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
453461
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
454-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
462+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
455463
}
456464

457465
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1037,7 +1045,7 @@ impl NaiveDate {
10371045

10381046
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10391047
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1040-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1048+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10411049
}
10421050

10431051
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1069,7 +1077,7 @@ impl NaiveDate {
10691077

10701078
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10711079
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1072-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1080+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10731081
}
10741082

10751083
/// Subtracts another `NaiveDate` from the current date.

0 commit comments

Comments
 (0)