Skip to content

Commit a31ba26

Browse files
authored
Fix gps time calculation (#785)
* Change test case to match the date patterns where the bug reproduces * Fix RMC date and time calculation
1 parent 303ec94 commit a31ba26

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

gps/gpsparser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ func (parser *Parser) Parse(sentence string) (Fix, error) {
9696
fix.Speed = findSpeed(fields[7])
9797
fix.Heading = findHeading(fields[8])
9898
date := findDate(fields[9])
99-
fix.Time = fix.Time.AddDate(date.Year(), int(date.Month()), date.Day())
99+
fix.Time = date.Add(time.Duration(fix.Time.Hour())*time.Hour +
100+
time.Duration(fix.Time.Minute())*time.Minute +
101+
time.Duration(fix.Time.Second())*time.Second +
102+
time.Duration(fix.Time.Nanosecond())*time.Nanosecond)
100103

101104
return fix, nil
102105
}

gps/gpsparser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ func TestParseRMC(t *testing.T) {
7070
t.Error("should have errInvalidRMCSentence error")
7171
}
7272

73-
val = "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,W,0.004,133.4,130522,0.0,E,D*2B"
73+
val = "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,W,0.004,133.4,010622,0.0,E,D*2B"
7474
fix, err := p.Parse(val)
7575
if err != nil {
7676
t.Error("should have parsed")
7777
}
7878

7979
c.Assert(fix.Time.Year(), qt.Equals, 2022)
80-
c.Assert(fix.Time.Month(), qt.Equals, time.May)
81-
c.Assert(fix.Time.Day(), qt.Equals, 13)
80+
c.Assert(fix.Time.Month(), qt.Equals, time.June)
81+
c.Assert(fix.Time.Day(), qt.Equals, 1)
8282
c.Assert(fix.Time.Hour(), qt.Equals, 20)
8383
c.Assert(fix.Time.Minute(), qt.Equals, 35)
8484
c.Assert(fix.Time.Second(), qt.Equals, 22)

0 commit comments

Comments
 (0)