fabiommendes/elm-hour/1.0.1

Hour

This module provides a representation of local times in Elm.

It is called Hour to avoid confusion with elm/time module and to live in a different namespace.

The API is based on justinmimbs/date, and both packages work well together to represent both dates and times.

Types

type Time
= T
{ hours : Int
, minutes : Int
, seconds : Int
, millis : Int
}

Represents a local time

Create

now : Task x Time

Gets the current time in the local timezone.

time : ( Int, Int, Int ) -> Time

Creates a new Time instance from a tuple of hours, minutes, and seconds.

Values are normalized to the range of 0-23 for hours, 0-59 for minutes, and 0-59 for seconds.

time (13, 5, 0)
    |> toIsoString
--> "13:05"

Out or range values are normalized:

time (12, 60, 60)
    |> toIsoString
--> "13:01"
{ hours : Int
, minutes : Int
, seconds : Int
, millis : Int
}
-> Time

Creates a new Time instance with milliseconds.

fromParts { hours = 13, minutes = 5, seconds = 0 , millis = 1}
    |> toIsoString
--> "13:05:00.001"

All values are normalized, so you can pass numbers in invalid ranges and they will wrap up or down to correct values.

fromParts {hours = -2, minutes = 100, seconds = 61, millis = 1500}
    |> parts
    --> {hours = 23, minutes = 41, seconds = 2, millis = 500}
fromPosix : Zone -> Posix -> Time

Create a Time instance from a Posix timestamp.

import Time


fromPosix Time.utc (Time.millisToPosix (12 * 60 * 60 * 1000))
    |> toIsoString
--> "12:00"
fromIsoString : String -> Maybe Time

Parse iso8601 time string to Time.

Expect hours in the format "HH:MM:SS" or "HH:MM:SS.mmm".

fromIsoString "12:30:05.500"
    |> Maybe.map parts
--> Just { hours = 12, minutes = 30, seconds = 5, millis = 500 }
fromMillis : Int -> Time

Create a Time instance from duration from the start of the day in milliseconds.

fromMillis (12 * 60 * 60 * 1000)
    |> toIsoString
--> "12:00"
fromSeconds : Int -> Time

Create a Time instance from duration from the start of the day in seconds.

fromSeconds (12 * 60 * 60)
    |> toIsoString
--> "12:00"

Extract

hours : Time -> Int

Extracts the hours from a Time

time (12, 30, 5)
    |> hours
--> 12
minutes : Time -> Int

Extracts the minutes from a Time

time (12, 30, 5)
        |> minutes
    --> 30
seconds : Time -> Int

Extracts the seconds from a Time

time (12, 30, 5)
        |> seconds
    --> 5
millis : Time -> Int

Extracts the milliseconds from a Time

fromParts {hours = 12, minutes = 30, seconds = 5, millis = 500}
        |> millis
    --> 500
-> { hours : Int
, minutes : Int
, seconds : Int
, millis : Int
}

Extracts all components of a Time as a record

fromParts {hours = 12, minutes = 30, seconds = 5, millis = 500}
        |> parts
    --> {hours = 12, minutes = 30, seconds = 5, millis = 500}

Conversions

toIsoString : Time -> String

Converts a Time to ISO 8601 strings.

Times are rendered as "HH:MM:SS.mmm", where the seconds and milliseconds parts are ommited if possible.

time (12, 30, 5)
    |> toIsoString
--> "12:30:05"


time (12, 30, 0)
    |> toIsoString
--> "12:30"
toMillis : Time -> Int

Convert time to milliseconds ellapsed from the start of the day

fromParts {hours = 1, minutes = 0, seconds = 1, millis = 500}
    |> toMillis
--> 3601500
toSeconds : Time -> Int

Convert time to seconds ellapsed from the start of the day

fromParts {hours = 1, minutes = 0, seconds = 1, millis = 500}
    |> toSeconds
--> 3601

Formatting

format : String -> Time -> String

Format a time using a string as a template.

format "EEEE, d MMMM y" (fromOrdinalDate 1970 1)
    == "Thursday, 1 January 1970"

Alphabetic characters in the template represent time information; the number of times a character is repeated specifies the form of a name or the padding of a number.

Alphabetic characters can be escaped within single-quotes; a single-quote can be escaped as a sequence of two single-quotes, whether appearing inside or outside an escaped sequence.

