Skip to content

Commit f27e2ff

Browse files
authored
Reject datetime when fraction digits are too large (#229)
We were unwrapping rather than rejecting on the new `to_nanosecond` method for `Fraction`. Context: `to_nanosecond` returns an `Option<u32>`, which represents the nanoseconds value when the digits are < 9, but returns `None` if the digits exceed 9. I think this mostly points towards a doc improvement needed in `ixdtf` that also links to `to_truncated_nanosecond` to show the difference between the two cases.
1 parent ed7c1af commit f27e2ff

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/iso.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,17 @@ impl IsoTime {
635635
/// Returns an `IsoTime` based off parse components.
636636
pub(crate) fn from_time_record(time_record: TimeRecord) -> TemporalResult<Self> {
637637
let second = time_record.second.clamp(0, 59);
638-
let (millisecond, rem) = time_record
638+
let fractional_seconds = time_record
639639
.fraction
640-
.and_then(|x| x.to_nanoseconds())
641-
.map(|x| x.div_rem_euclid(&1_000_000))
642-
.unwrap_or((0, 0));
640+
.map(|x| {
641+
x.to_nanoseconds().ok_or(
642+
TemporalError::range().with_message("fractional seconds exceeds nine digits."),
643+
)
644+
})
645+
.transpose()?
646+
.unwrap_or(0);
647+
648+
let (millisecond, rem) = fractional_seconds.div_rem_euclid(&1_000_000);
643649
let (micros, nanos) = rem.div_rem_euclid(&1_000);
644650

645651
Self::new(

0 commit comments

Comments
 (0)