[][src]Struct chrono_period::NaivePeriod

pub struct NaivePeriod {
    pub start: NaiveDateTime,
    pub end: NaiveDateTime,
}

A period of time between two ISO 8601 dates/times (NaiveDateTimes). This is similar to a Duration, except that it has a fixed start date/time (and thus a fixed end date/time), allowing clients to determine if specific segments of time intersect.

Notes

This period is not reliant on time zones. For the time being, offsets aren't considered at all. As such, if you need to interset two time periods that are of differing time zones, you're out of luck (patches accepted, though).

Fields

start: NaiveDateTime

The date at which the period begins.

end: NaiveDateTime

The date at which the period ends. This is inclusive, meaning that the period runs up to and including this date/time.

Methods

impl NaivePeriod[src]

pub fn new(start: NaiveDateTime, end: NaiveDateTime) -> Self[src]

Create a new NaivePeriod from two NaiveDateTime objects.

Arguments

  • start: A NaiveDateTime representing when the NaivePeriod will start.
  • end: A NaiveDateTime representing when the NaivePeriod will end. Note that this date/time will be included in the period itself.

Returns

  • A NaivePeriod object having the specified start and end NaiveDateTimes.

Example

let start = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));
let end = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np1 = NaivePeriod::new(start, end);

assert_eq!(np1.duration(), Duration::days(366));

pub fn from_start_duration(start: NaiveDateTime, duration: Duration) -> Self[src]

Create a new NaivePeriod from a NaiveDateTime object and a Duration object.

Arguments

  • start: A NaiveDateTime representing when the NaivePeriod will start.
  • duration: A Duration object representing the the length of time this NaivePeriod will cover.

Returns

  • A NaivePeriod object having the specified start NaiveDateTime and length of the specified Duration.

Example

let start = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(12, 0, 0));

let np = NaivePeriod::from_start_duration(start, Duration::days(366));

assert_eq!(Duration::days(366), np.duration());

pub fn duration(&self) -> Duration[src]

Retrieve the Duration this NaivePeriod covers.

pub fn get_intersection_with(&self, other: NaivePeriod) -> Option<NaivePeriod>[src]

Retrieve the intersection of this NaivePeriod with another NaivePeriod, if one exists.

Arguments

  • other: A NaivePeriod to intersect with this NaivePeriod

Returns

  • An Option containing either the intersection of the two NaivePeriods, if they overlap; None, otherwise.

Examples

let start1 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np1 = NaivePeriod::from_start_duration(start1, Duration::days(366));

let start2 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));
let end = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np2 = NaivePeriod::new(start2, end);

let intersection = np1.get_intersection_with(np2);

assert!(intersection.is_some());
assert_eq!(Duration::days(366), intersection.unwrap().duration())

let start1 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));
let end1 = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np1 = NaivePeriod::new(start1, end1);

let start2 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 12, 1), NaiveTime::from_hms(0, 0, 0));
let end2 = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 14), NaiveTime::from_hms(0, 0, 0));

let np2 = NaivePeriod::new(start2, end2);

let start3 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 12, 1), NaiveTime::from_hms(0, 0, 0));
let end3 = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np3 = NaivePeriod::new(start3, end3);

let intersection = np1.get_intersection_with(np2);

assert_eq!(np3, intersection.unwrap());

pub fn intersects_with(&self, other: NaivePeriod) -> bool[src]

Determine if this NaivePeriod intersects with another NaivePeriod.

Arguments

  • other: A NaivePeriod to intersect with this NaivePeriod

Returns

  • true, if this NaivePeriod and other overlap in some finite time period; false, otherwise

Example

let start1 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np1 = NaivePeriod::from_start_duration(start1, Duration::days(366));

let start2 = NaiveDateTime::new(NaiveDate::from_ymd(2020, 1, 1), NaiveTime::from_hms(0, 0, 0));
let end = NaiveDateTime::new(NaiveDate::from_ymd(2021, 1, 1), NaiveTime::from_hms(0, 0, 0));

let np2 = NaivePeriod::new(start2, end);

assert!(np1.intersects_with(np2))

Trait Implementations

impl Clone for NaivePeriod[src]

impl Copy for NaivePeriod[src]

impl Debug for NaivePeriod[src]

impl Eq for NaivePeriod[src]

impl Ord for NaivePeriod[src]

impl PartialEq<NaivePeriod> for NaivePeriod[src]

impl PartialOrd<NaivePeriod> for NaivePeriod[src]

impl StructuralEq for NaivePeriod[src]

impl StructuralPartialEq for NaivePeriod[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.