Skip to content

Commit

Permalink
Pace fix for small distances
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Oct 14, 2022
1 parent 6322276 commit dcd6f48
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
3 changes: 1 addition & 2 deletions fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"time"

"github.com/tormoder/fit"
)
Expand All @@ -29,7 +28,7 @@ func parseFIT(r io.Reader) ([]*activity, error) {
!includeTimestamp(r0.Timestamp, r1.Timestamp) ||
!includeDuration(dur) ||
!includeDistance(act.distance) ||
!includePace(dur/time.Duration(act.distance)) {
!includePace(dur, act.distance) {
return nil, nil
}
act.records = make([]*record, 0, len(a.Records))
Expand Down
29 changes: 29 additions & 0 deletions fit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"bytes"
"encoding/binary"
"testing"
"time"

"github.com/tormoder/fit"
)

func TestFITShortDistance(t *testing.T) {
w := &bytes.Buffer{}
if f, err := fit.NewFile(fit.FileTypeActivity, fit.NewHeader(fit.V20, false)); err != nil {
t.Fatal(err)
} else {
a, _ := f.Activity()
a.Sessions = append(a.Sessions, &fit.SessionMsg{TotalDistance: 1})
a.Records = append(a.Records, &fit.RecordMsg{Timestamp: time.Now()}, &fit.RecordMsg{Timestamp: time.Now().Add(time.Second)})
if err := fit.Encode(w, f, binary.BigEndian); err != nil {
t.Fatal(err)
}
}
if acts, err := parseFIT(w); err != nil {
t.Fatal(err)
} else if len(acts) != 1 {
t.Fatal("expected 1 activity")
}
}
3 changes: 1 addition & 2 deletions gpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"io"
"strings"
"time"

"github.com/tkrajina/gpxgo/gpx"
)
Expand Down Expand Up @@ -99,7 +98,7 @@ func parseGPX(r io.Reader) ([]*activity, error) {
!includeTimestamp(p0.Timestamp, p1.Timestamp) ||
!includeDuration(dur) ||
!includeDistance(act.distance) ||
!includePace(dur/time.Duration(act.distance)) {
!includePace(dur, act.distance) {
continue
}

Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func parse() error {
if act.distance > maxDist {
maxDist = act.distance
}
pace := dur / time.Duration(act.distance)
pace := time.Duration(float64(dur) / act.distance)
if pace < minP {
minP = pace
}
Expand Down Expand Up @@ -533,7 +533,8 @@ func includeDistance(distance float64) bool {
return true
}

func includePace(pace time.Duration) bool {
func includePace(duration time.Duration, distance float64) bool {
pace := time.Duration(float64(duration) / distance)
if pace == 0 {
return false
}
Expand Down
3 changes: 1 addition & 2 deletions tcx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"time"

"github.com/llehouerou/go-tcx"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func parseTCX(r io.Reader) ([]*activity, error) {
!includeTimestamp(t0.Time, t1.Time) ||
!includeDuration(dur) ||
!includeDistance(act.distance) ||
!includePace(dur/time.Duration(act.distance)) {
!includePace(dur, act.distance) {
continue
}

Expand Down

0 comments on commit dcd6f48

Please sign in to comment.