1pub mod a8_micros {
5 pub const MICROS_PER_MILLIS: u64 = 1_000;
7}
8
9pub mod b2_time {
11 use chrono::{Timelike, NaiveTime};
12
13 pub trait TimeExt {
15 fn micros_of_day(&self) -> u64;
17
18 fn from_micros_day_unsafe(micros: u64) -> NaiveTime;
20
21 fn from_hmsi_friendly_unsafe(millis: u64) -> NaiveTime;
23
24 fn hour_minute(&self) -> u32;
25
26 fn to_string6(&self) -> String;
27
28 fn to_string3(&self) -> String;
29 }
30
31 impl TimeExt for NaiveTime {
32 fn micros_of_day(&self) -> u64 {
33 let hour = self.hour() as u64;
35 let minute = self.minute() as u64;
36 let second = self.second() as u64;
37 let nano = self.nanosecond() as u64; let micro = nano / 1000; (hour * 3600 + minute * 60 + second) * 1_000_000 + micro
41 }
42
43 fn from_micros_day_unsafe(micros: u64) -> NaiveTime {
44 let seconds = (micros / 1_000_000) as u32;
45 let micros_remainder = (micros % 1_000_000) as u32;
46 let nanos = micros_remainder * 1000; let hours = seconds / 3600;
49 let minutes = (seconds % 3600) / 60;
50 let seconds = seconds % 60;
51
52 NaiveTime::from_hms_nano_opt(hours, minutes, seconds, nanos).unwrap_or_default()
53 }
54
55 fn from_hmsi_friendly_unsafe(millis: u64) -> NaiveTime {
56 let micros = millis * crate::fasttime::a8_micros::MICROS_PER_MILLIS;
57 Self::from_micros_day_unsafe(micros)
58 }
59
60 fn hour_minute(&self) -> u32 {
61 self.hour() * 100 + self.minute()
62 }
63
64 fn to_string6(&self) -> String {
65 self.format("%H:%M:%S.%6f").to_string()
66 }
67
68 fn to_string3(&self) -> String {
69 self.format("%H:%M:%S.%3f").to_string()
70 }
71 }
72}