Skip to content

Commit 92ae781

Browse files
authored
Added wrappers for converting UTC times into GPS times (#83)
1 parent 6b49375 commit 92ae781

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

swiftnav-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ fn main() {
7373
.allowlist_function("add_secs")
7474
.allowlist_function("decode_utc_parameters")
7575
.allowlist_function("gps2utc")
76+
.allowlist_function("utc2gps")
7677
.allowlist_function("date2mjd")
7778
.allowlist_function("mjd2utc")
7879
.allowlist_function("utc2mjd")

swiftnav/src/time.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ impl GpsTime {
123123
&self.0
124124
}
125125

126+
pub(crate) fn mut_c_ptr(&mut self) -> *mut swiftnav_sys::gps_time_t {
127+
&mut self.0
128+
}
129+
126130
pub(crate) fn unknown() -> swiftnav_sys::gps_time_t {
127131
swiftnav_sys::gps_time_t { tow: -1.0, wn: -1 }
128132
}
@@ -729,19 +733,27 @@ impl UtcTime {
729733
)
730734
}
731735

736+
/// Converts the UTC time into GPS time
737+
pub fn to_gps(&self, utc_params: &UtcParams) -> GpsTime {
738+
let mut gps = GpsTime::new_unchecked(0, 0.0);
739+
unsafe {
740+
swiftnav_sys::utc2gps(self.c_ptr(), gps.mut_c_ptr(), utc_params.c_ptr());
741+
}
742+
gps
743+
}
744+
745+
/// Converts the UTC time into GPS time using the hardcoded list of leap
746+
/// seconds.
747+
///
748+
/// # ⚠️ 🦘 ⏱ ⚠️ - Leap Seconds
749+
/// The hard coded list of leap seconds will get out of date, it is
750+
/// preferable to use [`UtcTime::to_gps()`] with the newest set of UTC parameters
732751
pub fn to_gps_hardcoded(&self) -> GpsTime {
733-
let is_lse = self.seconds() >= 60.0;
734-
let mjd = self.to_mjd();
735-
let gps = unsafe { swiftnav_sys::mjd2gps(mjd.0) };
736-
let gps = GpsTime(gps);
737-
738-
// During a leap second event the MJD is wrong by a second, so remove the
739-
// erroneous second here
740-
if is_lse {
741-
gps - std::time::Duration::from_secs(1)
742-
} else {
743-
gps
752+
let mut gps = GpsTime::new_unchecked(0, 0.0);
753+
unsafe {
754+
swiftnav_sys::utc2gps(self.c_ptr(), gps.mut_c_ptr(), std::ptr::null());
744755
}
756+
gps
745757
}
746758
}
747759

0 commit comments

Comments
 (0)