Skip to content

Commit 4fe0f9b

Browse files
authored
Merge pull request #326 from robamu/update_chrono_api
2 parents b2cf8c8 + e3005da commit 4fe0f9b

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/rtc.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use rtcc::{DateTimeAccess, Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime,
1818
pub enum Error {
1919
/// Invalid input error
2020
InvalidInputData,
21+
/// Invalid register data, failed to convert to rtcc Type
22+
InvalidRtcData,
2123
}
2224

2325
/// Real Time Clock peripheral
@@ -178,13 +180,10 @@ impl DateTimeAccess for Rtc {
178180
let minutes = self.minutes()?;
179181
let hours = hours_to_u8(self.hours()?)?;
180182

181-
Ok(
182-
NaiveDate::from_ymd(year.into(), month.into(), day.into()).and_hms(
183-
hours.into(),
184-
minutes.into(),
185-
seconds.into(),
186-
),
187-
)
183+
NaiveDate::from_ymd_opt(year.into(), month.into(), day.into())
184+
.ok_or(Error::InvalidRtcData)?
185+
.and_hms_opt(hours.into(), minutes.into(), seconds.into())
186+
.ok_or(Error::InvalidRtcData)
188187
}
189188
}
190189

@@ -333,11 +332,8 @@ impl Rtcc for Rtc {
333332
let minutes = self.minutes()?;
334333
let hours = hours_to_u8(self.hours()?)?;
335334

336-
Ok(NaiveTime::from_hms(
337-
hours.into(),
338-
minutes.into(),
339-
seconds.into(),
340-
))
335+
NaiveTime::from_hms_opt(hours.into(), minutes.into(), seconds.into())
336+
.ok_or(Error::InvalidRtcData)
341337
}
342338

343339
fn weekday(&mut self) -> Result<u8, Self::Error> {
@@ -370,7 +366,7 @@ impl Rtcc for Rtc {
370366
let month = self.month()?;
371367
let year = self.year()?;
372368

373-
Ok(NaiveDate::from_ymd(year.into(), month.into(), day.into()))
369+
NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).ok_or(Error::InvalidRtcData)
374370
}
375371
}
376372

@@ -384,13 +380,13 @@ fn bcd2_encode(word: u32) -> Result<(u8, u8), Error> {
384380
let l = match (word / 10).try_into() {
385381
Ok(v) => v,
386382
Err(_) => {
387-
return Err(Error::InvalidInputData);
383+
return Err(Error::InvalidRtcData);
388384
}
389385
};
390386
let r = match (word % 10).try_into() {
391387
Ok(v) => v,
392388
Err(_) => {
393-
return Err(Error::InvalidInputData);
389+
return Err(Error::InvalidRtcData);
394390
}
395391
};
396392

0 commit comments

Comments
 (0)