nmea_parser/gnss/
zda.rs

1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use super::*;
18
19/// ZDA - Time and date
20#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct ZdaData {
22    /// Navigation system
23    pub source: NavigationSystem,
24
25    /// UTC
26    #[serde(with = "json_date_time_utc")]
27    pub timestamp_utc: Option<DateTime<Utc>>,
28
29    /// Local time zone offset
30    #[serde(with = "json_fixed_offset")]
31    pub timezone_local: Option<FixedOffset>,
32}
33
34// -------------------------------------------------------------------------------------------------
35
36/// xxZDA: MSK Receiver Signal
37pub(crate) fn handle(
38    sentence: &str,
39    nav_system: NavigationSystem,
40) -> Result<ParsedMessage, ParseError> {
41    let split: Vec<&str> = sentence.split(',').collect();
42
43    Ok(ParsedMessage::Zda(ZdaData {
44        source: nav_system,
45        timestamp_utc: parse_hhmmss_ss(
46            split.get(1).unwrap_or(&""),
47            pick_date_with_fields(&split, 4, 3, 2, 0, 0, 0, 0)?,
48        )
49        .ok(),
50        timezone_local: pick_timezone_with_fields(&split, 5, 6).ok(),
51    }))
52}
53
54// -------------------------------------------------------------------------------------------------
55
56#[cfg(test)]
57mod test {
58    use super::*;
59
60    #[test]
61    fn test_parse_cpzda() {
62        match NmeaParser::new().parse_sentence("$GPZDA,072914.00,31,05,2018,-03,00") {
63            Ok(ps) => match ps {
64                ParsedMessage::Zda(zda) => {
65                    assert_eq!(zda.source, NavigationSystem::Gps);
66                    assert_eq!(
67                        zda.timestamp_utc,
68                        Utc.with_ymd_and_hms(2018, 5, 31, 7, 29, 14).single()
69                    );
70                    assert_eq!(zda.timezone_local, FixedOffset::east_opt(-3 * 3600));
71                }
72                ParsedMessage::Incomplete => {
73                    assert!(false);
74                }
75                _ => {
76                    assert!(false);
77                }
78            },
79            Err(e) => {
80                assert_eq!(e.to_string(), "OK");
81            }
82        }
83    }
84}