Properly handle invalid times

This commit is contained in:
syvb 2023-11-06 17:20:41 -05:00
parent ba0e79ecbd
commit ab0a0a8029
2 changed files with 7 additions and 2 deletions

View file

@ -11,7 +11,7 @@ exclude = [
] ]
[dependencies] [dependencies]
chrono = { version = "0.4.19", features = ["std", "serde"], default-features = false } chrono = { version = "0.4.31", features = ["std", "serde"], default-features = false }
serde = { version = "1.0.126", features = ["derive"] } serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64" serde_json = "1.0.64"
lazy_static = "1.4.0" lazy_static = "1.4.0"

View file

@ -542,6 +542,8 @@ pub enum EntityError {
ExpectedPidString, ExpectedPidString,
/// A mainsnak is missing /// A mainsnak is missing
MissingMainsnak, MissingMainsnak,
/// An hour/minute/second is out of bounds.
OutOfBoundsTime,
} }
fn get_json_string(json: Value) -> Result<String, EntityError> { fn get_json_string(json: Value) -> Result<String, EntityError> {
@ -622,6 +624,7 @@ fn parse_wb_time(time: &str) -> Result<chrono::DateTime<chrono::offset::Utc>, En
}, },
None => None, None => None,
}; };
#[allow(deprecated)] // TODO: avoid using ymd_opt here
let maybe_date = Utc.ymd_opt(year, month.unwrap_or(1), day.unwrap_or(1)); let maybe_date = Utc.ymd_opt(year, month.unwrap_or(1), day.unwrap_or(1));
let date = match maybe_date { let date = match maybe_date {
chrono::offset::LocalResult::Single(date) => date, chrono::offset::LocalResult::Single(date) => date,
@ -650,7 +653,9 @@ fn parse_wb_time(time: &str) -> Result<chrono::DateTime<chrono::offset::Utc>, En
} else { } else {
(0, 0, 0) (0, 0, 0)
}; };
Ok(date.and_hms(hour, min, sec)) Ok(date
.and_hms_opt(hour, min, sec)
.ok_or(EntityError::OutOfBoundsTime)?)
} }
impl ClaimValueData { impl ClaimValueData {