commit dd49bc50fa81e7f166e47641840af73a338e0d35
parent 5ec4e6be9acea5ac1ba401e23fb50b28290be0bf
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 30 Oct 2024 17:44:40 +0000
parse trkpt timestamp
Diffstat:
4 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
@@ -58,5 +58,6 @@ _Do not look below this line_
* [done] Pos can't include elevation if it's sometimes unknown
* do we even need Track? will it ever be anything more than a collection
of Points?
-* need a real gpx file with namespace decls before we can parse power and stuff
+* [done] need a real gpx file with namespace decls before we can parse power and stuff
* tests seem to pass without <gpx> element?
+* stop returning bogus data when missing required elements (e.g. time)
diff --git a/lib/Track.hs b/lib/Track.hs
@@ -7,10 +7,12 @@ module Track (
elevation,
cadence,
power,
+ time,
parse,
Track.length
) where
import Data.Time
+import qualified Data.Time.ISO8601
import qualified Data.List
import Text.XML
import Text.XML.Cursor as Cursor
@@ -61,6 +63,7 @@ elToPoint c =
lat = getAttr "lat"
lon = getAttr "lon"
ele = child c >>= element (gpxNS "ele") >>= child >>= content
+ ts = child c >>= element (gpxNS "time") >>= child >>= content
gpxtpx = child c >>=
element (gpxNS "extensions")
>>= child
@@ -73,7 +76,11 @@ elToPoint c =
(case ele of
[e] -> Just $ asFloat e
_ -> Nothing)
- (UTCTime (toEnum 60631) 43200)
+ (case ts of
+ [e] -> case Data.Time.ISO8601.parseISO8601 (Data.Text.unpack e) of
+ Just utime -> utime
+ _ -> UTCTime (toEnum 0) 0
+ _ -> UTCTime (toEnum 0) 0)
(case cadence of
[e] -> Just (asInt e)
_ -> Nothing)
diff --git a/souplesse.cabal b/souplesse.cabal
@@ -90,7 +90,7 @@ library souplesse-lib
, time
, containers
, text
- -- , text-iso8601
+ , iso8601-time
default-language: GHC2021
test-suite tests
@@ -102,5 +102,6 @@ test-suite tests
base >=4.7 && <5
, souplesse-lib
, raw-strings-qq
+ , time
, HUnit
default-language: GHC2021
diff --git a/tests/UnitTest.hs b/tests/UnitTest.hs
@@ -9,6 +9,7 @@ import Test.HUnit
import qualified System.Exit as Exit
import Control.Exception
import Debug.Trace (trace, traceShow)
+import qualified Data.Time
preamble = [r|
<?xml version="1.0" encoding="UTF-8"?>
@@ -39,6 +40,7 @@ onepointWithAttrs = wrap [r|
<trkseg>
<trkpt lat="51" lon="-0.1">
<ele>25.2</ele>
+ <time>2024-10-23T08:34:59.779+01:00</time>
<extensions><gpxtpx:TrackPointExtension>
<gpxtpx:speed>2.4</gpxtpx:speed>
<gpxtpx:cad>128</gpxtpx:cad>
@@ -88,8 +90,8 @@ test4 = TestCase $
Left err -> assertFailure (displayException err)
Right (p:ps) ->
assertEqual "matches attributes"
- (Just 25.2, Just 128, Just 55)
- (Track.elevation p, Track.cadence p, Track.power p)
+ (Just 25.2, Just 128, Just 55, Data.Time.UTCTime (toEnum 60606) 27299.779)
+ (Track.elevation p, Track.cadence p, Track.power p, Track.time p)
Right [] -> assertFailure "no points"
tests :: Test