update kilotime to be simpler + feature change

This commit is contained in:
Elfein Landers 2022-08-06 20:31:32 -07:00
parent 8c511e6550
commit 1ce64c6e35

View file

@ -3,56 +3,40 @@ use core::fmt::{Display, Error, Formatter};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(transparent)] #[repr(transparent)]
pub struct Kilosecond(usize); pub struct Kilosecond(u32);
impl Kilosecond { impl Kilosecond {
pub fn from_ms(ms: usize) -> Self { pub fn from_ms(ms: u32) -> Self {
Self(ms) Self(ms)
} }
pub fn from_sec(sec: usize) -> Self {
pub fn from_sec(sec: u32) -> Self {
Self(sec * 1000) Self(sec * 1000)
} }
pub fn from_minutes(min: usize) -> Self {
pub fn from_minutes(min: u32) -> Self {
Self(min * 60 * 1000) Self(min * 60 * 1000)
} }
pub fn from_hours(hrs: usize) -> Self {
pub fn from_hours(hrs: u32) -> Self {
Self(hrs * 60 * 60 * 1000) Self(hrs * 60 * 60 * 1000)
} }
pub fn from_days(days: usize) -> Self {
pub fn from_days(days: u32) -> Self {
Self(days * 24 * 60 * 60 * 1000) 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 { impl Display for Kilosecond {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
let mut reg = self.0; write![f, "{}K {}S", self.k(), self.s()]
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(())
} }
} }
@ -72,6 +56,6 @@ impl core::ops::Sub for Kilosecond {
impl From<Time> for Kilosecond { impl From<Time> for Kilosecond {
fn from(t: Time) -> Self { fn from(t: Time) -> Self {
Self((t.hour as usize * 3600 + t.minutes as usize * 60 + t.seconds as usize) * 1000) Self((t.hour as u32 * 3600 + t.minutes as u32 * 60 + t.seconds as u32) * 1000)
} }
} }