jxxcarlson / elm-typed-time / 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 TypedTime

The data type for the package


type Unit
    = Milliseconds
    | Seconds
    | Minutes
    | Hours

Constructors

hours : Basics.Float -> TypedTime

hours 1.5 |> toString Minutes
--> "01:30"

minutes : Basics.Float -> TypedTime

minutes 14.5 |> toString Seconds
--> "00:14:30"

seconds : Basics.Float -> TypedTime

seconds 44 |> toString Seconds
--> "00:00:44"

milliseconds : Basics.Float -> TypedTime

milliseconds 1000  |> toString Seconds
--> "00:00:01"

zero : TypedTime

The zero element for TypedTime.

Comparison

equal : TypedTime -> TypedTime -> Basics.Bool

equal (milliseconds 1000) (seconds 1)
--> True

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

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

gt : TypedTime -> TypedTime -> Basics.Bool

Greater than:

gt (seconds 1.0001) (seconds 1)
--> True

gte : TypedTime -> TypedTime -> Basics.Bool

Greater than or equal to:

gte (seconds 1) (seconds 1)
--> True

gte (seconds 1.001) (seconds 1)
--> True

lt : TypedTime -> TypedTime -> Basics.Bool

Les than:

lt (seconds 1) (seconds 1.001)
--> True

lte : TypedTime -> TypedTime -> Basics.Bool

Less than or equal to:

lte (seconds 1) (seconds 1)
--> True

lte (seconds 1) (seconds 1.001)
--> True

Operators

common : Unit -> Unit -> Unit

common Seconds Minutes
--> Seconds

multiply : Basics.Float -> TypedTime -> TypedTime

equal (multiply 0.5 (minutes 1)) (seconds 30)
--> True

divide : Basics.Float -> TypedTime -> TypedTime

equal (divide 0.5 (minutes 1)) (seconds 120)
--> 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 -> Basics.Float

abs ((ratio (minutes 1) (seconds 60)) - 1.0) < 0.000001
--> True

Conversion

toSeconds : TypedTime -> Basics.Float

abs( (toSeconds (seconds 1)) - 1.0) < 0.00001
--> True

abs( (toSeconds (minutes 1)) - 60.0) < 0.00001
--> True

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

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)