From 1ce64c6e3549eb9a5761735d9d94c04224e1841c Mon Sep 17 00:00:00 2001 From: Elfein Landers Date: Sat, 6 Aug 2022 20:31:32 -0700 Subject: [PATCH] update kilotime to be simpler + feature change --- ableos/src/relib/time/kilotime.rs | 100 +++++++++++++----------------- 1 file changed, 42 insertions(+), 58 deletions(-) diff --git a/ableos/src/relib/time/kilotime.rs b/ableos/src/relib/time/kilotime.rs index 7774a61..6aa9e02 100644 --- a/ableos/src/relib/time/kilotime.rs +++ b/ableos/src/relib/time/kilotime.rs @@ -3,75 +3,59 @@ use core::fmt::{Display, Error, Formatter}; #[derive(Debug, Clone, Copy)] #[repr(transparent)] -pub struct Kilosecond(usize); +pub struct Kilosecond(u32); impl Kilosecond { - pub fn from_ms(ms: usize) -> Self { - Self(ms) - } - pub fn from_sec(sec: usize) -> Self { - Self(sec * 1000) - } - pub fn from_minutes(min: usize) -> Self { - Self(min * 60 * 1000) - } - pub fn from_hours(hrs: usize) -> Self { - Self(hrs * 60 * 60 * 1000) - } - pub fn from_days(days: usize) -> Self { - Self(days * 24 * 60 * 60 * 1000) - } + pub fn from_ms(ms: u32) -> Self { + Self(ms) + } + + pub fn from_sec(sec: u32) -> Self { + Self(sec * 1000) + } + + pub fn from_minutes(min: u32) -> Self { + Self(min * 60 * 1000) + } + + pub fn from_hours(hrs: u32) -> Self { + Self(hrs * 60 * 60 * 1000) + } + + pub fn from_days(days: u32) -> Self { + Self(days * 24 * 60 * 60 * 1000) + } + + pub fn s(&self) -> u16 { + self.0 % 1000 + } + + pub fn k(&self) -> u32 { + self.0 / 1000 + } } impl Display for Kilosecond { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - let mut reg = self.0; - let mut buf = [0u8, 0, 0, 0, 0]; - let mut n = 0; - while n < 8 { - if n > 2 { - buf[7 - n] = match reg % 10 { - 0 => b'0', - 1 => b'1', - 2 => b'2', - 3 => b'3', - 4 => b'4', - 5 => b'5', - 6 => b'6', - 7 => b'7', - 8 => b'8', - 9 => b'9', - _ => unreachable!["CPU is borken"], - }; - } - reg /= 10; - n += 1; - } - for (n, b) in buf.iter().enumerate() { - write![f, "{}", *b as char]?; - if n == 1 { - write![f, "."]?; - } - } - Ok(()) - } + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + write![f, "{}K {}S", self.k(), self.s()] + } } impl core::ops::Add for Kilosecond { - type Output = Self; - fn add(self, rhs: Self) -> Self { - Self(self.0 + rhs.0) - } + type Output = Self; + fn add(self, rhs: Self) -> Self { + Self(self.0 + rhs.0) + } } impl core::ops::Sub for Kilosecond { - type Output = Self; - fn sub(self, rhs: Self) -> Self { - Self(self.0 - rhs.0) - } + type Output = Self; + fn sub(self, rhs: Self) -> Self { + Self(self.0 - rhs.0) + } } impl From