Skip to content

Commit 013427e

Browse files
committed
Change NaiveDate::from_of to take ordinal and year flags
1 parent 83b24c9 commit 013427e

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/naive/date.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,31 @@ impl NaiveDate {
242242
pub(crate) fn weeks_from(&self, day: Weekday) -> i32 {
243243
(self.ordinal() as i32 - self.weekday().num_days_from(day) as i32 + 6) / 7
244244
}
245-
/// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
246-
fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
247-
if (MIN_YEAR..=MAX_YEAR).contains(&year) {
248-
let of = of.inner();
249-
Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
250-
} else {
251-
None
245+
246+
/// Makes a new `NaiveDate` from year, ordinal and flags.
247+
/// Does not check whether the flags are correct for the provided year.
248+
const fn from_of(year: i32, ordinal: u32, flags: YearFlags) -> Option<NaiveDate> {
249+
if year < MIN_YEAR || year > MAX_YEAR {
250+
return None; // Out-of-range
251+
}
252+
// Enable debug check once the MSRV >= 1.57 (panicking in const feature)
253+
// debug_assert!(YearFlags::from_year(year).0 == flags.0);
254+
match Of::new(ordinal, flags) {
255+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
256+
None => None, // Invalid: Ordinal outside of the nr of days in a year with those flags.
252257
}
253258
}
254259

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

260272
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -325,7 +337,7 @@ impl NaiveDate {
325337
#[must_use]
326338
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
327339
let flags = YearFlags::from_year(year);
328-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
340+
NaiveDate::from_of(year, ordinal, flags)
329341
}
330342

331343
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -394,20 +406,17 @@ impl NaiveDate {
394406
if weekord <= delta {
395407
// ordinal < 1, previous year
396408
let prevflags = YearFlags::from_year(year - 1);
397-
NaiveDate::from_of(
398-
year - 1,
399-
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
400-
)
409+
NaiveDate::from_of(year - 1, weekord + prevflags.ndays() - delta, prevflags)
401410
} else {
402411
let ordinal = weekord - delta;
403412
let ndays = flags.ndays();
404413
if ordinal <= ndays {
405414
// this year
406-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
415+
NaiveDate::from_of(year, ordinal, flags)
407416
} else {
408417
// ordinal > ndays, next year
409418
let nextflags = YearFlags::from_year(year + 1);
410-
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags)?)
419+
NaiveDate::from_of(year + 1, ordinal - ndays, nextflags)
411420
}
412421
}
413422
} else {
@@ -452,7 +461,7 @@ impl NaiveDate {
452461
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
453462
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
454463
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
455-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
464+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
456465
}
457466

458467
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1038,7 +1047,7 @@ impl NaiveDate {
10381047

10391048
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10401049
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
1041-
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
1050+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10421051
}
10431052

10441053
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1070,7 +1079,7 @@ impl NaiveDate {
10701079

10711080
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10721081
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)?)
1082+
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10741083
}
10751084

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

0 commit comments

Comments
 (0)