Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Nov 4, 2024
1 parent d29017c commit b09f08b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
22 changes: 14 additions & 8 deletions crates/lox-time/src/python/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use pyo3::{pyclass, pymethods, Bound, PyAny, PyErr, PyObject, PyResult, Python};

use lox_math::is_close::IsClose;

use super::ut1::PyNoOpOffsetProvider;
use crate::calendar_dates::{CalendarDate, Date};
use crate::deltas::{TimeDelta, ToDelta};
use crate::julian_dates::{Epoch, JulianDate, Unit};
Expand All @@ -26,11 +27,16 @@ use crate::python::ut1::{PyDeltaUt1Provider, PyUt1Provider};
use crate::python::utc::PyUtc;
use crate::subsecond::{InvalidSubsecond, Subsecond};
use crate::time_of_day::TimeOfDay;
use crate::time_scales::{DynTimeScale, InvalidTimeScale};
use crate::transformations::{ToTai, ToTcb, ToTcg, ToTdb, ToTt, TryToScale};
use crate::utc::transformations::ToUtc;
use crate::{Time, TimeError, TimeLike};
use crate::{DynTime, Time, TimeError, TimeLike};

use super::ut1::PyNoOpOffsetProvider;
impl From<InvalidTimeScale> for PyErr {
fn from(value: InvalidTimeScale) -> Self {
PyValueError::new_err(value.to_string())
}
}

impl From<InvalidSubsecond> for PyErr {
fn from(value: InvalidSubsecond) -> Self {
Expand Down Expand Up @@ -73,7 +79,7 @@ impl FromStr for Unit {

#[pyclass(name = "Time", module = "lox_space", frozen)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PyTime(pub Time<PyTimeScale>);
pub struct PyTime(pub(crate) DynTime);

#[pymethods]
impl PyTime {
Expand All @@ -88,7 +94,7 @@ impl PyTime {
minute: u8,
seconds: f64,
) -> PyResult<PyTime> {
let scale: PyTimeScale = scale.parse()?;
let scale: DynTimeScale = scale.parse()?;
let time = Time::builder_with_scale(scale)
.with_ymd(year, month, day)
.with_hms(hour, minute, seconds)
Expand All @@ -104,7 +110,7 @@ impl PyTime {
jd: f64,
epoch: &str,
) -> PyResult<Self> {
let scale: PyTimeScale = scale.parse()?;
let scale: DynTimeScale = scale.parse()?;
let epoch: Epoch = epoch.parse()?;
Ok(Self(Time::from_julian_date(scale, jd, epoch)?))
}
Expand Down Expand Up @@ -141,11 +147,11 @@ impl PyTime {

#[classmethod]
pub fn from_iso(_cls: &Bound<'_, PyType>, iso: &str, scale: Option<&str>) -> PyResult<PyTime> {
let scale: PyTimeScale = match scale {
let scale: DynTimeScale = match scale {
Some(scale) => scale.parse()?,
None => match iso.split_once(char::is_whitespace) {
Some((_, scale)) => scale.parse()?,
None => PyTimeScale::Tai,
None => DynTimeScale::Tai,
},
};
let time = Time::from_iso(scale, iso)?;
Expand All @@ -159,7 +165,7 @@ impl PyTime {
seconds: i64,
subsecond: f64,
) -> PyResult<PyTime> {
let scale: PyTimeScale = scale.parse()?;
let scale: DynTimeScale = scale.parse()?;
let subsecond = Subsecond::new(subsecond)?;
let time = Time::new(scale, seconds, subsecond);
Ok(PyTime(time))
Expand Down
39 changes: 39 additions & 0 deletions crates/lox-time/src/time_scales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
exclusively as an IO format.
*/

use std::str::FromStr;

use thiserror::Error;

pub mod transformations;

/// Marker trait denoting a continuous astronomical time scale.
Expand Down Expand Up @@ -136,6 +140,26 @@ impl TimeScale for DynTimeScale {
}
}

#[derive(Error, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
#[error("invalid time scale: {0}")]
pub struct InvalidTimeScale(String);

impl FromStr for DynTimeScale {
type Err = InvalidTimeScale;

fn from_str(name: &str) -> Result<Self, Self::Err> {
match name {
"TAI" => Ok(DynTimeScale::Tai),
"TCB" => Ok(DynTimeScale::Tcb),
"TCG" => Ok(DynTimeScale::Tcg),
"TDB" => Ok(DynTimeScale::Tdb),
"TT" => Ok(DynTimeScale::Tt),
"UT1" => Ok(DynTimeScale::Ut1),
_ => Err(InvalidTimeScale(name.to_string())),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -156,4 +180,19 @@ mod tests {
assert_eq!(scale.abbreviation(), abbreviation);
assert_eq!(scale.name(), name);
}

#[rstest]
#[case("TAI", "International Atomic Time")]
#[case("TT", "Terrestrial Time")]
#[case("TCG", "Geocentric Coordinate Time")]
#[case("TCB", "Barycentric Coordinate Time")]
#[case("TDB", "Barycentric Dynamical Time")]
#[case("UT1", "Universal Time")]
#[should_panic(expected = "invalid time scale: NotATimeScale")]
#[case("NotATimeScale", "not a time scale")]
fn test_dyn_time_scale(#[case] abbreviation: &'static str, #[case] name: &'static str) {
let scale = DynTimeScale::from_str(abbreviation).unwrap();
assert_eq!(scale.abbreviation(), abbreviation);
assert_eq!(scale.name(), name);
}
}

0 comments on commit b09f08b

Please sign in to comment.