Skip to main content

Interval

Struct Interval 

Source
pub struct Interval<T>
where T: TimeInstant,
{ pub start: T, pub end: T, }
Expand description

Represents an interval between two instants.

An Interval is defined by a start and end time instant of type T, where T implements the TimeInstant trait. This allows for periods defined in different time systems (Julian Date, Modified Julian Date, UTC, etc.).

§Examples

use tempoch::{Interval, ModifiedJulianDate};

let start = ModifiedJulianDate::new(59000.0);
let end = ModifiedJulianDate::new(59001.0);
let period = Interval::new(start, end);

// Duration in days
let duration = period.duration();

Fields§

§start: T§end: T

Implementations§

Source§

impl<T> Interval<T>
where T: TimeInstant,

Source

pub fn new(start: T, end: T) -> Interval<T>

Creates a new period between two time instants.

Note: this constructor does not validate that start <= end. Prefer try_new when endpoints come from untrusted or computed input.

§Arguments
  • start - The start time instant
  • end - The end time instant
§Examples
use tempoch::{Interval, JulianDate};

let start = JulianDate::new(2451545.0);
let end = JulianDate::new(2451546.0);
let period = Interval::new(start, end);
Source

pub fn try_new(start: T, end: T) -> Result<Interval<T>, InvalidIntervalError>

Creates a new interval, validating that start <= end.

Returns InvalidIntervalError::StartAfterEnd if the start instant is after the end instant. This also rejects NaN-based instants, since NaN comparisons always return false.

§Examples
use tempoch::{Interval, JulianDate};

let ok = Interval::try_new(JulianDate::new(100.0), JulianDate::new(200.0));
assert!(ok.is_ok());

let err = Interval::try_new(JulianDate::new(200.0), JulianDate::new(100.0));
assert!(err.is_err());
Source

pub fn duration(&self) -> <T as TimeInstant>::Duration

Returns the duration of the period as the difference between end and start.

§Examples
use tempoch::{Interval, JulianDate};
use qtty::Days;

let start = JulianDate::new(2451545.0);
let end = JulianDate::new(2451546.5);
let period = Interval::new(start, end);

let duration = period.duration();
assert_eq!(duration, Days::new(1.5));
Source

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

Returns the overlapping sub-period between self and other.

Periods are treated as half-open ranges [start, end): if one period ends exactly when the other starts, the intersection is empty and None is returned.

Source§

impl<S> Interval<Time<S>>
where S: TimeScale,

Source

pub fn to<Target>( &self, ) -> Result<Interval<<Target as PeriodTimeTarget<S>>::Instant>, ConversionError>
where Target: PeriodTimeTarget<S>,

Convert this period to another time scale.

Each endpoint is converted preserving the represented absolute interval.

Supported targets:

  • Any time-scale marker (JD, MJD, UT, …)
  • chrono::DateTime<Utc>
§Errors

Returns ConversionError::OutOfRange if the endpoints fall outside the representable range of the target type (only possible when converting to DateTime<Utc>).

§Examples
use chrono::{DateTime, Utc};
use tempoch::{Interval, JD, MJD, Period, Time};

let period_jd = Period::new(Time::<JD>::new(2451545.0), Time::<JD>::new(2451546.0));
let period_mjd = period_jd.to::<MJD>().unwrap();
let _period_utc: Interval<DateTime<Utc>> = period_jd.to::<DateTime<Utc>>().unwrap();

assert!((period_mjd.start.value() - 51544.5).abs() < 1e-12);
assert!((period_mjd.end.value() - 51545.5).abs() < 1e-12);
Source§

impl<T> Interval<T>
where T: TimeInstant<Duration = Quantity<Day>>,

Source

pub fn duration_days(&self) -> Quantity<Day>

Returns the duration of the period in days as a floating-point value.

This method is available for time instants with Days as their duration type (e.g., JulianDate and ModifiedJulianDate).

§Examples
use tempoch::{Interval, ModifiedJulianDate};
use qtty::Days;

let start = ModifiedJulianDate::new(59000.0);
let end = ModifiedJulianDate::new(59001.5);
let period = Interval::new(start, end);

assert_eq!(period.duration_days(), Days::new(1.5));
Source§

impl Interval<DateTime<Utc>>

Source

pub fn to<Target>(&self) -> Interval<<Target as PeriodUtcTarget>::Instant>
where Target: PeriodUtcTarget,

Convert this UTC interval to another target.

Supported targets:

  • Any time-scale marker (JD, MJD, UT, …)
  • Any Time<...> alias (JulianDate, ModifiedJulianDate, …)
  • chrono::DateTime<Utc>
Source

pub fn duration_days(&self) -> f64

Returns the duration in days as a floating-point value.

This converts the chrono::Duration to days.

Source

pub fn duration_seconds(&self) -> i64

Returns the duration in seconds.

Trait Implementations§

Source§

impl<T> Clone for Interval<T>
where T: Clone + TimeInstant,

Source§

fn clone(&self) -> Interval<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Interval<T>
where T: Debug + TimeInstant,

Source§

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

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

impl<T> Display for Interval<T>
where T: TimeInstant + Display,

Source§

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

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

impl<T> PartialEq for Interval<T>

Source§

fn eq(&self, other: &Interval<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Copy for Interval<T>
where T: Copy + TimeInstant,

Source§

impl<T> StructuralPartialEq for Interval<T>
where T: TimeInstant,

Auto Trait Implementations§

§

impl<T> Freeze for Interval<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Interval<T>
where T: RefUnwindSafe,

§

impl<T> Send for Interval<T>
where T: Send,

§

impl<T> Sync for Interval<T>
where T: Sync,

§

impl<T> Unpin for Interval<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Interval<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Interval<T>
where T: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.