Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement transformations on time scales #128

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion crates/lox-time/src/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

use std::fmt::Display;
use std::ops::{Add, Neg, RangeInclusive, Sub};
use std::ops::{Add, AddAssign, Neg, RangeInclusive, Sub, SubAssign};

use num::ToPrimitive;
use thiserror::Error;
Expand Down Expand Up @@ -339,6 +339,12 @@ impl Add for TimeDelta {
}
}

impl AddAssign for TimeDelta {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

impl Sub for TimeDelta {
type Output = Self;

Expand All @@ -360,6 +366,12 @@ impl Sub for TimeDelta {
}
}

impl SubAssign for TimeDelta {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}

impl From<i64> for TimeDelta {
fn from(value: i64) -> Self {
TimeDelta::from_seconds(value)
Expand Down
84 changes: 82 additions & 2 deletions crates/lox-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ use time_scales::{Tai, Tcb, Tcg, Tdb, Tt, Ut1};
use crate::calendar_dates::{CalendarDate, Date};
use crate::deltas::{TimeDelta, ToDelta};
use crate::julian_dates::{Epoch, JulianDate, Unit};
use crate::prelude::transformations::ToScale;
use crate::subsecond::Subsecond;
use crate::time_scales::TimeScale;
use crate::time_scales::transformations::{OffsetProvider, ToTai, TryToScale};
use crate::time_scales::{DynTimeScale, TimeScale};
use crate::utc::leap_seconds::BuiltinLeapSeconds;
use crate::utc::transformations::to_utc_with_provider;
use crate::utc::{LeapSecondsProvider, Utc, UtcError};

pub mod calendar_dates;
pub mod constants;
Expand All @@ -71,7 +76,6 @@ pub mod subsecond;
pub(crate) mod test_helpers;
pub mod time_of_day;
pub mod time_scales;
pub mod transformations;
pub mod ut1;
pub mod utc;

Expand Down Expand Up @@ -128,6 +132,8 @@ pub struct Time<T: TimeScale> {
subsecond: Subsecond,
}

pub type DynTime = Time<DynTimeScale>;

impl<T: TimeScale> Time<T> {
/// Instantiates a [Time] in the given [TimeScale] from the count of seconds since J2000, subdivided
/// into integral seconds and [Subsecond].
Expand Down Expand Up @@ -344,6 +350,80 @@ impl<T: TimeScale> Time<T> {
pub fn subsecond(&self) -> f64 {
self.subsecond.into()
}

pub fn try_to_scale<U, P>(&self, scale: U, provider: &P) -> Result<Time<U>, P::Error>
where
T: TryToScale<U, P>,
U: TimeScale,
P: OffsetProvider,
{
let delta = self.scale.try_to_scale(&scale, self.to_delta(), provider)?;
Ok(Time::from_delta(scale, delta))
}

pub fn to_scale<U: TimeScale>(&self, scale: U) -> Time<U>
where
T: ToScale<U>,
{
let delta = self.scale.to_scale(&scale, self.to_delta());
Time::from_delta(scale, delta)
}

pub fn to_tai(&self) -> Time<Tai>
where
T: ToScale<Tai>,
{
self.to_scale(Tai)
}

pub fn to_tcb(&self) -> Time<Tcb>
where
T: ToScale<Tcb>,
{
self.to_scale(Tcb)
}

pub fn to_tcg(&self) -> Time<Tcg>
where
T: ToScale<Tcg>,
{
self.to_scale(Tcg)
}

pub fn to_tdb(&self) -> Time<Tdb>
where
T: ToScale<Tdb>,
{
self.to_scale(Tdb)
}

pub fn to_tt(&self) -> Time<Tt>
where
T: ToScale<Tt>,
{
self.to_scale(Tt)
}

pub fn try_to_ut1<P: OffsetProvider>(&self, provider: &P) -> Result<Time<Ut1>, P::Error>
where
T: TryToScale<Ut1, P>,
{
self.try_to_scale(Ut1, provider)
}

pub fn to_utc_with_provider(&self, provider: &impl LeapSecondsProvider) -> Result<Utc, UtcError>
where
T: ToScale<Tai>,
{
to_utc_with_provider(self.to_tai(), provider)
}

pub fn to_utc(&self) -> Result<Utc, UtcError>
where
T: ToScale<Tai>,
{
self.to_utc_with_provider(&BuiltinLeapSeconds)
}
}

impl<T: TimeScale> IsClose for Time<T> {
Expand Down
Loading
Loading