mprober_lib/rtc_time/
mod.rs

1use std::io::ErrorKind;
2
3use chrono::prelude::*;
4
5use crate::scanner_rust::{generic_array::typenum::U52, ScannerAscii, ScannerError};
6
7/// Get the RTC datetime by reading the `/proc/driver/rtc` file.
8///
9/// ```rust
10/// use mprober_lib::rtc_time;
11///
12/// let rtc_date_time = rtc_time::get_rtc_date_time().unwrap();
13///
14/// println!("{rtc_date_time}");
15/// ```
16#[inline]
17pub fn get_rtc_date_time() -> Result<NaiveDateTime, ScannerError> {
18    let mut sc: ScannerAscii<_, U52> = ScannerAscii::scan_path2("/proc/driver/rtc")?;
19
20    sc.drop_next_bytes("rtc_time".len())?.ok_or(ErrorKind::UnexpectedEof)?;
21    sc.drop_next_until(": ")?.ok_or(ErrorKind::UnexpectedEof)?;
22
23    let hour = sc.next_u32_until(":")?.ok_or(ErrorKind::UnexpectedEof)?;
24    let minute = sc.next_u32_until(":")?.ok_or(ErrorKind::UnexpectedEof)?;
25    let second = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
26
27    sc.drop_next_bytes("rtc_time".len())?.ok_or(ErrorKind::UnexpectedEof)?;
28    sc.drop_next_until(": ")?.ok_or(ErrorKind::UnexpectedEof)?;
29
30    let year = sc.next_i32_until("-")?.ok_or(ErrorKind::UnexpectedEof)?;
31    let month = sc.next_u32_until("-")?.ok_or(ErrorKind::UnexpectedEof)?;
32    let date = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
33
34    Ok(NaiveDateTime::new(
35        NaiveDate::from_ymd_opt(year, month, date).unwrap(),
36        NaiveTime::from_hms_opt(hour, minute, second).unwrap(),
37    ))
38}