Skip to content

Reject IsoDate when outside the allowed range #62

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

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,54 @@ mod tests {
let result = later.since(&earlier, None, None, None, None).unwrap();
assert_eq!(result.days(), 9719.0,);
}

// test262/test/built-ins/Temporal/Calendar/prototype/month/argument-string-invalid.js
#[test]
fn invalid_strings() {
const INVALID_STRINGS: [&str; 35] = [
// invalid ISO strings:
"",
"invalid iso8601",
"2020-01-00",
"2020-01-32",
"2020-02-30",
"2021-02-29",
"2020-00-01",
"2020-13-01",
"2020-01-01T",
"2020-01-01T25:00:00",
"2020-01-01T01:60:00",
"2020-01-01T01:60:61",
"2020-01-01junk",
"2020-01-01T00:00:00junk",
"2020-01-01T00:00:00+00:00junk",
"2020-01-01T00:00:00+00:00[UTC]junk",
"2020-01-01T00:00:00+00:00[UTC][u-ca=iso8601]junk",
"02020-01-01",
"2020-001-01",
"2020-01-001",
"2020-01-01T001",
"2020-01-01T01:001",
"2020-01-01T01:01:001",
// valid, but forms not supported in Temporal:
"2020-W01-1",
"2020-001",
"+0002020-01-01",
// valid, but this calendar must not exist:
"2020-01-01[u-ca=notexist]",
// may be valid in other contexts, but insufficient information for PlainDate:
"2020-01",
"+002020-01",
"01-01",
"2020-W01",
"P1Y",
"-P12Y",
// valid, but outside the supported range:
"-999999-01-01",
"+999999-01-01",
];
for s in INVALID_STRINGS {
assert!(Date::<()>::from_str(s).is_err())
}
}
}
14 changes: 11 additions & 3 deletions src/iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,30 @@ impl IsoDate {
day: i32,
overflow: ArithmeticOverflow,
) -> TemporalResult<Self> {
match overflow {
let id = match overflow {
ArithmeticOverflow::Constrain => {
let month = month.clamp(1, 12);
let days_in_month = utils::iso_days_in_month(year, month);
let d = day.clamp(1, days_in_month);
// NOTE: Values are clamped in a u8 range.
Ok(Self::new_unchecked(year, month as u8, d as u8))
Self::new_unchecked(year, month as u8, d as u8)
}
ArithmeticOverflow::Reject => {
if !is_valid_date(year, month, day) {
return Err(TemporalError::range().with_message("not a valid ISO date."));
}
// NOTE: Values have been verified to be in a u8 range.
Ok(Self::new_unchecked(year, month as u8, day as u8))
Self::new_unchecked(year, month as u8, day as u8)
}
};

if !iso_dt_within_valid_limits(id, &IsoTime::noon()) {
return Err(
TemporalError::range().with_message("Date is not within ISO date time limits.")
);
}

Ok(id)
}

/// Create a balanced `IsoDate`
Expand Down
2 changes: 1 addition & 1 deletion src/rounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<T: Roundable> IncrementRounder<T> {
pub(crate) fn from_positive_parts(number: T, increment: NonZeroU64) -> TemporalResult<Self> {
let increment = <T as NumCast>::from(increment.get()).temporal_unwrap()?;

debug_assert!(number > T::ZERO);
debug_assert!(number >= T::ZERO);

Ok(Self {
sign: true,
Expand Down