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: TImplementations§
Source§impl<T> Interval<T>where
T: TimeInstant,
impl<T> Interval<T>where
T: TimeInstant,
Sourcepub fn new(start: T, end: T) -> Interval<T>
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 instantend- 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);Sourcepub fn try_new(start: T, end: T) -> Result<Interval<T>, InvalidIntervalError>
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());Sourcepub fn duration(&self) -> <T as TimeInstant>::Duration
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));Sourcepub fn intersection(&self, other: &Interval<T>) -> Option<Interval<T>>
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,
impl<S> Interval<Time<S>>where
S: TimeScale,
Sourcepub fn to<Target>(
&self,
) -> Result<Interval<<Target as PeriodTimeTarget<S>>::Instant>, ConversionError>where
Target: PeriodTimeTarget<S>,
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>
impl<T> Interval<T>
Sourcepub fn duration_days(&self) -> Quantity<Day>
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>>
impl Interval<DateTime<Utc>>
Sourcepub fn to<Target>(&self) -> Interval<<Target as PeriodUtcTarget>::Instant>where
Target: PeriodUtcTarget,
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>
Sourcepub fn duration_days(&self) -> f64
pub fn duration_days(&self) -> f64
Returns the duration in days as a floating-point value.
This converts the chrono::Duration to days.
Sourcepub fn duration_seconds(&self) -> i64
pub fn duration_seconds(&self) -> i64
Returns the duration in seconds.