aboutsummaryrefslogtreecommitdiff
path: root/src/Edition/Model/Time.elm
diff options
context:
space:
mode:
authorJoris2016-09-04 21:21:11 +0200
committerJoris2016-09-04 21:21:31 +0200
commit973a039b54327df74396605410ea9abe19c8a4e7 (patch)
treec702564d17e0a490d56845027238eb4f231be785 /src/Edition/Model/Time.elm
parent62fee9133f36f655c1ed83e0c2e85394f9948bf5 (diff)
Upgrade to elm 0.17.1
Diffstat (limited to 'src/Edition/Model/Time.elm')
-rw-r--r--src/Edition/Model/Time.elm52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/Edition/Model/Time.elm b/src/Edition/Model/Time.elm
new file mode 100644
index 0000000..35971c3
--- /dev/null
+++ b/src/Edition/Model/Time.elm
@@ -0,0 +1,52 @@
+module Edition.Model.Time exposing
+ ( keyCodeToChar
+ , toTime
+ , toMinutesAndSeconds
+ )
+
+import Time exposing (Time)
+import List
+import Array
+import String
+import Char
+
+import Model.Keyboard exposing (KeyCode)
+import Utils.List exposing (..)
+import Utils.Maybe exposing (..)
+
+keyCodeToChar : KeyCode -> Maybe Char
+keyCodeToChar code =
+ let char = Char.fromCode code
+ in if Char.isDigit char
+ then Just char
+ else Nothing
+
+toTime : List Char -> Time
+toTime numbers =
+ numbers
+ |> toMinutesAndSeconds
+ |> \(a, b) -> (stringToInt a, stringToInt b)
+ |> \(minutes, seconds) -> (toFloat minutes) * 60 * 1000 + (toFloat seconds) * 1000
+
+toMinutesAndSeconds : List Char -> (String, String)
+toMinutesAndSeconds numbers =
+ numbers
+ |> List.take 4
+ |> List.reverse
+ |> completeBegin '0' 4
+ |> splitAt 2
+ |> \(a, b) -> (String.fromList a, String.fromList b)
+
+completeBegin : a -> Int -> List a -> List a
+completeBegin x count xs =
+ let length = List.length xs
+ in List.append (repeat (count - length) x) xs
+
+stringToInt : String -> Int
+stringToInt str =
+ str
+ |> String.toInt
+ |> \res ->
+ case res of
+ Ok n -> n
+ Err _ -> 0