jxxcarlson/elm-typed-time/1.0.0

NOTE: the latest version is 1.2.0

TypedTime

This library provides functions for working with typed time. For example,

equal (seconds 60) (minutes 1)
--> True


add (hours 1) (minutes 20) |> toString Minutes
--> "01:20"


add (hours 1) (minutes 20) |> toString Seconds
--> "01:20:00"

Types

type Unit
= Milliseconds
| Seconds
| Minutes
| Hours

Constructors

hours : Float -> TypedTime
hours 1.5 |> toString Minutes
--> "01:30"
minutes : Float -> TypedTime
minutes 14.5 |> toString Seconds
--> "00:14:30"
seconds : Float -> TypedTime
seconds 44 |> toString Seconds
--> "00:00:44"
milliseconds : Float -> TypedTime
milliseconds 1000  |> toString Seconds
--> "00:00:01"

Operators

equal : TypedTime -> TypedTime -> Bool
equal (milliseconds 1000) (seconds 1)
--> True


 equal (hours 1) (minutes 60)
 --> True


 equal (minutes 1) (seconds 60)
 --> True
common Seconds Minutes
--> Seconds
multiply : Float -> TypedTime -> TypedTime
equal (multiply 0.5 (minutes 1)) (seconds 30)
--> True
add : TypedTime -> TypedTime -> TypedTime
equal (add (seconds 60) (minutes 1)) (minutes 2)
--> True
sub : TypedTime -> TypedTime -> TypedTime
equal (sub (seconds 120) (minutes 1)) (minutes 1)
--> True
sum : List TypedTime -> TypedTime
equal (sum [seconds 30, minutes 1]) (milliseconds 90000)
--> True


equal (sum [seconds 30, minutes 1]) (minutes 1.5)
--> True
ratio : TypedTime -> TypedTime -> Float
abs ((ratio (minutes 1) (seconds 60)) - 1.0) < 0.000001
--> True

Conversion

toString : Unit -> TypedTime -> String
toString Seconds (minutes 30)
--> "00:30:00"


toString Seconds (hours 0.5)
--> "00:30:00"


toString Minutes (hours 0.5)
--> "00:30"
fromString : Unit -> String -> Maybe TypedTime
import Parser


fromString Milliseconds "123"
--> Just (milliseconds 123)


fromString Seconds "23"
--> Just  (seconds 23)


fromString Seconds "2.17"
--> Just  (seconds 2.17)


fromString Seconds "4:23"
--> Just (add (minutes 4) (seconds 23))


fromString Seconds "04:03"
--> Just (add (minutes 4) (seconds 3))


fromString Minutes "04:03"
--> Just (add (hours 4) (minutes 3))


fromString Minutes "4.7"
--> Just (minutes 4.7)