mirror of
https://github.com/bspeice/dtparse
synced 2024-11-13 17:38:09 -05:00
Merge pull request #10 from messense/fix-issue-9
Use from_hms_micro_opt instead of from_hms_micro
This commit is contained in:
commit
8b0248e7bb
30
src/lib.rs
30
src/lib.rs
@ -74,13 +74,12 @@ impl From<ParseIntError> for ParseInternalError {
|
|||||||
pub enum ParseError {
|
pub enum ParseError {
|
||||||
AmbiguousWeekday,
|
AmbiguousWeekday,
|
||||||
InternalError(ParseInternalError),
|
InternalError(ParseInternalError),
|
||||||
InvalidDay,
|
|
||||||
InvalidMonth,
|
InvalidMonth,
|
||||||
UnrecognizedToken(String),
|
UnrecognizedToken(String),
|
||||||
InvalidParseResult(ParsingResult),
|
InvalidParseResult(ParsingResult),
|
||||||
AmPmWithoutHour,
|
AmPmWithoutHour,
|
||||||
InvalidHour,
|
|
||||||
TimezoneUnsupported,
|
TimezoneUnsupported,
|
||||||
|
ImpossibleTimestamp(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ParseInternalError> for ParseError {
|
impl From<ParseInternalError> for ParseError {
|
||||||
@ -774,7 +773,7 @@ impl Parser {
|
|||||||
if fuzzy {
|
if fuzzy {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::InvalidHour)
|
Err(ParseError::ImpossibleTimestamp("Invalid hour"))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
@ -806,13 +805,24 @@ impl Parser {
|
|||||||
|
|
||||||
let d = d + d_offset;
|
let d = d + d_offset;
|
||||||
|
|
||||||
let t = NaiveTime::from_hms_micro(
|
let hour = res.hour.unwrap_or(default.hour() as i32) as u32;
|
||||||
res.hour.unwrap_or(default.hour() as i32) as u32,
|
let minute = res.minute.unwrap_or(default.minute() as i32) as u32;
|
||||||
res.minute.unwrap_or(default.minute() as i32) as u32,
|
let second = res.second.unwrap_or(default.second() as i32) as u32;
|
||||||
res.second.unwrap_or(default.second() as i32) as u32,
|
let microsecond = res.microsecond
|
||||||
res.microsecond
|
.unwrap_or(default.timestamp_subsec_micros() as i32) as u32;
|
||||||
.unwrap_or(default.timestamp_subsec_micros() as i32) as u32,
|
let t = NaiveTime::from_hms_micro_opt(hour, minute, second, microsecond).ok_or_else(|| {
|
||||||
);
|
if hour >= 24 {
|
||||||
|
ParseError::ImpossibleTimestamp("Invalid hour")
|
||||||
|
} else if minute >= 60 {
|
||||||
|
ParseError::ImpossibleTimestamp("Invalid minute")
|
||||||
|
} else if second >= 60 {
|
||||||
|
ParseError::ImpossibleTimestamp("Invalid second")
|
||||||
|
} else if microsecond >= 2_000_000 {
|
||||||
|
ParseError::ImpossibleTimestamp("Invalid microsecond")
|
||||||
|
} else {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(NaiveDateTime::new(d, t))
|
Ok(NaiveDateTime::new(d, t))
|
||||||
}
|
}
|
||||||
|
@ -14,4 +14,6 @@ fn test_fuzz() {
|
|||||||
let mut p = Parser::default();
|
let mut p = Parser::default();
|
||||||
let res = p.parse("\x0D\x31", None, None, false, false, Some(&default), false, HashMap::new()).unwrap();
|
let res = p.parse("\x0D\x31", None, None, false, false, Some(&default), false, HashMap::new()).unwrap();
|
||||||
assert_eq!(res.0, default);
|
assert_eq!(res.0, default);
|
||||||
}
|
|
||||||
|
assert_eq!(parse("\x2D\x2D\x32\x31\x38\x6D"), Err(ParseError::ImpossibleTimestamp("Invalid minute")));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user