rstime 0.1.0

A zero-dependency Rust time library providing date, time, datetime types with formatting, parsing, Unix timestamps, and clock functionality.
Documentation
//! Clock module
//!
//! Provides `Clock` trait and implementations for system time and
//! monotonic timing.

use crate::date::Date;
use crate::datetime::DateTime;
use crate::duration::Duration;
use crate::time::Time;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

/// Clock trait for obtaining current time
///
/// Implementations include [`SystemClock`] for wall-clock time
/// and [`MonotonicClock`] for elapsed timing.
pub trait Clock {
    /// Current date and time
    fn now(&self) -> DateTime;
    /// Today's date
    fn today(&self) -> Date;
    /// Current time of day
    fn time_now(&self) -> Time;
}

/// System clock using `std::time::SystemTime`
///
/// Returns the current wall-clock time based on the system clock.
///
/// # Examples
///
/// ```rust
/// use rstime::{SystemClock, Clock};
///
/// let clock = SystemClock;
/// let now = clock.now();
/// println!("Current time: {}", now);
/// ```
pub struct SystemClock;

impl Clock for SystemClock {
    fn now(&self) -> DateTime {
        let elapsed = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(std::time::Duration::ZERO);
        DateTime::from_unix_millis(elapsed.as_millis() as i64)
    }

    fn today(&self) -> Date {
        self.now().date
    }

    fn time_now(&self) -> Time {
        self.now().time
    }
}

/// Monotonic clock for measuring elapsed time
///
/// Uses `std::time::Instant` and is not subject to system clock adjustments.
/// Ideal for performance measurement and timing.
///
/// # Examples
///
/// ```rust
/// use rstime::MonotonicClock;
///
/// let clock = MonotonicClock::start();
/// let elapsed = clock.elapsed();
/// println!("Elapsed: {}", elapsed);
/// ```
pub struct MonotonicClock {
    start: Instant,
}

impl MonotonicClock {
    /// Create a new monotonic clock starting now
    pub fn new() -> Self {
        MonotonicClock {
            start: Instant::now(),
        }
    }

    /// Start measuring time (alias for `new`)
    pub fn start() -> Self {
        MonotonicClock::new()
    }

    /// Time elapsed since the clock was started or last reset
    pub fn elapsed(&self) -> Duration {
        let elapsed = self.start.elapsed();
        Duration {
            secs: elapsed.as_secs() as i64,
            nanos: elapsed.subsec_nanos() as i32,
        }
    }

    /// Reset the clock to the current moment
    pub fn reset(&mut self) {
        self.start = Instant::now();
    }
}

impl DateTime {
    /// Convenience method: current date and time from the system clock
    pub fn now() -> Self {
        SystemClock.now()
    }
}

impl Date {
    /// Convenience method: today's date from the system clock
    pub fn today() -> Self {
        SystemClock.today()
    }
}

impl Time {
    /// Convenience method: current time from the system clock
    pub fn now() -> Self {
        SystemClock.time_now()
    }
}