Skip to main content

cqlite_core/platform/
time.rs

1//! Time utilities
2
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5/// Time provider
6#[derive(Debug)]
7pub struct TimeProvider;
8
9impl Default for TimeProvider {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15impl TimeProvider {
16    /// Create a new time provider
17    pub fn new() -> Self {
18        Self
19    }
20
21    /// Get current time as microseconds since epoch
22    pub fn now_micros(&self) -> u64 {
23        SystemTime::now()
24            .duration_since(UNIX_EPOCH)
25            .unwrap_or_else(|_| Duration::from_secs(0))
26            .as_micros() as u64
27    }
28
29    /// Get current time as milliseconds since epoch
30    pub fn now_millis(&self) -> u64 {
31        SystemTime::now()
32            .duration_since(UNIX_EPOCH)
33            .unwrap_or_else(|_| Duration::from_secs(0))
34            .as_millis() as u64
35    }
36
37    /// Get current time as seconds since epoch
38    pub fn now_secs(&self) -> u64 {
39        SystemTime::now()
40            .duration_since(UNIX_EPOCH)
41            .unwrap_or_else(|_| Duration::from_secs(0))
42            .as_secs()
43    }
44
45    /// Get current system time
46    pub fn now(&self) -> SystemTime {
47        SystemTime::now()
48    }
49
50    /// Create duration from milliseconds
51    pub fn duration_from_millis(&self, millis: u64) -> Duration {
52        Duration::from_millis(millis)
53    }
54
55    /// Create duration from seconds
56    pub fn duration_from_secs(&self, secs: u64) -> Duration {
57        Duration::from_secs(secs)
58    }
59}