[−][src]Struct chrono_period::NaivePeriod
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
: ANaiveDateTime
representing when theNaivePeriod
will start.end
: ANaiveDateTime
representing when theNaivePeriod
will end. Note that this date/time will be included in the period itself.
Returns
- A
NaivePeriod
object having the specified start and endNaiveDateTime
s.
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
: ANaiveDateTime
representing when theNaivePeriod
will start.duration
: ADuration
object representing the the length of time thisNaivePeriod
will cover.
Returns
- A
NaivePeriod
object having the specified startNaiveDateTime
and length of the specifiedDuration
.
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
: ANaivePeriod
to intersect with thisNaivePeriod
Returns
- An
Option
containing either the intersection of the twoNaivePeriod
s, 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
: ANaivePeriod
to intersect with thisNaivePeriod
Returns
true
, if thisNaivePeriod
andother
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]
fn clone(&self) -> NaivePeriod
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Copy for NaivePeriod
[src]
impl Debug for NaivePeriod
[src]
impl Eq for NaivePeriod
[src]
impl Ord for NaivePeriod
[src]
fn cmp(&self, other: &NaivePeriod) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl PartialEq<NaivePeriod> for NaivePeriod
[src]
fn eq(&self, other: &NaivePeriod) -> bool
[src]
fn ne(&self, other: &NaivePeriod) -> bool
[src]
impl PartialOrd<NaivePeriod> for NaivePeriod
[src]
fn partial_cmp(&self, other: &NaivePeriod) -> Option<Ordering>
[src]
fn lt(&self, other: &NaivePeriod) -> bool
[src]
fn le(&self, other: &NaivePeriod) -> bool
[src]
fn gt(&self, other: &NaivePeriod) -> bool
[src]
fn ge(&self, other: &NaivePeriod) -> bool
[src]
impl StructuralEq for NaivePeriod
[src]
impl StructuralPartialEq for NaivePeriod
[src]
Auto Trait Implementations
impl RefUnwindSafe for NaivePeriod
impl Send for NaivePeriod
impl Sync for NaivePeriod
impl Unpin for NaivePeriod
impl UnwindSafe for NaivePeriod
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,