Templates are based on Date Format Patterns in Unicode Technical Standard #35. Only the following subset of formatting characters are available:

```
"h" -- The hour, using a 12-hour clock from 1 to 12.
"hh" -- The hour, using a 12-hour clock from 01 to 12.
"H" -- The hour, using a 24-hour clock from 0 to 23.
"HH" -- The hour, using a 24-hour clock from 00 to 23.
"m" -- The minute, from 0 through 59.
"mm" -- The minute, from 00 through 59.
"s" -- The second, from 0 through 59.
"ss" -- The second, from 00 through 59.
"a" -- The first character of the AM/PM designator.
"aa" -- The AM/PM designator.
"f" -- The tenths of a second in a date and time value.
"ff" -- The hundredths of a second in a date and time value.
"fff" -- The milliseconds in a date and time value.
```


format "hh:mmaa 'and' s 'seconds'" (time (13, 15, 42))
    --> "01:15pm. and 42 seconds"
type alias Language =
{ am : String
, pm : String
}

Represent a supported language

formatWithLanguage : Language -> String -> Time -> String

Format a time using a string as a template, with a specific language.

Arithmetic

type Unit
= Hours
| Minutes
| Seconds
| Milliseconds

Represent a unit of time

Used in functions to construct explicit time intervals.

add : Unit -> Int -> Time -> Time

Get past or future time by adding a number of units to it.

time ( 9, 0, 0 )
    |> add Hours 1
    |> toIsoString
--> "10:00"
diff : Unit -> Time -> Time -> Int

Get the difference between two times, as a number of whole units.

diff Minutes
    (time ( 10, 0, 0 ))
    (time ( 8, 0, 0 ))
    --> 120

Rounding and intervals

= Hour
| Half
| Quarter
| Minute
| Second
| Millisecond

Represent a time interval

    Hour -- 1 hour
    Half -- 30 minutes
    Quarter -- 15 minutes
    Minute -- 1 minute
    Second -- 1 second
    Millisecond -- 1 millisecond

Round up a time to the beginning of the closest interval.

The resulting time will be greater than or equal to the one provided.

Any fractional time will be rounded up to the next whole interval.

time ( 9, 0, 1 )
    |> Hour.ceiling Hour
    |> toIsoString
--> "10:00"

But exact times will be unchanged:

time ( 9, 0, 0 )
    |> Hour.ceiling Hour
    |> toIsoString
--> "09:00"

Round down a time to the beginning of the closest interval.

The resulting time will be less than or equal to the one provided.

time ( 9, 20, 1 )
    |> Hour.floor Quarter
    |> toIsoString
--> "09:15"
range : Interval -> Int -> Time -> Time -> List Time

Create a list of times, at rounded intervals, increasing by a step value, between two times.

The list will start on or after the first time, and end before the second time.

range Half 3 (time ( 9, 0, 0 )) (time ( 17, 0, 0 ))
    |> List.map toIsoString
    --> [ "09:00", "10:30", "12:00", "13:30", "15:00", "16:30" ]

Notice the final time is not present in the list.

Ordering

compare : Time -> Time -> Order

Compare two times. This can be used as the compare function for List.sortWith

List.sortWith Hour.compare [ time ( 9, 0, 0 ), time ( 8, 0, 0 ) ]
    |> List.map toIsoString
--> [ "08:00", "09:00" ]
isBetween : Time -> Time -> Time -> Bool

Test if a time is within a range, inclusive, of the range values.

time ( 8, 30, 0 )
    |> isBetween (time ( 8, 0, 0 )) (time ( 9, 0, 0 ))
--> True
min : Time -> Time -> Time

Get the smaller of two times.

Hour.min (time ( 8, 0, 0 )) (time ( 9, 0, 0 ))
    |> toIsoString
--> "08:00"
max : Time -> Time -> Time

Get the larger of two times.

Hour.max (time ( 8, 0, 0 )) (time ( 9, 0, 0 ))
    |> toIsoString
--> "09:00"
clamp : Time -> Time -> Time -> Time

Clamp a time within a range.

clamp start end appointment
    (time ( 9, 0, 0 )) -- start
    (time ( 17, 0, 0 )) -- end
    (time ( 18, 0, 0 )) -- appointment
    |> toIsoString
-- 17:00:00

Encoding

decoder : Decoder Time

JSON decoder for Time

encode : Time -> Value

JSON encoder for Time