Skip to content

Commit

Permalink
Migrate deprecated NaiveDate functions
Browse files Browse the repository at this point in the history
and_hms() -> and_hms_opt()
from_ymd() -> from_ymd_opt()
  • Loading branch information
jcard0na committed Nov 24, 2022
1 parent f883570 commit 0ede113
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion examples/rtc_low_apb1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ fn main() -> ! {
let mut led = gpiob.pb5.into_push_pull_output();

// Initialize RTC
let init = NaiveDate::from_ymd(2019, 8, 9).and_hms(13, 37, 42);
let init = NaiveDate::from_ymd_opt(2019, 8, 9)
.unwrap()
.and_hms(13, 37, 42)
.unwrap();
// If the target hardware has an external crystal, ClockSource::LSE can be used
// instead of ClockSource::LSI for greater accuracy
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, ClockSource::LSI, Some(init)).unwrap();
Expand Down
12 changes: 10 additions & 2 deletions src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ impl Rtc {

// Initialize RTC, if not yet initialized
if rtc.rtc.isr.read().inits().bit_is_clear() {
rtc.set(init.unwrap_or_else(|| NaiveDate::from_ymd(2001, 1, 1).and_hms(0, 0, 0)))?;
rtc.set(init.unwrap_or_else(|| {
NaiveDate::from_ymd_opt(2001, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
}))?;
}

// Disable wakeup timer. It's periodic and persists over resets, but for
Expand Down Expand Up @@ -315,7 +320,10 @@ impl Rtc {
let minute = bcd2_decode(tr.mnt().bits(), tr.mnu().bits());
let second = bcd2_decode(tr.st().bits(), tr.su().bits());

NaiveDate::from_ymd(year, month, day).and_hms(hour, minute, second)
NaiveDate::from_ymd_opt(year, month, day)
.unwrap()
.and_hms_opt(hour, minute, second)
.unwrap()
}

/// Enable interrupts
Expand Down

0 comments on commit 0ede113

Please sign in to comment.