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