pub struct Event { /* private fields */ }
Expand description
Represents an event that happens at a given point in time and may span until an optional end datetime.
Single instant events can be created via Event::at
, while Event::new
and
Event::try_new
can be used to construct events with an explict end.
§Example
use jiff::{ToSpan, civil::date};
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::new(start, end);
assert_eq!(event.start(), start);
assert_eq!(event.end(), Some(end));
assert_eq!(event.duration().fieldwise(), 1.day());
Implementations§
Source§impl Event
impl Event
Sourcepub fn at(instant: DateTime) -> Event
pub fn at(instant: DateTime) -> Event
Creates a new Event
which starts and ends at instant
.
The event duration is effectively zero.
§Example
use jiff::civil::date;
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let event = Event::at(start);
Sourcepub fn new(start: DateTime, end: DateTime) -> Event
pub fn new(start: DateTime, end: DateTime) -> Event
Creates a new Event
which spans from a start
(inclusive) to an end
(exclusive).
The fallible version of this method is Event::try_new
.
§Panics
Panics if start >= end
.
§Example
use jiff::civil::date;
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::new(start, end);
Sourcepub fn try_new(start: DateTime, end: DateTime) -> Result<Event, Error>
pub fn try_new(start: DateTime, end: DateTime) -> Result<Event, Error>
Creates a new Event
which spans from a start
(inclusive) to an end
(exclusive).
The panicking version of this method is Event::new
.
§Errors
Returns and Error
if start >= end
.
§Example
use jiff::civil::date;
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::try_new(start, end)?;
Sourcepub fn start(&self) -> DateTime
pub fn start(&self) -> DateTime
Returns the DateTime
at which the event starts.
§Example
use jiff::civil::date;
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let event = Event::at(start);
assert_eq!(event.start(), start);
Sourcepub fn end(&self) -> Option<DateTime>
pub fn end(&self) -> Option<DateTime>
Returns the DateTime
at which the event ends if it has an end, None
otherwise.
§Example
use jiff::civil::date;
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let event = Event::at(start);
assert!(event.end().is_none());
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::new(start, end);
assert_eq!(event.end(), Some(end));
Sourcepub fn duration(&self) -> Span
pub fn duration(&self) -> Span
Returns the duration between the events’ start and end.
For events that don’t have an end, this always returns a zero Span
.
§Example
use jiff::{ToSpan, civil::date};
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let event = Event::at(start);
assert!(event.duration().is_zero());
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::new(start, end);
assert_eq!(event.duration().fieldwise(), 1.day());
Sourcepub fn contains(&self, instant: DateTime) -> bool
pub fn contains(&self, instant: DateTime) -> bool
Returns true
if instant
falls within the events’ duration, false
otherwise.
For events that don’t have an end, this is equivalent to event.start() == instant
.
§Example
use jiff::{ToSpan, civil::date};
use recurring::Event;
let start = date(2025, 1, 1).at(0, 0, 0, 0);
let event = Event::at(start);
assert!(!event.contains(start - 1.nanosecond()));
assert!(event.contains(start));
assert!(!event.contains(start + 1.nanosecond()));
let end = date(2025, 1, 2).at(0, 0, 0, 0);
let event = Event::new(start, end);
assert!(!event.contains(start - 1.nanosecond()));
assert!(event.contains(start));
assert!(event.contains(start + 1.nanosecond()));
assert!(event.contains(end - 1.nanosecond()));
assert!(!event.contains(end));
assert!(!event.contains(end + 1.nanosecond()));
Trait Implementations§
Source§impl Ord for Event
impl Ord for Event
Source§impl PartialOrd for Event
impl PartialOrd for Event
Source§impl ToSeries for Event
impl ToSeries for Event
Source§fn to_series<P: Pattern>(&self, pattern: P) -> Result<Series<P>, Error>
fn to_series<P: Pattern>(&self, pattern: P) -> Result<Series<P>, Error>
Converts an Event
to a Series
with the given recurrence Pattern
.
§Errors
Returns an error if the event duration cannot be represented as a Span
or
if the events’ start
is DateTime::MAX
.
§Example
use jiff::civil::date;
use recurring::{Event, ToSeries, pattern::hourly};
let date = date(2025, 1, 1);
let start = date.at(0, 0, 0, 0);
let end = date.at(0, 30, 0, 0);
let event = Event::new(start, end);
let series = event.to_series(hourly(2))?;
let mut events = series.iter();
assert_eq!(events.next(), Some(Event::new(date.at(0, 0, 0, 0), date.at(0, 30, 0, 0))));
assert_eq!(events.next(), Some(Event::new(date.at(2, 0, 0, 0), date.at(2, 30, 0, 0))));