Skip to content

Commit d2e88b2

Browse files
committed
Change NaiveDate::from_of to take ordinal and year flags
1 parent cc22196 commit d2e88b2

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/naive/date.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,35 @@ 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_ordinal_and_flags(
248+
year: i32,
249+
ordinal: u32,
250+
flags: YearFlags,
251+
) -> Option<NaiveDate> {
252+
if year < MIN_YEAR || year > MAX_YEAR {
253+
return None; // Out-of-range
254+
}
255+
// Enable debug check once the MSRV >= 1.57 (panicking in const feature)
256+
// debug_assert!(YearFlags::from_year(year).0 == flags.0);
257+
match Of::new(ordinal, flags) {
258+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
259+
None => None, // Invalid: Ordinal outside of the nr of days in a year with those flags.
251260
}
252261
}
253262

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()?)
263+
/// Makes a new `NaiveDate` from year and packed month-day-flags.
264+
/// Does not check whether the flags are correct for the provided year.
265+
const fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
266+
if year < MIN_YEAR || year > MAX_YEAR {
267+
return None; // Out-of-range
268+
}
269+
match mdf.to_of() {
270+
Some(of) => Some(NaiveDate { ymdf: (year << 13) | (of.inner() as DateImpl) }),
271+
None => None, // Non-existing date
272+
}
257273
}
258274

259275
/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
@@ -324,7 +340,7 @@ impl NaiveDate {
324340
#[must_use]
325341
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
326342
let flags = YearFlags::from_year(year);
327-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
343+
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
328344
}
329345

330346
/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
@@ -393,20 +409,21 @@ impl NaiveDate {
393409
if weekord <= delta {
394410
// ordinal < 1, previous year
395411
let prevflags = YearFlags::from_year(year - 1);
396-
NaiveDate::from_of(
412+
NaiveDate::from_ordinal_and_flags(
397413
year - 1,
398-
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
414+
weekord + prevflags.ndays() - delta,
415+
prevflags,
399416
)
400417
} else {
401418
let ordinal = weekord - delta;
402419
let ndays = flags.ndays();
403420
if ordinal <= ndays {
404421
// this year
405-
NaiveDate::from_of(year, Of::new(ordinal, flags)?)
422+
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
406423
} else {
407424
// ordinal > ndays, next year
408425
let nextflags = YearFlags::from_year(year + 1);
409-
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags)?)
426+
NaiveDate::from_ordinal_and_flags(year + 1, ordinal - ndays, nextflags)
410427
}
411428
}
412429
} else {
@@ -451,7 +468,7 @@ impl NaiveDate {
451468
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
452469
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
453470
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)?)
471+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
455472
}
456473

457474
/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
@@ -1037,7 +1054,7 @@ impl NaiveDate {
10371054

10381055
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10391056
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)?)
1057+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10411058
}
10421059

10431060
/// Subtracts the `days` part of given `Duration` from the current date.
@@ -1069,7 +1086,7 @@ impl NaiveDate {
10691086

10701087
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
10711088
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)?)
1089+
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags)
10731090
}
10741091

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

0 commit comments

Comments
 (0)