use chrono::prelude::*;
use jam_types::{Slot, JAM_COMMON_ERA};
pub fn slot_time(slot: Slot) -> String {
chrono::Local
.timestamp_opt(JAM_COMMON_ERA as i64 + slot as i64 * 6, 0)
.unwrap()
.format("%H:%M:%S")
.to_string()
}
pub fn slot(slot: Slot) -> String {
slot.to_string()
.as_bytes()
.rchunks(3)
.rev()
.map(std::str::from_utf8)
.collect::<Result<Vec<&str>, _>>()
.expect("converted from number; qed")
.join(",")
}
pub fn percent<T: Into<u128>>(n: T, d: T) -> String {
let ppm = (n.into() as f64 / d.into() as f64 * 100_000f64).ceil() / 1_000f64;
if ppm >= 1000.0 {
ppm.round()
} else if ppm >= 100.0 {
(ppm * 10f64).round() / 10f64
} else if ppm >= 10.0 {
(ppm * 100f64).round() / 100f64
} else {
ppm
}
.to_string()
}
pub fn bytes<T: Into<u128>>(n: T) -> String {
units(n, "B", 1024.0)
}
pub fn amount<T: Into<u128>>(n: T) -> String {
units(n, "", 1000.0)
}
pub fn gas<T: Into<u128>>(n: T) -> String {
units(n, "", 1000.0)
}
pub fn units<T: Into<u128>>(n: T, ones: &str, divisor: f64) -> String {
const UNITS: [&str; 7] = ["", "K", "M", "G", "T", "P", "E"];
let mut i = 0;
let mut n = n.into() as f64;
while n >= 999.5 && i > 0 || n >= 9999.5 {
n /= divisor;
i += 1;
}
let q = if n >= 100.0 {
1.0
} else if n >= 10.0 {
10.0
} else {
100.0
};
format!("{}{}", (n * q + 1.0e-14).round() / q, if i == 0 { ones } else { UNITS[i] })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn amount_works() {
assert_eq!(amount(0u32), "0");
assert_eq!(amount(999u32), "999");
assert_eq!(amount(9_999u32), "9999");
assert_eq!(amount(10_000u32), "10K");
assert_eq!(amount(10_049u32), "10K");
assert_eq!(amount(10_050u32), "10.1K");
assert_eq!(amount(100_499u32), "100K");
assert_eq!(amount(100_500u32), "101K");
assert_eq!(amount(1_004_999u32), "1M");
assert_eq!(amount(1_005_000u32), "1.01M");
assert_eq!(amount(10_049_999u32), "10M");
assert_eq!(amount(10_050_000u32), "10.1M");
}
}