aboutsummaryrefslogtreecommitdiff
path: root/src/Edition/Model/Time.elm
blob: 35971c3584615253e18dd263192beb40f05dfe4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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