1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Time sources for stores whose behaviour depends on the current time.
//!
//! Spend ledgers read the time from a [`Clock`] to pick period windows and to expire
//! reservations. Production code uses [`SystemClock`]; tests and conformance suites
//! use [`ManualClock`] to cross a day or month boundary without waiting for it.
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use chrono::{DateTime, Utc};
/// Source of the current UTC time.
///
/// # Example
///
/// ```rust
/// use adk_core::{Clock, SystemClock};
///
/// let before = chrono::Utc::now();
/// assert!(SystemClock.now() >= before);
/// ```
pub trait Clock: fmt::Debug + Send + Sync {
/// Returns the current time.
fn now(&self) -> DateTime<Utc>;
}
/// The operating system's wall clock.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> DateTime<Utc> {
Utc::now()
}
}
/// A clock that reads a time the caller sets and moves only when told to.
///
/// Clones share the same time, so a test can hand one clone to a store and advance
/// another.
///
/// # Example
///
/// ```rust
/// use adk_core::{Clock, ManualClock};
/// use chrono::{TimeZone, Utc};
/// use std::time::Duration;
///
/// let clock = ManualClock::new(Utc.with_ymd_and_hms(2026, 10, 31, 23, 59, 0).unwrap());
/// clock.advance(Duration::from_secs(120));
/// assert_eq!(clock.now(), Utc.with_ymd_and_hms(2026, 11, 1, 0, 1, 0).unwrap());
/// ```
#[derive(Debug, Clone)]
pub struct ManualClock {
now: Arc<Mutex<DateTime<Utc>>>,
}
impl ManualClock {
/// Creates a clock that reads `at`.
pub fn new(at: DateTime<Utc>) -> Self {
Self { now: Arc::new(Mutex::new(at)) }
}
/// Sets the time the clock reads.
pub fn set(&self, at: DateTime<Utc>) {
*self.now.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = at;
}
/// Moves the clock forward by `by`.
pub fn advance(&self, by: Duration) {
let by = chrono::Duration::from_std(by).unwrap_or(chrono::Duration::MAX);
let mut now = self.now.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
*now = now.checked_add_signed(by).unwrap_or(DateTime::<Utc>::MAX_UTC);
}
}
impl Clock for ManualClock {
fn now(&self) -> DateTime<Utc> {
*self.now.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
}