From 22319e7a221a457b212845c3875e98911d0d714e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:50:20 -0800 Subject: [PATCH] craft-time: Tick converts to and from std::time::Duration Tick::from_duration floors to the tick and saturates at i64::MAX ticks. Tick::to_duration rounds to the nearest nanosecond and returns None for a negative tick, so a Duration survives the round trip unchanged. --- crates/time/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs index 4a3af58dfd..2aabeadcea 100644 --- a/crates/time/src/lib.rs +++ b/crates/time/src/lib.rs @@ -14,6 +14,7 @@ use serde::{Deserialize, Serialize}; use std::fmt; use std::ops::{Add, AddAssign, Div, Mul, Neg, Rem, Sub, SubAssign}; +use std::time::Duration; /// SoundCraft's sample-index, tempo-map, grid, and display-time API, retained verbatim for /// session-format compatibility while sharing the broader Craft time primitives above. @@ -22,6 +23,8 @@ pub mod soundcraft; /// Clock ticks per second: 254 016 000 000. pub const TICKS_PER_SECOND: i64 = 254_016_000_000; +const NANOS_PER_SECOND: u64 = 1_000_000_000; + /// A point or duration in time, in ticks. /// Arithmetic saturates at storage limits; representable results remain exact. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] @@ -77,6 +80,23 @@ impl Tick { clamp_i64(if r * 2 >= unit { q + 1 } else { q }) } + /// A [`Duration`] as ticks, floored to the tick and saturating at `i64::MAX` ticks. + pub fn from_duration(d: Duration) -> Tick { + let secs = i128::from(d.as_secs()) * i128::from(TICKS_PER_SECOND); + let subsec = i128::from(d.subsec_nanos()) * i128::from(TICKS_PER_SECOND) / i128::from(NANOS_PER_SECOND); + Tick(clamp_i64(secs + subsec)) + } + + /// Ticks as a [`Duration`], rounded to the nearest nanosecond (halves up), or `None` for a + /// negative tick. `Tick::from_duration(d).to_duration()` returns `d` for every `d` up to + /// `i64::MAX` ticks (about 420 days). + pub fn to_duration(self) -> Option { + let ticks = u128::try_from(self.0).ok()?; + let tps = u128::from(TICKS_PER_SECOND.unsigned_abs()); + let nanos = (ticks * u128::from(NANOS_PER_SECOND) + tps / 2) / tps; + Some(Duration::from_nanos(u64::try_from(nanos).ok()?)) + } + pub fn abs(self) -> Tick { Tick(self.0.saturating_abs()) } @@ -822,6 +842,29 @@ mod tests { assert_eq!(Tick(TICKS_PER_SECOND).to_rational_round(1, 1), 1); } + #[test] + fn durations_round_trip_through_ticks() { + for nanos in [0, 1, 2, 3, 499, 500_000_001, 33_366_667, 1_000_000_000, 86_399_999_999_999] { + let d = Duration::from_nanos(nanos); + assert_eq!(Tick::from_duration(d).to_duration(), Some(d), "{nanos} ns"); + } + let near_limit = Duration::from_secs(400 * 86_400) + Duration::from_nanos(999_999_999); + assert_eq!(Tick::from_duration(near_limit).to_duration(), Some(near_limit)); + } + + #[test] + fn durations_floor_and_saturate() { + assert!(Tick::from_duration(Duration::from_nanos(1)) < Tick(TICKS_PER_SECOND / 1_000_000_000 + 1)); + assert!(Tick::from_duration(Duration::from_nanos(1)) > Tick::ZERO); + assert_eq!(Tick::from_duration(Duration::MAX), Tick(i64::MAX)); + assert!(Tick(i64::MAX).to_duration().is_some_and(|d| d > Duration::from_secs(400 * 86_400))); + assert_eq!(Tick(-1).to_duration(), None); + assert_eq!(Tick(i64::MIN).to_duration(), None); + // Half a nanosecond is 127.008 ticks. + assert_eq!(Tick(128).to_duration(), Some(Duration::from_nanos(1))); + assert_eq!(Tick(127).to_duration(), Some(Duration::ZERO)); + } + #[test] fn test_hostile_rates_never_panic() { let zero = FrameRate { num: 0, den: 0 };