Documentation
use std::{
    sync::{Arc, RwLock},
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

/// Duration since epoch at 2015-05-15T00:00:00 UTC.
///
/// This is the date of the [Rust 1.0 announcement][rust-announcement].
///
/// [rust-announcement]: https://blog.rust-lang.org/2015/05/15/Rust-1.0.html
const RUST1_EPOCH: Duration = Duration::from_secs(1431648000);

/// Fake clock.
#[derive(Debug, Clone)]
pub struct FakeClock(Arc<RwLock<SystemTime>>);

/// Create a new [`FakeClock`] with the time `2015-05-15T00:00:00 UTC`.
impl Default for FakeClock {
    fn default() -> Self {
        Self::from(RUST1_EPOCH)
    }
}

impl FakeClock {
    /// Returns the current time.
    pub fn now(&self) -> SystemTime {
        *self.0.read().unwrap()
    }

    /// Advance the clock by `duration`.
    pub fn advance(&self, duration: Duration) {
        let mut clock = self.0.write().unwrap();
        *clock += duration;
    }

    /// Unwind the clock by `duration`.
    pub fn unwind(&self, duration: Duration) {
        let mut clock = self.0.write().unwrap();
        *clock -= duration;
    }

    /// Set the clock to time `t`.
    pub fn set(&self, t: SystemTime) {
        let mut clock = self.0.write().unwrap();
        *clock = t;
    }
}

/// Create a new [`FakeClock`] at time since [`UNIX_EPOCH`].
impl From<Duration> for FakeClock {
    fn from(value: Duration) -> Self {
        Self::from(UNIX_EPOCH + value)
    }
}

/// Create a new [`FakeClock`] at [`SystemTime`].
impl From<SystemTime> for FakeClock {
    fn from(t: SystemTime) -> Self {
        Self(Arc::new(RwLock::new(t)))
    }
}

/// Fake monotonic clock.
#[derive(Debug, Clone)]
pub struct FakeMonotonicClock(Arc<RwLock<Instant>>);

impl Default for FakeMonotonicClock {
    fn default() -> Self {
        Self::from(Instant::now())
    }
}

impl FakeMonotonicClock {
    /// Returns the current time.
    pub fn now(&self) -> Instant {
        *self.0.read().unwrap()
    }

    /// Advance the clock by `duration`.
    pub fn advance(&self, duration: Duration) {
        let mut clock = self.0.write().unwrap();
        *clock += duration;
    }

    /// Set the clock to time `t`.
    pub fn set(&self, t: Instant) {
        let mut clock = self.0.write().unwrap();
        *clock = t;
    }
}

impl From<Instant> for FakeMonotonicClock {
    fn from(value: Instant) -> Self {
        Self(Arc::new(RwLock::new(value)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod simple {
        use super::*;

        #[test]
        fn from_system_time() {
            let now = SystemTime::now();
            let clock = FakeClock::from(now);
            assert_eq!(clock.now(), now);
        }

        #[test]
        fn from_duration() {
            let now = SystemTime::now();
            let since_epoch = now.duration_since(UNIX_EPOCH).unwrap();
            let clock = FakeClock::from(since_epoch);
            assert_eq!(clock.now(), now);
        }

        #[test]
        fn advance_secs() {
            let now = SystemTime::now();
            let clock = FakeClock::from(now);
            clock.advance(Duration::from_secs(1));
            assert_eq!(clock.now(), now + Duration::from_secs(1));
        }

        #[test]
        fn advance_nanos() {
            let now = SystemTime::now();
            let clock = FakeClock::from(now);
            clock.advance(Duration::from_nanos(1));
            assert_eq!(clock.now(), now + Duration::from_nanos(1));
        }

        #[test]
        fn unwind_secs() {
            let now = SystemTime::now();
            let clock = FakeClock::from(now);
            clock.unwind(Duration::from_secs(1));
            assert_eq!(clock.now(), now - Duration::from_secs(1));
        }

        #[test]
        fn unwind_nanos() {
            let now = SystemTime::now();
            let clock = FakeClock::from(now);
            clock.unwind(Duration::from_nanos(1));
            assert_eq!(clock.now(), now - Duration::from_nanos(1));
        }

        #[test]
        fn set_time() {
            let clock = FakeClock::default();
            let t = SystemTime::now();
            assert_ne!(clock.now(), t);

            clock.set(t);
            assert_eq!(clock.now(), t);
        }
    }

    mod monotonic {
        use super::*;

        #[test]
        fn from_instant() {
            let now = Instant::now();
            let clock = FakeMonotonicClock::from(now);
            assert_eq!(clock.now(), now);
        }

        #[test]
        fn advance_secs() {
            let now = Instant::now();
            let clock = FakeMonotonicClock::from(now);
            clock.advance(Duration::from_secs(1));
            assert_eq!(clock.now(), now + Duration::from_secs(1));
        }

        #[test]
        fn advance_nanos() {
            let now = Instant::now();
            let clock = FakeMonotonicClock::from(now);
            clock.advance(Duration::from_nanos(1));
            assert_eq!(clock.now(), now + Duration::from_nanos(1));
        }

        #[test]
        fn set_time() {
            let clock = FakeMonotonicClock::default();
            let t = Instant::now();
            assert_ne!(clock.now(), t);

            clock.set(t);
            assert_eq!(clock.now(), t);
        }
    }
}