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

Simplify from_isoywd a bit #1464

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 21 additions & 25 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,33 +314,29 @@ impl NaiveDate {
pub const fn from_isoywd_opt(year: i32, week: u32, weekday: Weekday) -> Option<NaiveDate> {
let flags = YearFlags::from_year(year);
let nweeks = flags.nisoweeks();
if 1 <= week && week <= nweeks {
// ordinal = week ordinal - delta
let weekord = week * 7 + weekday as u32;
let delta = flags.isoweek_delta();
if weekord <= delta {
// ordinal < 1, previous year
let prevflags = YearFlags::from_year(year - 1);
NaiveDate::from_ordinal_and_flags(
year - 1,
weekord + prevflags.ndays() - delta,
prevflags,
)
if week == 0 || week > nweeks {
return None;
}
// ordinal = week ordinal - delta
let weekord = week * 7 + weekday as u32;
let delta = flags.isoweek_delta();
let (year, ordinal, flags) = if weekord <= delta {
// ordinal < 1, previous year
let prevflags = YearFlags::from_year(year - 1);
(year - 1, weekord + prevflags.ndays() - delta, prevflags)
} else {
let ordinal = weekord - delta;
let ndays = flags.ndays();
if ordinal <= ndays {
// this year
(year, ordinal, flags)
} else {
let ordinal = weekord - delta;
let ndays = flags.ndays();
if ordinal <= ndays {
// this year
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
} else {
// ordinal > ndays, next year
let nextflags = YearFlags::from_year(year + 1);
NaiveDate::from_ordinal_and_flags(year + 1, ordinal - ndays, nextflags)
}
// ordinal > ndays, next year
let nextflags = YearFlags::from_year(year + 1);
(year + 1, ordinal - ndays, nextflags)
}
} else {
None
}
};
NaiveDate::from_ordinal_and_flags(year, ordinal, flags)
}

/// Makes a new `NaiveDate` from a day's number in the proleptic Gregorian calendar, with
Expand Down