Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit fdf9450

Browse files
klyvesimeonmiteff
authored andcommitted
Added validation for timestamp according to nmea spec
1 parent 4733bb9 commit fdf9450

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

tagblock.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package nmea
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"strconv"
78
"strings"
9+
"time"
810
)
911

1012
const (
@@ -42,13 +44,30 @@ type TagBlock struct {
4244
}
4345

4446
func parseInt64(raw string) (int64, error) {
45-
i, err := strconv.ParseInt(raw[2:], 10, 32)
47+
i, err := strconv.ParseInt(raw[2:], 10, 64)
4648
if err != nil {
4749
return 0, fmt.Errorf("nmea: tagblock unable to parse uint32 [%s]", raw)
4850
}
4951
return i, nil
5052
}
5153

54+
// Timestamp can come as milliseconds or seconds
55+
func validUnixTimestamp(timestamp int64) (int64, error) {
56+
if timestamp < 0 {
57+
return 0, errors.New("nmea: Tagblock timestamp is not valid must be between 0 and now + 24h")
58+
}
59+
now := time.Now()
60+
unix := now.Unix() + 24*3600
61+
if timestamp > unix {
62+
if timestamp > unix*1000 {
63+
return 0, errors.New("nmea: Tagblock timestamp is not valid")
64+
}
65+
return timestamp / 1000, nil
66+
}
67+
68+
return timestamp, nil
69+
}
70+
5271
// parseTagBlock adds support for tagblocks
5372
// https://rietman.wordpress.com/2016/09/17/nemastudio-now-supports-the-nmea-0183-tag-block/
5473
func parseTagBlock(raw string) (TagBlock, string, error) {
@@ -90,6 +109,10 @@ func parseTagBlock(raw string) (TagBlock, string, error) {
90109
if err != nil {
91110
return tagBlock, raw, err
92111
}
112+
tagBlock.Time, err = validUnixTimestamp(tagBlock.Time)
113+
if err != nil {
114+
return tagBlock, raw, err
115+
}
93116
case TypeDestinationID:
94117
tagBlock.Destination = item[2:]
95118
case TypeGrouping:

tagblock_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ var tagblocktests = []struct {
4141
Head: "UdPbC?",
4242
},
4343
},
44+
{
45+
name: "Test unix timestamp",
46+
raw: "UdPbC?\\x:NorSat_1,c:1564827317*42\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
47+
msg: TagBlock{
48+
Time: 1564827317,
49+
Source: "",
50+
Head: "UdPbC?",
51+
},
52+
},
53+
{
54+
55+
name: "Test milliseconds timestamp",
56+
raw: "UdPbC?\\x:NorSat_1,c:1564827317000*72\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
57+
msg: TagBlock{
58+
Time: 1564827317,
59+
Source: "",
60+
Head: "UdPbC?",
61+
},
62+
},
63+
{
64+
65+
name: "Test invalid high timestamp",
66+
raw: "UdPbC?\\x:NorSat_1,c:25648273170000000*71\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
67+
err: "nmea: Tagblock timestamp is not valid",
68+
},
69+
{
70+
71+
name: "Test invalid low timestamp",
72+
raw: "UdPbC?\\x:NorSat_1,c:-10*60\\!AIVDM,1,1,,A,19NSRaP02A0fo91kwnaMKbjR08:J,0*15",
73+
err: "nmea: Tagblock timestamp is not valid must be between 0 and now + 24h",
74+
},
4475
{
4576

4677
name: "Test all input types",

0 commit comments

Comments
 (0)