| Crates.io | caryatid_module_clock |
| lib.rs | caryatid_module_clock |
| version | 0.12.0 |
| created_at | 2025-02-07 17:03:27.737104+00 |
| updated_at | 2025-06-18 15:01:10.528465+00 |
| description | Clock module for Caryatid |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1547166 |
| size | 49,544 |
The Clock module provides a regular tick event which can be used to drive time-based behaviour in other modules without having to create your own interval system.
The Clock module doesn't need any configuration, it just needs to be mentioned in the top-level configuration:
[module.clock]
The Clock module sends a ClockTickMessage once a second, which is defined in the common
messages in the SDK:
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct ClockTickMessage {
/// Time of tick, UTC
pub time: DateTime<Utc>,
/// Tick number
pub number: u64
}
The time is a DateTime in UTC, derived from a regular interval, which means although each tick may not be precisely one second after
the previous one, it won't drift over time, and the long-term average is exactly once per second. The number increments from zero at
startup each tick, and is a handy way to derive longer intervals with % - e.g.
if message.number % 60 == 0 {
// ... happens once a minute ...
}
The Clock module needs to be parameterised with the type of an outer message enum which contains a ClockTickMessage variant, and
provides a From implementation to promote one. For example, your system-wide message enum might be:
use caryatid_sdk::messages::ClockTickMessage;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Message {
None(()),
// ... other messages ...
Clock(ClockTickMessage),
}
impl From<ClockTickMessage> for Message {
fn from(msg: ClockTickMessage) -> Self {
Message::Clock(msg)
}
}
Then within your main.rs you would register the Clock module into the process like this:
Clock::<Message>::register(&mut process);
See the typed example to see this in action.