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,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<Time> for Kilosecond {
fn from(t: Time) -> Self {
Self((t.hour as usize * 3600 + t.minutes as usize * 60 + t.seconds as usize) * 1000)
}
fn from(t: Time) -> Self {
Self((t.hour as u32 * 3600 + t.minutes as u32 * 60 + t.seconds as u32) * 1000)
}
}