Skip to content

Commit 8261d47

Browse files
authored
make timestamp pad and use seconds correctly (#140)
1 parent af91536 commit 8261d47

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

swiftnav/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ thiserror = "2.0.17"
2020
[dev-dependencies]
2121
float_eq = "1.0.1"
2222
proptest = "1.9.0"
23+
nmea = "0.7"
2324

2425
# This tells docs.rs to include the katex header for math formatting
2526
# To do this locally

swiftnav/src/nmea/gga.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl GGA {
117117
let second = f64::from(self.time.second());
118118
let second_fracs = f64::from(self.time.nanosecond()) / 1_000_000_000.0;
119119

120-
let timestamp = format!("{hour}{minute}{:.2}", second + second_fracs);
120+
let timestamp = format!("{hour:0>2}{minute:0>2}{:05.2}", second + second_fracs);
121121

122122
let (lat_deg, lat_mins) = self.llh.latitude_degree_decimal_minutes();
123123
let lat_hemisphere = self.llh.latitudinal_hemisphere();
@@ -165,6 +165,28 @@ impl GGA {
165165
mod test {
166166
use super::*;
167167

168+
#[test]
169+
#[allow(clippy::float_cmp, reason = "testing exact parsing")]
170+
fn nmea_crate_can_parse_gga_sentence() {
171+
let gga = GGA::builder()
172+
.sat_in_use(12)
173+
.time(DateTime::from_timestamp(1_761_351_489, 89_999_999).unwrap())
174+
.gps_quality(GPSQuality::SPS)
175+
.hdop(0.9)
176+
.llh(super::LLHDegrees::new(37.7749, -122.4194, 10.0))
177+
.build();
178+
179+
let parse_result =
180+
::nmea::parse_str(&gga.to_sentence()).expect("Failed to parse GGA sentence");
181+
182+
let ::nmea::ParseResult::GGA(parsed_gga) = parse_result else {
183+
panic!("Parsed result is not a GGA sentence");
184+
};
185+
186+
assert_eq!(parsed_gga.latitude.unwrap(), 37.7749);
187+
assert_eq!(parsed_gga.longitude.unwrap(), -122.4194);
188+
}
189+
168190
#[test]
169191
fn gga_can_be_turned_into_an_nmea_sentence() {
170192
let gga = GGA::builder()
@@ -179,7 +201,7 @@ mod test {
179201

180202
assert_eq!(
181203
sentence,
182-
"$GPGGA,0189.00,3746.4940000,N,12225.1640000,W,1,12,0.9,0.0,M,,,*01\r\n"
204+
"$GPGGA,001809.00,3746.4940000,N,12225.1640000,W,1,12,0.9,0.0,M,,,*01\r\n"
183205
);
184206
}
185207

@@ -200,7 +222,7 @@ mod test {
200222

201223
assert_eq!(
202224
sentence,
203-
"$GPGGA,0189.00,3403.1320000,N,01814.6220000,W,2,8,1.2,0.0,M,1.00,2,42*1C\r\n"
225+
"$GPGGA,001809.00,3403.1320000,N,01814.6220000,W,2,8,1.2,0.0,M,1.00,2,42*1C\r\n"
204226
);
205227
}
206228

0 commit comments

Comments
 (0)