don't panic on invalid dates

This commit is contained in:
Robin Appelman 2026-11-15 14:24:16 +01:00
commit 9ab5d1903f
Signed by: robin
SSH key fingerprint: SHA256:kifqgrqnut7q1YUIrI0AdMNC23qiKAS/T9uGFmIYvbE
2 changed files with 15 additions and 11 deletions

View file

@ -36,7 +36,7 @@ pub trait EventHandler: Default {
start_time: &mut Option<NaiveDateTime>, start_time: &mut Option<NaiveDateTime>,
subjects: &mut SubjectMap<Self::PerSubjectData>, subjects: &mut SubjectMap<Self::PerSubjectData>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let event_time: NaiveDateTime = raw_event.date.try_into().unwrap(); let event_time: NaiveDateTime = raw_event.date.try_into()?;
let match_time = match start_time { let match_time = match start_time {
Some(start_time) => (event_time - *start_time).num_seconds() as u32, Some(start_time) => (event_time - *start_time).num_seconds() as u32,
None => { None => {

View file

@ -51,16 +51,20 @@ impl<'a> TryFrom<RawDate<'a>> for NaiveDateTime {
type Error = ParseIntError; type Error = ParseIntError;
fn try_from(RawDate(raw): RawDate<'a>) -> Result<Self, Self::Error> { fn try_from(RawDate(raw): RawDate<'a>) -> Result<Self, Self::Error> {
Ok( let month = raw[0..2].parse()?;
NaiveDate::from_ymd_opt(raw[6..10].parse()?, raw[0..2].parse()?, raw[3..5].parse()?) let day = raw[3..5].parse()?;
.unwrap() let year = raw[6..10].parse()?;
.and_hms_opt(
raw[13..15].parse()?, // one out of the 3.5 million+ logs, 1 randomly has DD/MM/YYYY instead of MM/DD/YYYY?
raw[16..18].parse()?,
raw[19..21].parse()?, Ok(NaiveDate::from_ymd_opt(year, month, day)
) .ok_or_else(|| "257".parse::<u8>().unwrap_err())?
.unwrap(), .and_hms_opt(
) raw[13..15].parse()?,
raw[16..18].parse()?,
raw[19..21].parse()?,
)
.unwrap())
} }
} }