create interface around the clock

This commit is contained in:
Cadey Ratio 2019-10-24 11:24:17 -04:00
parent 96d50ab941
commit 9014c1f167
3 changed files with 24 additions and 9 deletions

20
src/clock.rs Normal file
View File

@ -0,0 +1,20 @@
use chrono::prelude::*;
use cmos::{CMOSCenturyHandler, CMOS};
use core::convert::TryInto;
use lazy_static::lazy_static;
use spin::Mutex;
lazy_static! {
static ref CLOCK: Mutex<CMOS> = Mutex::new(unsafe { CMOS::new() });
}
pub fn current_time() -> DateTime<Utc> {
let rtc = CLOCK.lock().read_rtc(CMOSCenturyHandler::CurrentYear(2019));
return Utc
.ymd(
rtc.year.try_into().unwrap(),
rtc.month.into(),
rtc.day.into(),
)
.and_hms(rtc.hour.into(), rtc.minute.into(), rtc.second.into());
}

View File

@ -9,6 +9,7 @@
extern crate alloc;
pub mod allocator;
pub mod clock;
pub mod gdt;
pub mod interrupts;
pub mod memory;

View File

@ -6,6 +6,7 @@
extern crate alloc;
use alloc::string::ToString;
use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use xe_os::println;
@ -13,21 +14,14 @@ use xe_os::println;
entry_point!(kernel_main);
fn kernel_main(boot_info: &'static BootInfo) -> ! {
use cmos::{CMOSCenturyHandler, CMOS};
// Create a CMOS object (unsafe due to the use of port I/O)
let mut cmos = unsafe { CMOS::new() };
xe_os::init(&boot_info);
#[cfg(test)]
test_main();
// Read the rtc date time using this year
let rtc = cmos.read_rtc(CMOSCenturyHandler::CurrentYear(2019));
println!(
"current time: {}:{}:{} {} M{} {}",
rtc.hour, rtc.minute, rtc.second, rtc.year, rtc.month, rtc.day
);
let now = xe_os::clock::current_time();
println!("{}", now.to_string());
println!("Running WASM:");