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
impl DateTimeRange
Sourcepub fn new(start: DateTime, end: DateTime) -> Result<Self, DateTimeError>
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));Sourcepub const fn start(&self) -> DateTime
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);Sourcepub const fn end(&self) -> DateTime
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);Sourcepub fn contains(&self, point: &DateTime) -> bool
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")?));Sourcepub fn intersects(&self, other: &Self) -> bool
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));Sourcepub fn intersection(&self, other: &Self) -> Option<Self>
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")?)?));Sourcepub fn union(&self, other: &Self) -> Option<Self>
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);Sourcepub fn duration(&self) -> Duration
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));