Skip to content

Commit 403b247

Browse files
pitdickerdjc
authored andcommitted
Change NaiveDate::from_of to take ordinal and year flags
1 parent 7d98475 commit 403b247

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/naive/date.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,35 @@ impl NaiveDate {
252252
pub(crate) fn weeks_from(&self, day: Weekday) -> i32 {
253253
(self.ordinal() as i32 - self.weekday().num_days_from(day) as i32 + 6) / 7
254254
}
255-
/// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
256-
fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
257-
if (MIN_YEAR..=MAX_YEAR).contains(&year) {
258-
let of = of.inner();
259-
Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
260-
} else {
261-
None
255+
256+
/// Makes a new `NaiveDate` from year, ordinal and flags.
257+
/// Does not check whether the flags are correct for the provided year.
258+
const fn from_ordinal_and_flags(
259+
year: i32,
260+
ordinal: u32,
261+
flags: YearFlags,
262+
) -> Option<NaiveDate> {
263+
if year < MIN_YEAR || year > MAX_YEAR {
264+
return None; // Out-of-range
265+
}
266+
// Enable debug check once the MSRV >= 1.57 (panicking in const feature)
267+
// debug_assert!(YearFlags::from_year(year).0 == flags.0);
268+
match Of::new(ordinal, flags) {
269+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
270+
None => None, // Invalid: Ordinal outside of the nr of days in a year with those flags.
262271
}
263272
}
264273

265-
/// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
266-
fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
267-
NaiveDate::from_of(year, mdf.to_of()?)
274+
/// Makes a new `NaiveDate` from year and packed month-day-flags.
275+
/// Does not check whether the flags are correct for the provided year.
276+
const fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
277+
if year < MIN_YEAR || year > MAX_YEAR {
278+
return None; // Out-of-range
279+
}
280+
match mdf.to_of() {
281+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
282+
None => None, // Non-existing date
283+
}
268284
}
269285

270286
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -335,7 +351,7 @@ impl NaiveDate {
335351
#[must_use]
336352
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
337353
let flags = YearFlags::from_year(year);
338-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
354+
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
339355
}
340356

341357
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -404,20 +420,21 @@ impl NaiveDate {
404420
if weekord <= delta {
405421
// ordinal < 1, previous year
406422
let prevflags = YearFlags::from_year(year - 1);
407-
NaiveDate::from_of(
423+
NaiveDate::from_ordinal_and_flags(
408424
year - 1,
409-
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
425+
weekord + prevflags.ndays() - delta,
426+
prevflags,
410427
)
411428
} else {
412429
let ordinal = weekord - delta;
413430
let ndays = flags.ndays();
414431
if ordinal <= ndays {
415432
// this year
416-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
433+
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
417434
} else {
418435
// ordinal > ndays, next year
419436
let nextflags = YearFlags::from_year(year + 1);
420-
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags)?)
437+
NaiveDate::from_ordinal_and_flags(year + 1, ordinal - ndays, nextflags)
421438
}
422439
}
423440
} else {
@@ -462,7 +479,7 @@ impl NaiveDate {
462479
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
463480
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
464481
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
465-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
482+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
466483
}
467484

468485
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1070,7 +1087,7 @@ impl NaiveDate {
10701087

10711088
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10721089
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1073-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1090+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10741091
}
10751092

10761093
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1102,7 +1119,7 @@ impl NaiveDate {
11021119

11031120
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
11041121
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1105-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1122+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
11061123
}
11071124

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

0 commit comments

Comments
 (0)