parse trkpt timestamp

This commit is contained in:
Daniel Barlow 2024-10-30 17:44:40 +00:00
parent 5ec4e6be9a
commit dd49bc50fa
4 changed files with 16 additions and 5 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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