pub trait Clock: Debug {
Show 18 methods
// Required methods
fn timestamp_ns(&self) -> UnixNanos;
fn timestamp_us(&self) -> u64;
fn timestamp_ms(&self) -> u64;
fn timestamp(&self) -> f64;
fn timer_names(&self) -> Vec<&str>;
fn timer_count(&self) -> usize;
fn timer_exists(&self, name: &Ustr) -> bool;
fn register_default_handler(&mut self, callback: TimeEventCallback);
fn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2;
fn set_time_alert_ns(
&mut self,
name: &str,
alert_time_ns: UnixNanos,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
) -> Result<()>;
fn set_timer_ns(
&mut self,
name: &str,
interval_ns: u64,
start_time_ns: Option<UnixNanos>,
stop_time_ns: Option<UnixNanos>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
fire_immediately: Option<bool>,
) -> Result<()>;
fn next_time_ns(&self, name: &str) -> Option<UnixNanos>;
fn cancel_timer(&mut self, name: &str);
fn cancel_timers(&mut self);
fn reset(&mut self);
// Provided methods
fn utc_now(&self) -> DateTime<Utc> { ... }
fn set_time_alert(
&mut self,
name: &str,
alert_time: DateTime<Utc>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
) -> Result<()> { ... }
fn set_timer(
&mut self,
name: &str,
interval: Duration,
start_time: Option<DateTime<Utc>>,
stop_time: Option<DateTime<Utc>>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
fire_immediately: Option<bool>,
) -> Result<()> { ... }
}
Expand description
Represents a type of clock.
§Notes
An active timer is one which has not expired (timer.is_expired == False
).
Required Methods§
Sourcefn timestamp_ns(&self) -> UnixNanos
fn timestamp_ns(&self) -> UnixNanos
Returns the current UNIX timestamp in nanoseconds (ns).
Sourcefn timestamp_us(&self) -> u64
fn timestamp_us(&self) -> u64
Returns the current UNIX timestamp in microseconds (μs).
Sourcefn timestamp_ms(&self) -> u64
fn timestamp_ms(&self) -> u64
Returns the current UNIX timestamp in milliseconds (ms).
Sourcefn timer_names(&self) -> Vec<&str>
fn timer_names(&self) -> Vec<&str>
Returns the names of active timers in the clock.
Sourcefn timer_count(&self) -> usize
fn timer_count(&self) -> usize
Returns the count of active timers in the clock.
Sourcefn timer_exists(&self, name: &Ustr) -> bool
fn timer_exists(&self, name: &Ustr) -> bool
If a timer with the name
exists.
Sourcefn register_default_handler(&mut self, callback: TimeEventCallback)
fn register_default_handler(&mut self, callback: TimeEventCallback)
Register a default event handler for the clock. If a timer does not have an event handler, then this handler is used.
Sourcefn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2
fn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2
Get handler for TimeEvent
.
Note: Panics if the event does not have an associated handler
Sourcefn set_time_alert_ns(
&mut self,
name: &str,
alert_time_ns: UnixNanos,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
) -> Result<()>
fn set_time_alert_ns( &mut self, name: &str, alert_time_ns: UnixNanos, callback: Option<TimeEventCallback>, allow_past: Option<bool>, ) -> Result<()>
Set a timer to alert at the specified time.
Any existing timer registered under the same name
is cancelled with a warning before the new alert is scheduled.
§Flags
allow_past | Behavior |
---|---|
true | If alert time is in the past, the alert fires immediately; otherwise at alert time. |
false | Returns an error if alert time is earlier than now. |
§Callback
callback
: Some, then callback handles the time event.callback
: None, then the clock’s default time event callback is used.
§Errors
Returns an error if name
is invalid, alert_time_ns
is earlier than now when not allowed,
or any predicate check fails.
Sourcefn set_timer_ns(
&mut self,
name: &str,
interval_ns: u64,
start_time_ns: Option<UnixNanos>,
stop_time_ns: Option<UnixNanos>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
fire_immediately: Option<bool>,
) -> Result<()>
fn set_timer_ns( &mut self, name: &str, interval_ns: u64, start_time_ns: Option<UnixNanos>, stop_time_ns: Option<UnixNanos>, callback: Option<TimeEventCallback>, allow_past: Option<bool>, fire_immediately: Option<bool>, ) -> Result<()>
Set a timer to fire time events at every interval between start and stop time.
Any existing timer registered under the same name
is cancelled before the new timer is scheduled.
§Start Time
None
orSome(0)
: Uses the current time as start time.Some(non_zero)
: Uses the specified timestamp as start time.
§Flags
allow_past | fire_immediately | Behavior |
---|---|---|
true | true | First event fires immediately at start time, even if start time is in the past. |
true | false | First event fires at start time + interval, even if start time is in the past. |
false | true | Returns error if start time is in the past (first event would be immediate but past). |
false | false | Returns error if start time + interval is in the past. |
§Callback
callback
: Some, then callback handles the time event.callback
: None, then the clock’s default time event callback is used.
§Errors
Returns an error if name
is invalid, interval_ns
is not positive,
or if any predicate check fails.
Sourcefn next_time_ns(&self, name: &str) -> Option<UnixNanos>
fn next_time_ns(&self, name: &str) -> Option<UnixNanos>
Returns the time interval in which the timer name
is triggered.
If the timer doesn’t exist None
is returned.
Sourcefn cancel_timer(&mut self, name: &str)
fn cancel_timer(&mut self, name: &str)
Cancels the timer with name
.
Sourcefn cancel_timers(&mut self)
fn cancel_timers(&mut self)
Cancels all timers.
Provided Methods§
Sourcefn utc_now(&self) -> DateTime<Utc>
fn utc_now(&self) -> DateTime<Utc>
Returns the current date and time as a timezone-aware DateTime<UTC>
.
Sourcefn set_time_alert(
&mut self,
name: &str,
alert_time: DateTime<Utc>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
) -> Result<()>
fn set_time_alert( &mut self, name: &str, alert_time: DateTime<Utc>, callback: Option<TimeEventCallback>, allow_past: Option<bool>, ) -> Result<()>
Set a timer to alert at the specified time.
See Clock::set_time_alert_ns
for flag semantics.
§Callback
callback
: Some, then callback handles the time event.callback
: None, then the clock’s default time event callback is used.
§Errors
Returns an error if name
is invalid, alert_time
is in the past when not allowed,
or any predicate check fails.
Sourcefn set_timer(
&mut self,
name: &str,
interval: Duration,
start_time: Option<DateTime<Utc>>,
stop_time: Option<DateTime<Utc>>,
callback: Option<TimeEventCallback>,
allow_past: Option<bool>,
fire_immediately: Option<bool>,
) -> Result<()>
fn set_timer( &mut self, name: &str, interval: Duration, start_time: Option<DateTime<Utc>>, stop_time: Option<DateTime<Utc>>, callback: Option<TimeEventCallback>, allow_past: Option<bool>, fire_immediately: Option<bool>, ) -> Result<()>
Set a timer to fire time events at every interval between start and stop time.
Any existing timer registered under the same name
is cancelled with a warning before the new timer is scheduled.
See Clock::set_timer_ns
for flag semantics.
§Callback
callback
: Some, then callback handles the time event.callback
: None, then the clock’s default time event callback is used.
§Errors
Returns an error if name
is invalid, interval
is not positive,
or if any predicate check fails.