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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::TSpan;
use chrono::{DateTime, Duration, Utc};
impl TSpan {
/// Creates a `TSpan` from a `chrono::Duration` / `TimeTSpan` (nanosecond precision).
///
/// This is the exact reverse of [`TSpan::to_chrono_duration`].
///
/// - The conversion is **lossless** when the chrono duration fits inside an `i64`
/// number of nanoseconds.
/// - Uses existing `from_ns` helper.
/// - If `num_nanoseconds()` returns `None` (the chrono value is outside the
/// range that chrono itself can represent as nanoseconds), we clamp to
/// **exactly** the maximum/minimum nanosecond value that chrono can store
/// (`i64::MAX` / `i64::MIN` nanoseconds) rather than saturating to
/// `TSpan::MAX` / `TSpan::MIN`.
pub fn from_chrono_duration(dur: Duration) -> Self {
match dur.num_nanoseconds() {
Some(ns) => Self::from_ns(ns as i128),
None => {
let ns = if dur > Duration::zero() {
i64::MAX
} else {
i64::MIN
};
Self::from_ns(ns as i128)
}
}
}
/// Creates a `TSpan` representing the duration since the Unix epoch
/// (1970-01-01 00:00:00 UTC) from a `chrono::DateTime<Utc>`.
///
/// This is the exact reverse of [`TSpan::to_chrono_datetime_utc`].
///
/// - Returns a `TSpan` whose value is the number of nanoseconds since the
/// Unix epoch (with sub-nanosecond attoseconds set to zero).
/// - Uses the safe `timestamp_nanos_opt()` API.
/// - If the DateTime is outside the nanosecond range chrono can represent,
/// we clamp to **exactly** the maximum/minimum nanosecond value that chrono
/// itself can store (`i64::MAX` / `i64::MIN` nanoseconds since epoch)
/// rather than saturating to `TSpan::MAX` / `TSpan::MIN`.
pub fn from_chrono_datetime_utc(dt: DateTime<Utc>) -> Self {
match dt.timestamp_nanos_opt() {
Some(ns) => Self::from_ns(ns as i128),
None => {
let ns = if dt > DateTime::<Utc>::UNIX_EPOCH {
i64::MAX
} else {
i64::MIN
};
Self::from_ns(ns as i128)
}
}
}
}