Skip to content

Commit

Permalink
Merge pull request #168 from gwbres/timescale_fmt
Browse files Browse the repository at this point in the history
timecale.rs: introduce format/display
  • Loading branch information
ChristopherRabotin authored Nov 2, 2022
2 parents fe389c6 + 7bd6ee5 commit 4d93c77
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/timescale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use pyo3::prelude::*;
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};

use core::fmt;
use core::str::FromStr;

use crate::{Duration, Epoch, Errors, ParsingErrors, SECONDS_PER_DAY};
Expand Down Expand Up @@ -81,6 +82,40 @@ impl TimeScale {
Self::ET | Self::TT => 2,
}
}

/// Returns true if Self is based off a GNSS constellation
pub const fn is_gnss(&self) -> bool {
matches!(self, Self::GPST | Self::GST | Self::BDT)
}
}

impl fmt::Display for TimeScale {
/// Prints given TimeScale
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::TAI => write!(f, "TAI"),
Self::TT => write!(f, "TT"),
Self::ET => write!(f, "ET"),
Self::TDB => write!(f, "TDB"),
Self::UTC => write!(f, "UTC"),
Self::GPST => write!(f, "GPST"),
Self::GST => write!(f, "GST"),
Self::BDT => write!(f, "BDT"),
}
}
}

impl fmt::LowerHex for TimeScale {
/// Prints given TimeScale in RINEX format
/// ie., standard GNSS constellation name is prefered when possible
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::GPST => write!(f, "GPS"),
Self::GST => write!(f, "GAL"),
Self::BDT => write!(f, "BDS"),
_ => write!(f, "{self}"),
}
}
}

#[cfg_attr(feature = "python", pymethods)]
Expand Down
45 changes: 45 additions & 0 deletions tests/timescale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extern crate hifitime;
use hifitime::TimeScale;
use std::str::FromStr;

#[test]
fn test_from_str() {
let values = vec![
("TAI", TimeScale::TAI),
("UTC", TimeScale::UTC),
("TT", TimeScale::TT),
("TDB", TimeScale::TDB),
("GPST", TimeScale::GPST),
("GST", TimeScale::GST),
("BDT", TimeScale::BDT),
];
for value in values {
let (descriptor, expected) = value;
let ts = TimeScale::from_str(descriptor);
assert_eq!(ts.is_ok(), true);
let ts = ts.unwrap();
assert_eq!(ts, expected);
// test to_str()/format()
assert_eq!(format!("{}", ts), descriptor);
// test format(0x)
let expected: &str = match ts {
TimeScale::GPST => "GPS",
TimeScale::GST => "GAL",
TimeScale::BDT => "BDS",
_ => descriptor, // untouched
};
assert_eq!(format!("{:x}", ts), expected);
}
}

#[test]
fn test_is_gnss() {
let ts = TimeScale::GPST;
assert!(ts.is_gnss());
let ts = TimeScale::GST;
assert!(ts.is_gnss());
let ts = TimeScale::UTC;
assert!(!ts.is_gnss());
let ts = TimeScale::TAI;
assert!(!ts.is_gnss());
}

0 comments on commit 4d93c77

Please sign in to comment.