From 3f95a45b276cfd7059263d2dd77f2558cdfc87b0 Mon Sep 17 00:00:00 2001 From: Roderick van Domburg Date: Tue, 25 Jan 2022 20:02:41 +0100 Subject: [PATCH] Parse dates without month or day (fixes #943) --- core/src/date.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/core/src/date.rs b/core/src/date.rs index d7cf09ef..3c78f265 100644 --- a/core/src/date.rs +++ b/core/src/date.rs @@ -53,11 +53,22 @@ impl Date { impl TryFrom<&DateMessage> for Date { type Error = crate::Error; fn try_from(msg: &DateMessage) -> Result { - let date = _Date::from_calendar_date( - msg.get_year(), - (msg.get_month() as u8).try_into()?, - msg.get_day() as u8, - )?; + // Some metadata contains a year, but no month. In that case just set January. + let month = if msg.has_month() { + msg.get_month() as u8 + } else { + 1 + }; + + // Having no day will work, but may be unexpected: it will imply the last day + // of the month before. So prevent that, and just set day 1. + let day = if msg.has_day() { + msg.get_day() as u8 + } else { + 1 + }; + + let date = _Date::from_calendar_date(msg.get_year(), month.try_into()?, day)?; let time = Time::from_hms(msg.get_hour() as u8, msg.get_minute() as u8, 0)?; Ok(Self::from_utc(PrimitiveDateTime::new(date, time))) }