diff --git a/src/platform/no_std/time.rs b/src/platform/no_std/time.rs index d12ad259..dbe72bc2 100644 --- a/src/platform/no_std/time.rs +++ b/src/platform/no_std/time.rs @@ -53,14 +53,6 @@ impl Instant { } } -impl Sub for Instant { - type Output = Instant; - - fn sub(self, rhs: Duration) -> Instant { - Self(self.0 - rhs) - } -} - impl Sub for Instant { type Output = Duration; diff --git a/src/platform/normal/mod.rs b/src/platform/normal/mod.rs index d8c6d5c6..fe6f2f48 100644 --- a/src/platform/normal/mod.rs +++ b/src/platform/normal/mod.rs @@ -127,15 +127,6 @@ cfg_if::cfg_if! { } } - impl Sub for Instant { - type Output = Instant; - - #[inline] - fn sub(self, rhs: Duration) -> Instant { - Self(self.0 - rhs) - } - } - impl Sub for Instant { type Output = Duration; @@ -171,15 +162,6 @@ cfg_if::cfg_if! { } } - impl Sub for Instant { - type Output = Instant; - - #[inline] - fn sub(self, rhs: Duration) -> Instant { - Self((self.0 as i64 - i64::try_from(rhs.whole_nanoseconds()).unwrap()) as u64) - } - } - impl Sub for Instant { type Output = Duration; @@ -203,15 +185,6 @@ cfg_if::cfg_if! { } } - impl Sub for Instant { - type Output = Instant; - - #[inline] - fn sub(self, rhs: Duration) -> Instant { - Self(time::ext::InstantExt::sub_signed(self.0, rhs)) - } - } - impl Sub for Instant { type Output = Duration; diff --git a/src/platform/wasm/unknown/time.rs b/src/platform/wasm/unknown/time.rs index 983304c1..3437dc7d 100644 --- a/src/platform/wasm/unknown/time.rs +++ b/src/platform/wasm/unknown/time.rs @@ -41,14 +41,6 @@ impl Instant { } } -impl Sub for Instant { - type Output = Instant; - - fn sub(self, rhs: Duration) -> Instant { - Self(self.0 - rhs) - } -} - impl Sub for Instant { type Output = Duration; diff --git a/src/platform/wasm/web/time.rs b/src/platform/wasm/web/time.rs index ddeac2fb..71fd8fc2 100644 --- a/src/platform/wasm/web/time.rs +++ b/src/platform/wasm/web/time.rs @@ -81,14 +81,6 @@ impl Instant { } } -impl Sub for Instant { - type Output = Instant; - - fn sub(self, rhs: Duration) -> Instant { - Self(self.0 - rhs) - } -} - impl Sub for Instant { type Output = Duration; diff --git a/src/timing/time_stamp.rs b/src/timing/time_stamp.rs index 8774e94d..f0665177 100644 --- a/src/timing/time_stamp.rs +++ b/src/timing/time_stamp.rs @@ -1,7 +1,4 @@ -use crate::{ - platform::{Duration, Instant}, - TimeSpan, -}; +use crate::{platform::Instant, TimeSpan}; use core::ops::Sub; /// A `TimeStamp` stores a point in time that can be used to calculate a @@ -26,12 +23,3 @@ impl Sub for TimeStamp { TimeSpan::from(self.0 - rhs.0) } } - -impl Sub for TimeStamp { - type Output = TimeStamp; - - #[inline] - fn sub(self, rhs: TimeSpan) -> TimeStamp { - TimeStamp(self.0 - Duration::from(rhs)) - } -} diff --git a/src/timing/timer/active_attempt.rs b/src/timing/timer/active_attempt.rs index f88f9384..7909fc95 100644 --- a/src/timing/timer/active_attempt.rs +++ b/src/timing/timer/active_attempt.rs @@ -6,11 +6,14 @@ use crate::{ #[derive(Debug, Clone)] pub struct ActiveAttempt { pub state: State, + /// The date time when the attempt started. pub attempt_started: AtomicDateTime, + /// The time stamp when the attempt started. pub start_time: TimeStamp, - pub start_time_with_offset: TimeStamp, - // This gets adjusted after resuming - pub adjusted_start_time: TimeStamp, + /// The original offset gets kept around to undo the pauses. + pub original_offset: TimeSpan, + /// The adjusted offset gets modified as pauses get accumulated. + pub adjusted_offset: TimeSpan, pub game_time_paused_at: Option, pub loading_times: Option, } @@ -54,9 +57,8 @@ impl ActiveAttempt { game_time, }; } - State::NotEnded { time_paused_at, .. } => { - time_paused_at.unwrap_or_else(|| TimeStamp::now() - self.adjusted_start_time) - } + State::NotEnded { time_paused_at, .. } => time_paused_at + .unwrap_or_else(|| TimeStamp::now() - self.start_time + self.adjusted_offset), }; let game_time = self @@ -75,11 +77,11 @@ impl ActiveAttempt { .. } = self.state { - return Some(TimeStamp::now() - self.start_time_with_offset - pause_time); + return Some(TimeStamp::now() - self.start_time + self.original_offset - pause_time); } - if self.start_time_with_offset != self.adjusted_start_time { - Some(self.start_time_with_offset - self.adjusted_start_time) + if self.original_offset != self.adjusted_offset { + Some(self.original_offset - self.adjusted_offset) } else { None } @@ -105,7 +107,7 @@ impl ActiveAttempt { return Err(Error::TimerPaused); } - let real_time = TimeStamp::now() - self.adjusted_start_time; + let real_time = TimeStamp::now() - self.start_time + self.adjusted_offset; if real_time < TimeSpan::zero() { return Err(Error::NegativeTime); diff --git a/src/timing/timer/mod.rs b/src/timing/timer/mod.rs index a05c0ecc..4a0e0954 100644 --- a/src/timing/timer/mod.rs +++ b/src/timing/timer/mod.rs @@ -282,7 +282,7 @@ impl Timer { if self.active_attempt.is_none() { let attempt_started = AtomicDateTime::now(); let start_time = TimeStamp::now(); - let start_time_with_offset = start_time - self.run.offset(); + let offset = self.run.offset(); self.active_attempt = Some(ActiveAttempt { state: State::NotEnded { @@ -291,8 +291,8 @@ impl Timer { }, attempt_started, start_time, - start_time_with_offset, - adjusted_start_time: start_time_with_offset, + original_offset: offset, + adjusted_offset: offset, game_time_paused_at: None, loading_times: None, }); @@ -497,7 +497,8 @@ impl Timer { }; if time_paused_at.is_none() { - *time_paused_at = Some(TimeStamp::now() - active_attempt.adjusted_start_time); + *time_paused_at = + Some(TimeStamp::now() - active_attempt.start_time + active_attempt.adjusted_offset); Ok(Event::Paused) } else { Err(Error::AlreadyPaused) @@ -513,7 +514,8 @@ impl Timer { }; if let Some(pause_time) = *time_paused_at { - active_attempt.adjusted_start_time = TimeStamp::now() - pause_time; + active_attempt.adjusted_offset = + pause_time - (TimeStamp::now() - active_attempt.start_time); *time_paused_at = None; Ok(Event::Resumed) } else { @@ -579,7 +581,7 @@ impl Timer { }; if let Some(active_attempt) = &mut self.active_attempt { - active_attempt.adjusted_start_time = active_attempt.start_time_with_offset; + active_attempt.adjusted_offset = active_attempt.original_offset; Ok(event) } else { Err(Error::NoRunInProgress) diff --git a/tests/rendering.rs b/tests/rendering.rs index ad5f42cd..2519efe4 100644 --- a/tests/rendering.rs +++ b/tests/rendering.rs @@ -180,8 +180,8 @@ fn actual_split_file() { check( &layout.state(&mut image_cache, &timer.snapshot()), &image_cache, - "42b2292f5c884c17", - "1e4c66359bdc32e8", + "c1e9e757c15a35ae", + "4fe65a630b531c54", "actual_split_file", ); } @@ -228,8 +228,8 @@ fn timer_delta_background() { &layout.state(&mut image_cache, &timer.snapshot()), &image_cache, [250, 300], - "e96525c84aa77f66", - "bd8139d0f625af38", + "748fa26a41a8d5a3", + "8bae1351d0dd52d7", "timer_delta_background_stopped", ); } diff --git a/tests/run_files/livesplit1.0.lss b/tests/run_files/livesplit1.0.lss index be2f1172..a8627a72 100644 --- a/tests/run_files/livesplit1.0.lss +++ b/tests/run_files/livesplit1.0.lss @@ -3,7 +3,7 @@ Pac-Man World 3 Any% - 00:00:00 + 12:34:56 3