Files
sqm-autorate-rust/src/time.rs
T
Nils Andreas Svee 4aaf8fa100 chore: add SPDX license identifiers and copyright headers to source files
Apply the MPL-2.0 license to all source files and include the full license text to ensure legal compliance and proper attribution.
2026-04-23 12:39:52 +07:00

47 lines
1.0 KiB
Rust

// SPDX-FileCopyrightText: 2022-Present Nils Andreas Svee mailto:contact@lochnair.net (github @Lochnair)
//
// SPDX-License-Identifier: MPL-2.0
use rustix::fs::Timespec;
use rustix::thread::ClockId;
use rustix::time::clock_gettime;
pub struct Time {
time_s: u64,
time_ns: u64,
}
impl Time {
pub fn new(id: ClockId) -> Self {
let time: Timespec = clock_gettime(id);
Self {
time_s: time.tv_sec as u64,
time_ns: time.tv_nsec as u64,
}
}
pub fn get_time_since_midnight(&self) -> i64 {
(self.time_s as i64 % 86400 * 1000) + (self.time_ns as i64 / 1000000)
}
pub fn secs(&self) -> u64 {
self.time_s
}
pub fn nsecs(&self) -> u64 {
self.time_ns
}
pub fn as_nanos(&self) -> u64 {
self.time_s * 1_000_000_000 + self.time_ns
}
pub fn as_secs_f64(&self) -> f64 {
self.time_s as f64 + self.time_ns as f64 / 1_000_000_000.0
}
pub fn to_milliseconds(&self) -> u64 {
(self.time_s * 1000) + (self.time_ns / 1000000)
}
}