Skip to content

Commit

Permalink
Use string instead of float as clock device output format
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 30, 2024
1 parent 0b749c4 commit 5708d48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/sys/clk/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::timer;

use crate::api::fs::{FileIO, IO};

use alloc::format;

#[derive(Debug, Clone)]
pub struct BootTime;

Expand All @@ -11,16 +13,18 @@ impl BootTime {
}

pub fn size() -> usize {
core::mem::size_of::<f64>()
// Must be larger than 8 bytes to be readable as a block device
// Format: "<seconds>.<nanoseconds>" => at least 20 + 1 + 6 bytes
32
}
}

impl FileIO for BootTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = boot_time().to_be_bytes();
let time = format!("{:.6}", boot_time());
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
buf[0..n].clone_from_slice(time.as_bytes());
Ok(n)
} else {
Err(())
Expand Down
10 changes: 7 additions & 3 deletions src/sys/clk/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::timer;

use crate::api::fs::{FileIO, IO};

use alloc::format;

const DAYS_BEFORE_MONTH: [u64; 13] = [
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
];
Expand All @@ -16,16 +18,18 @@ impl EpochTime {
}

pub fn size() -> usize {
core::mem::size_of::<f64>()
// Must be larger than 8 bytes to be readable as a block device
// Format: "<seconds>.<nanoseconds>" => at least 20 + 1 + 6 bytes
32
}
}

impl FileIO for EpochTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = epoch_time().to_be_bytes();
let time = format!("{:.6}", epoch_time());
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
buf[0..n].clone_from_slice(time.as_bytes());
Ok(n)
} else {
Err(())
Expand Down

0 comments on commit 5708d48

Please sign in to comment.