Skip to main content

DateTimeRange

Struct DateTimeRange 

Source
pub struct DateTimeRange { /* private fields */ }
Expand description

A closed interval of instants, start <= end, bounds inclusive.

Equality and hashing follow DateTime: two ranges are equal when their bounds are the same instants, whatever offsets they carry.

Implementations§

Source§

impl DateTimeRange

Source

pub fn new(start: DateTime, end: DateTime) -> Result<Self, DateTimeError>

Creates the range from start to end, both included.

start == end is allowed and yields a single-instant range.

§Errors

Returns DateTimeError::InvalidRange if start is later than end.

§Examples
use dtt::datetime::DateTime;
use dtt::error::DateTimeError;
use dtt::range::DateTimeRange;

let a = DateTime::parse("2024-01-01T00:00:00Z")?;
let b = DateTime::parse("2024-01-02T00:00:00Z")?;
assert!(DateTimeRange::new(a, b).is_ok());
assert_eq!(DateTimeRange::new(b, a), Err(DateTimeError::InvalidRange));
Source

pub const fn start(&self) -> DateTime

Returns the first instant of the range.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let a = DateTime::parse("2024-01-01T00:00:00Z")?;
let r = DateTimeRange::new(a, a)?;
assert_eq!(r.start(), a);
Source

pub const fn end(&self) -> DateTime

Returns the last instant of the range.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let a = DateTime::parse("2024-01-01T00:00:00Z")?;
let r = DateTimeRange::new(a, a)?;
assert_eq!(r.end(), a);
Source

pub fn contains(&self, point: &DateTime) -> bool

Returns true if start <= point <= end.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let a = DateTime::parse("2024-01-01T00:00:00Z")?;
let b = DateTime::parse("2024-01-02T00:00:00Z")?;
let r = DateTimeRange::new(a, b)?;
assert!(r.contains(&a) && r.contains(&b));
assert!(!r.contains(&DateTime::parse("2024-01-02T00:00:01Z")?));
Source

pub fn intersects(&self, other: &Self) -> bool

Returns true if at least one instant is in both ranges. Ranges that only touch at one instant intersect.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let t = |s: &str| DateTime::parse(s);
let a = DateTimeRange::new(t("2024-01-01T00:00:00Z")?, t("2024-01-02T00:00:00Z")?)?;
let b = DateTimeRange::new(t("2024-01-02T00:00:00Z")?, t("2024-01-03T00:00:00Z")?)?;
assert!(a.intersects(&b));
Source

pub fn intersection(&self, other: &Self) -> Option<Self>

Returns the instants common to both ranges, or None if they are disjoint. Touching ranges give a single-instant range.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let t = |s: &str| DateTime::parse(s);
let a = DateTimeRange::new(t("2024-01-01T00:00:00Z")?, t("2024-01-03T00:00:00Z")?)?;
let b = DateTimeRange::new(t("2024-01-02T00:00:00Z")?, t("2024-01-04T00:00:00Z")?)?;
let both = a.intersection(&b);
assert_eq!(both, Some(DateTimeRange::new(t("2024-01-02T00:00:00Z")?, t("2024-01-03T00:00:00Z")?)?));
Source

pub fn union(&self, other: &Self) -> Option<Self>

Returns the smallest range covering both, if they overlap or touch; None if there is a gap between them.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let t = |s: &str| DateTime::parse(s);
let a = DateTimeRange::new(t("2024-01-01T00:00:00Z")?, t("2024-01-02T00:00:00Z")?)?;
let b = DateTimeRange::new(t("2024-01-02T00:00:00Z")?, t("2024-01-03T00:00:00Z")?)?;
let c = DateTimeRange::new(t("2024-01-05T00:00:00Z")?, t("2024-01-06T00:00:00Z")?)?;
assert!(a.union(&b).is_some());
assert_eq!(a.union(&c), None);
Source

pub fn duration(&self) -> Duration

Returns end - start.

§Examples
use dtt::datetime::DateTime;
use dtt::range::DateTimeRange;

let r = DateTimeRange::new(
    DateTime::parse("2024-01-01T00:00:00Z")?,
    DateTime::parse("2024-01-01T06:30:00Z")?,
)?;
assert_eq!(r.duration(), time::Duration::minutes(390));

Trait Implementations§

Source§

impl Clone for DateTimeRange

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DateTimeRange

Source§

impl Debug for DateTimeRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for DateTimeRange

Source§

impl Hash for DateTimeRange

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DateTimeRange

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DateTimeRange

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.