mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Parse dates without month or day (fixes #943)
This commit is contained in:
parent
2c63ef111a
commit
3f95a45b27
1 changed files with 16 additions and 5 deletions
|
@ -53,11 +53,22 @@ impl Date {
|
||||||
impl TryFrom<&DateMessage> for Date {
|
impl TryFrom<&DateMessage> for Date {
|
||||||
type Error = crate::Error;
|
type Error = crate::Error;
|
||||||
fn try_from(msg: &DateMessage) -> Result<Self, Self::Error> {
|
fn try_from(msg: &DateMessage) -> Result<Self, Self::Error> {
|
||||||
let date = _Date::from_calendar_date(
|
// Some metadata contains a year, but no month. In that case just set January.
|
||||||
msg.get_year(),
|
let month = if msg.has_month() {
|
||||||
(msg.get_month() as u8).try_into()?,
|
msg.get_month() as u8
|
||||||
msg.get_day() 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)?;
|
let time = Time::from_hms(msg.get_hour() as u8, msg.get_minute() as u8, 0)?;
|
||||||
Ok(Self::from_utc(PrimitiveDateTime::new(date, time)))
|
Ok(Self::from_utc(PrimitiveDateTime::new(date, time)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue