Skip to content

Commit

Permalink
Add ToUTC(), Sub() and Equal() functions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
osechet authored Nov 17, 2020
1 parent 2330350 commit d32cc55
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
5 changes: 1 addition & 4 deletions gps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions gps_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
81 changes: 81 additions & 0 deletions gps_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit d32cc55

Please sign in to comment.