Clock

Trait Clock 

Source
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§

Source

fn timestamp_ns(&self) -> UnixNanos

Returns the current UNIX timestamp in nanoseconds (ns).

Source

fn timestamp_us(&self) -> u64

Returns the current UNIX timestamp in microseconds (μs).

Source

fn timestamp_ms(&self) -> u64

Returns the current UNIX timestamp in milliseconds (ms).

Source

fn timestamp(&self) -> f64

Returns the current UNIX timestamp in seconds.

Source

fn timer_names(&self) -> Vec<&str>

Returns the names of active timers in the clock.

Source

fn timer_count(&self) -> usize

Returns the count of active timers in the clock.

Source

fn timer_exists(&self, name: &Ustr) -> bool

If a timer with the name exists.

Source

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.

Source

fn get_handler(&self, event: TimeEvent) -> TimeEventHandlerV2

Get handler for TimeEvent.

Note: Panics if the event does not have an associated handler

Source

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_pastBehavior
trueIf alert time is in the past, the alert fires immediately; otherwise at alert time.
falseReturns 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.

Source

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 or Some(0): Uses the current time as start time.
  • Some(non_zero): Uses the specified timestamp as start time.
§Flags
allow_pastfire_immediatelyBehavior
truetrueFirst event fires immediately at start time, even if start time is in the past.
truefalseFirst event fires at start time + interval, even if start time is in the past.
falsetrueReturns error if start time is in the past (first event would be immediate but past).
falsefalseReturns 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.

Source

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.

Source

fn cancel_timer(&mut self, name: &str)

Cancels the timer with name.

Source

fn cancel_timers(&mut self)

Cancels all timers.

Source

fn reset(&mut self)

Resets the clock by clearing it’s internal state.

Provided Methods§

Source

fn utc_now(&self) -> DateTime<Utc>

Returns the current date and time as a timezone-aware DateTime<UTC>.

Source

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.

Source

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.

Implementors§