1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::{Dt, clamp_i128_to_i64};
use chrono::{DateTime, Duration, TimeDelta, Utc};
impl Dt {
/// Converts this `Dt` to a `chrono::DateTime<chrono::Utc>`.
///
/// This is the main/default conversion method for absolute instants.
///
/// - The `Dt` is first converted to TAI internally (respecting all
/// scales, leap seconds, and relativistic models).
/// - The duration since the Unix epoch (1970-01-01 00:00:00 UTC) is then
/// computed.
/// - Sub-nanosecond attoseconds are truncated toward zero.
/// - Saturates at the minimum/maximum representable `DateTime<Utc>`
/// (roughly years 1678–2262) if the instant is out of range.
/// Never returns an error.
pub fn to_chrono_datetime_utc(&self) -> DateTime<Utc> {
let span_since_epoch = self.to_diff_raw(Dt::UNIX_EPOCH);
let total_nanos = span_since_epoch.to_attos() / 1_000_000_000i128;
let nanos = clamp_i128_to_i64(total_nanos);
DateTime::<Utc>::from_timestamp_nanos(nanos)
}
/// Converts this `Span` to a `chrono::Duration` (nanosecond precision).
///
/// - Sub-nanosecond attoseconds are **truncated toward zero**.
/// - The conversion is fully exact up to the nanosecond (128-bit integer arithmetic).
/// - **Saturates** at `chrono::Duration::MIN` / `chrono::Duration::MAX`
/// (roughly ±292 million years) if the value is out of range.
/// Never returns an error.
pub fn to_chrono_duration(&self) -> Duration {
let total_nanos = self.to_attos() / 1_000_000_000i128;
let nanos = clamp_i128_to_i64(total_nanos);
// `TimeDelta::nanoseconds` is infallible and returns exactly the
// `chrono::Duration` alias.
TimeDelta::nanoseconds(nanos).into()
}
}