mirror of
https://github.com/chronotope/chrono.git
synced 2025-09-28 05:21:39 +00:00
Check for overflow when parsing datetime
This commit is contained in:
parent
50eb7e363d
commit
ad03bcbdcb
@ -126,6 +126,7 @@ fn test_datetime_rfc2822_and_rfc3339() {
|
||||
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
|
||||
Ok(edt.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000))
|
||||
);
|
||||
assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
|
||||
assert_eq!(
|
||||
DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
|
||||
Ok(edt.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))
|
||||
|
@ -626,6 +626,12 @@ impl Parsed {
|
||||
let offset = self.offset.ok_or(NOT_ENOUGH)?;
|
||||
let datetime = self.to_naive_datetime_with_offset(offset)?;
|
||||
let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?;
|
||||
|
||||
// this is used to prevent an overflow when calling FixedOffset::from_local_datetime
|
||||
datetime
|
||||
.checked_sub_signed(OldDuration::seconds(i64::from(offset.local_minus_utc())))
|
||||
.ok_or(OUT_OF_RANGE)?;
|
||||
|
||||
match offset.from_local_datetime(&datetime) {
|
||||
LocalResult::None => Err(IMPOSSIBLE),
|
||||
LocalResult::Single(t) => Ok(t),
|
||||
|
Loading…
x
Reference in New Issue
Block a user