Skip to main content

oa/core/
duration.rs

1//! Signed nanosecond-precision duration value.
2//!
3//! Donor: `oa::Duration` (`oacpp/source/cpp/include/oa/core/std/chrono.h`).
4//! Unlike [`std::time::Duration`] this value is signed; animation curves and
5//! other sparse timeline surfaces admit negative sample times.
6
7/// Signed nanosecond-precision duration.
8///
9/// Stores `i64` nanoseconds. Checked constructors return `None` on overflow
10/// instead of the donor's contract abort.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct Duration {
13	nanoseconds: i64,
14}
15
16/// `2^63` as `f64`; the largest magnitude a nanosecond count may reach before
17/// it can no longer convert to `i64`.
18const I64_RANGE_F64: f64 = 9_223_372_036_854_776e18;
19
20impl Duration {
21	/// Zero duration.
22	pub const ZERO: Self = Self { nanoseconds: 0 };
23
24	/// Construct from a signed nanosecond count.
25	pub const fn from_nanoseconds(nanoseconds: i64) -> Self {
26		Self { nanoseconds }
27	}
28
29	/// Construct from whole seconds, or `None` on overflow.
30	pub const fn from_seconds(seconds: i64) -> Option<Self> {
31		match seconds.checked_mul(1_000_000_000) {
32			Some(nanoseconds) => Some(Self { nanoseconds }),
33			None => None,
34		}
35	}
36
37	/// Construct from whole milliseconds, or `None` on overflow.
38	pub const fn from_milliseconds(milliseconds: i64) -> Option<Self> {
39		match milliseconds.checked_mul(1_000_000) {
40			Some(nanoseconds) => Some(Self { nanoseconds }),
41			None => None,
42		}
43	}
44
45	/// Construct from whole microseconds, or `None` on overflow.
46	pub const fn from_microseconds(microseconds: i64) -> Option<Self> {
47		match microseconds.checked_mul(1_000) {
48			Some(nanoseconds) => Some(Self { nanoseconds }),
49			None => None,
50		}
51	}
52
53	/// Construct from fractional seconds, or `None` when the value is not
54	/// finite or cannot be represented in `i64` nanoseconds.
55	pub fn from_seconds_f64(seconds: f64) -> Option<Self> {
56		let nanoseconds = seconds * 1.0e9;
57		if !nanoseconds.is_finite() || nanoseconds <= -I64_RANGE_F64 || nanoseconds >= I64_RANGE_F64 {
58			return None;
59		}
60		Some(Self {
61			nanoseconds: nanoseconds as i64,
62		})
63	}
64
65	/// Signed nanosecond count.
66	pub const fn nanoseconds(self) -> i64 {
67		self.nanoseconds
68	}
69
70	/// Truncating microsecond count.
71	pub const fn microseconds(self) -> i64 {
72		self.nanoseconds / 1_000
73	}
74
75	/// Truncating millisecond count.
76	pub const fn milliseconds(self) -> i64 {
77		self.nanoseconds / 1_000_000
78	}
79
80	/// Duration as fractional seconds.
81	pub fn to_seconds(self) -> f64 {
82		self.nanoseconds as f64 / 1.0e9
83	}
84
85	/// Duration as fractional milliseconds.
86	pub fn to_milliseconds(self) -> f64 {
87		self.nanoseconds as f64 / 1.0e6
88	}
89
90	/// Duration as fractional microseconds.
91	pub fn to_microseconds(self) -> f64 {
92		self.nanoseconds as f64 / 1.0e3
93	}
94
95	/// Checked addition, or `None` on overflow.
96	pub const fn checked_add(self, other: Self) -> Option<Self> {
97		match self.nanoseconds.checked_add(other.nanoseconds) {
98			Some(nanoseconds) => Some(Self { nanoseconds }),
99			None => None,
100		}
101	}
102
103	/// Checked subtraction, or `None` on overflow.
104	pub const fn checked_sub(self, other: Self) -> Option<Self> {
105		match self.nanoseconds.checked_sub(other.nanoseconds) {
106			Some(nanoseconds) => Some(Self { nanoseconds }),
107			None => None,
108		}
109	}
110}