pub mod a8_micros {
pub const MICROS_PER_MILLIS: u64 = 1_000;
}
pub mod b2_time {
use chrono::{Timelike, NaiveTime};
pub trait TimeExt {
fn micros_of_day(&self) -> u64;
fn from_micros_day_unsafe(micros: u64) -> NaiveTime;
fn from_hmsi_friendly_unsafe(millis: u64) -> NaiveTime;
fn hour_minute(&self) -> u32;
fn to_string6(&self) -> String;
fn to_string3(&self) -> String;
}
impl TimeExt for NaiveTime {
fn micros_of_day(&self) -> u64 {
let hour = self.hour() as u64;
let minute = self.minute() as u64;
let second = self.second() as u64;
let nano = self.nanosecond() as u64; let micro = nano / 1000;
(hour * 3600 + minute * 60 + second) * 1_000_000 + micro
}
fn from_micros_day_unsafe(micros: u64) -> NaiveTime {
let seconds = (micros / 1_000_000) as u32;
let micros_remainder = (micros % 1_000_000) as u32;
let nanos = micros_remainder * 1000;
let hours = seconds / 3600;
let minutes = (seconds % 3600) / 60;
let seconds = seconds % 60;
NaiveTime::from_hms_nano_opt(hours, minutes, seconds, nanos).unwrap_or_default()
}
fn from_hmsi_friendly_unsafe(millis: u64) -> NaiveTime {
let micros = millis * crate::fasttime::a8_micros::MICROS_PER_MILLIS;
Self::from_micros_day_unsafe(micros)
}
fn hour_minute(&self) -> u32 {
self.hour() * 100 + self.minute()
}
fn to_string6(&self) -> String {
self.format("%H:%M:%S.%6f").to_string()
}
fn to_string3(&self) -> String {
self.format("%H:%M:%S.%3f").to_string()
}
}
}