Skip to content

Instantly share code, notes, and snippets.

@cobalamin
Created August 5, 2016 15:06
Show Gist options
  • Select an option

  • Save cobalamin/c2c609b8c98df414683bd2c2d5d50bf0 to your computer and use it in GitHub Desktop.

Select an option

Save cobalamin/c2c609b8c98df414683bd2c2d5d50bf0 to your computer and use it in GitHub Desktop.
Parsing a stopwatch-like time string format in Elm
module ParseStopwatch exposing (..)
import Parser exposing (..)
import Parser.Char exposing (..)
import Parser.Number exposing (digit)
import Char
import Time exposing (..)
twoDigitInt : Parser Int
twoDigitInt =
(map (\tens ones -> tens * 10 + ones)) digit `and` digit
threeDigitInt : Parser Int
threeDigitInt =
(map (\hundreds tens ones -> hundreds * 100 + tens * 10 + ones)) digit `and` digit `and` digit
(**) x y = (toFloat x) * y
sumTimes : Int -> Int -> Int -> Int -> Time
sumTimes h m s ms =
(h ** hour) + (m ** minute) + (s ** second) + (ms ** millisecond)
parseTime : Parser Time
parseTime =
sumTimes
`map` twoDigitInt
<* symbol ':'
`and` twoDigitInt
<* symbol ':'
`and` twoDigitInt
<* symbol ','
`and` threeDigitInt
-- parse parseTime "07:39:01,921" == Ok 27541921
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment