diff --git a/.travis.yml b/.travis.yml index 1f58dc1..667a919 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: go go: - 1.11.x - 1.12.x +- 1.14.x +- 1.15.x script: - go fmt $(go list ./... | grep -v /vendor/) diff --git a/gps.go b/gps.go index c243cfd..0ca1402 100644 --- a/gps.go +++ b/gps.go @@ -28,10 +28,7 @@ var leaps []time.Duration // Called when package is imported. func init() { - if len(leaps) != 0 { - // already initialized - return - } + leaps = make([]time.Duration, 0) previous := 0 for i, yearLeaps := range leapSequence { year := leapSecondOrigin + i diff --git a/gps_time.go b/gps_time.go index cab706b..d73aa79 100644 --- a/gps_time.go +++ b/gps_time.go @@ -21,3 +21,23 @@ func (t GpsTime) Gps() time.Duration { func (t GpsTime) String() string { return time.Time(t).String() } + +// ToUTC returns t as a time.Time in UTC. +func (t GpsTime) ToUTC() time.Time { + return toUtcTime(t.Gps()) +} + +// Add returns the time t+d. +func (t GpsTime) Add(d time.Duration) GpsTime { + return Gps(t.Gps() + d) +} + +// Sub returns the duration t-u. +func (t GpsTime) Sub(u GpsTime) time.Duration { + return t.Gps() - u.Gps() +} + +// Equal reports whether t and u represent the same time instant. +func (t GpsTime) Equal(u GpsTime) bool { + return t.Gps() == u.Gps() +} diff --git a/gps_time_test.go b/gps_time_test.go index 4d91091..cd2128c 100644 --- a/gps_time_test.go +++ b/gps_time_test.go @@ -63,3 +63,84 @@ func TestString(t *testing.T) { t.Errorf("String() = %v, want %v", got, want) } } + +func TestGpsTime_ToUTC(t *testing.T) { + tests := []struct { + name string + t GpsTime + want time.Time + }{ + {"GPS Datum", GpsTime(gpsDatum), gpsDatum}, + {"Inside leap table", GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)}, + {"Before leap table", GpsTime(time.Date(1970, time.January, 10, 0, 0, 0, 0, time.UTC)), time.Date(1970, time.January, 10, 0, 0, 0, 0, time.UTC)}, + {"After leap table", GpsTime(time.Date(2025, time.July, 14, 0, 0, 0, 0, time.UTC)), time.Date(2025, time.July, 14, 0, 0, 0, 0, time.UTC)}, + {"Before leap", GpsTime(time.Date(2012, time.June, 30, 23, 59, 59, 0, time.UTC)), time.Date(2012, time.June, 30, 23, 59, 59, 0, time.UTC)}, + {"After leap", GpsTime(time.Date(2012, time.July, 1, 0, 0, 0, 0, time.UTC)), time.Date(2012, time.July, 1, 0, 0, 0, 0, time.UTC)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.t.ToUTC(); got != tt.want { + t.Errorf("GpsTime.ToUTC() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGpsTime_Add(t *testing.T) { + tests := []struct { + name string + t GpsTime + d time.Duration + want GpsTime + }{ + {"GPS Datum", GpsTime(gpsDatum), 5 * time.Second, GpsTime(gpsDatum.Add(5 * time.Second))}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.t.Add(tt.d); got != tt.want { + t.Errorf("GpsTime.Add() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGpsTime_Sub(t *testing.T) { + tests := []struct { + name string + t GpsTime + u GpsTime + want time.Duration + }{ + {"GPS Datum", GpsTime(gpsDatum), GpsTime(gpsDatum), 0}, + {"Inside leap table", GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), 0}, + {"Inside leap table vs GPS Datum", GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), GpsTime(gpsDatum), 948731799 * time.Second}, + {"GPS Datum vs Inside leap table", GpsTime(gpsDatum), GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), -948731799 * time.Second}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.t.Sub(tt.u); got != tt.want { + t.Errorf("GpsTime.Sub() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGpsTime_Equal(t *testing.T) { + tests := []struct { + name string + t GpsTime + u GpsTime + want bool + }{ + {"GPS Datum", GpsTime(gpsDatum), GpsTime(gpsDatum), true}, + {"Inside leap table", GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), true}, + {"GPS Datum vs Inside leap table", GpsTime(gpsDatum), GpsTime(time.Date(2010, time.January, 28, 16, 36, 24, 0, time.UTC)), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.t.Equal(tt.u); got != tt.want { + t.Errorf("GpsTime.Equal() = %v, want %v", got, tt.want) + } + }) + } +}