Merge 'Add helper to convert io::clock::Instant to useable format ' from Preston Thorpe

I needed this for another PR

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #2654
This commit is contained in:
Preston Thorpe
2025-08-19 08:18:52 -04:00
committed by GitHub

View File

@@ -1,9 +1,31 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant {
pub secs: i64,
pub micros: u32,
}
impl Instant {
pub fn to_system_time(self) -> SystemTime {
if self.secs >= 0 {
UNIX_EPOCH + Duration::new(self.secs as u64, self.micros * 1000)
} else {
let positive_secs = (-self.secs) as u64;
if self.micros > 0 {
// We have partial seconds that reduce the negative offset
// Need to borrow 1 second and subtract the remainder
let nanos_to_subtract = (1_000_000 - self.micros) * 1000;
UNIX_EPOCH - Duration::new(positive_secs - 1, nanos_to_subtract)
} else {
// Exactly N seconds before epoch
UNIX_EPOCH - Duration::new(positive_secs, 0)
}
}
}
}
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Instant {
fn from(value: chrono::DateTime<T>) -> Self {
Instant {