Skip to content

Commit

Permalink
Use parseIntSafe and some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
angas committed Apr 6, 2020
1 parent c34dd9b commit 2debf2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
32 changes: 16 additions & 16 deletions codecs/ZDA.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
/*
* === GST - UTC day, month, and year, and local time zone offset ===
* === ZDA - Time & Date - UTC, day, month, year and local time zone ===
*
* ------------------------------------------------------------------------------
* 1 2 3 4 5 6 7
* | | | | | | |
* $--GST,hhmmss.ss,dd,mm,yyyy,zz,zz,*hh<CR><LF>
* 1 2 3 4 5 6 7
* | | | | | | |
* $--ZDA,hhmmss.ss,dd,mm,yyyy,zz,zz*hh<CR><LF>
* ------------------------------------------------------------------------------
*
* Field Number:
* 1. UTC time
* 2. Day, ranging between 01 and 31
* 3. Month, ranging between 01 and 12
* 4. Year
* 5. Local time zone offset from GMT, ranging from 00 through ±13 hours
* 6. Local time zone offset from GMT, ranging from 00 through 59 minutes
* 1. UTC time (hours, minutes, seconds, may have fractional subsecond)
* 2. Day, 01 to 31
* 3. Month, 01 to 12
* 4. Year (4 digits)
* 5. Local zone description, 00 to +- 13 hours
* 6. Local zone minutes description, 00 to 59, apply same sign as local hours
* 7. Checksum
*/

import { parseTime } from "../helpers";
import { parseIntSafe, parseTime } from "../helpers";

export const sentenceId: "ZDA" = "ZDA";
export const sentenceName = "Data and Time";
export const sentenceName = "UTC, day, month, year, and local time zone";

export interface ZDAPacket {
sentenceId: "ZDA";
sentenceName: string;
talkerId?: string;
datetime: Date;
localHoursOffset: number;
localMinutesOffset: number;
localZoneHours: number;
localZoneMinutes: number;
}

export function decodeSentence(fields: string[]): ZDAPacket {
return {
sentenceId: sentenceId,
sentenceName: sentenceName,
datetime: parseTime(fields[1], fields.slice(2, 5).join("")),
localHoursOffset: parseInt(fields[5], 10),
localMinutesOffset: parseInt(fields[6], 10)
localZoneHours: parseIntSafe(fields[5]),
localZoneMinutes: parseIntSafe(fields[6])
};
}
13 changes: 5 additions & 8 deletions tests/ZDAtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { parseNmeaSentence } from "../index";

describe("ZDA", (): void => {
it("parser", (): void => {
const packet = parseNmeaSentence(
"$GNZDA,050956.00,30,03,2020,00,00*77"
);

const packet = parseNmeaSentence("$GPZDA,160012.71,11,03,2004,-1,00*7D");
packet.should.have.property("sentenceId", "ZDA");
packet.should.have.property("sentenceName", "Data and Time");
packet.should.have.property("datetime", new Date("2020-03-30T05:09:56.000Z"));
packet.should.have.property("localHoursOffset", 0);
packet.should.have.property("localMinutesOffset", 0);
packet.should.have.property("sentenceName", "UTC, day, month, year, and local time zone");
packet.should.have.property("datetime", new Date("2004-03-11T16:00:12.710Z"));
packet.should.have.property("localZoneHours", -1);
packet.should.have.property("localZoneMinutes", 0);
});
});

0 comments on commit 2debf2c

Please sign in to comment.