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.
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"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}Create a Time instance from a Posix timestamp.
import Time
fromPosix Time.utc (Time.millisToPosix (12 * 60 * 60 * 1000))
|> toIsoString
--> "12:00"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 }Create a Time instance from duration from the start of the day in milliseconds.
fromMillis (12 * 60 * 60 * 1000)
|> toIsoString
--> "12:00"Create a Time instance from duration from the start of the day in seconds.
fromSeconds (12 * 60 * 60)
|> toIsoString
--> "12:00"Extracts the milliseconds from a Time
fromParts {hours = 12, minutes = 30, seconds = 5, millis = 500}
|> millis
--> 500Extracts 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}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"Convert time to milliseconds ellapsed from the start of the day
fromParts {hours = 1, minutes = 0, seconds = 1, millis = 500}
|> toMillis
--> 3601500Convert time to seconds ellapsed from the start of the day
fromParts {hours = 1, minutes = 0, seconds = 1, millis = 500}
|> toSeconds
--> 3601Format 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"Represent a supported language
Format a time using a string as a template, with a specific language.
Represent a unit of time
Used in functions to construct explicit time intervals.
Get past or future time by adding a number of units to it.
time ( 9, 0, 0 )
|> add Hours 1
|> toIsoString
--> "10:00"Get the difference between two times, as a number of whole units.
diff Minutes
(time ( 10, 0, 0 ))
(time ( 8, 0, 0 ))
--> 120Represent a time interval
Hour -- 1 hour
Half -- 30 minutes
Quarter -- 15 minutes
Minute -- 1 minute
Second -- 1 second
Millisecond -- 1 millisecondRound 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"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.
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" ]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 ))
--> TrueGet the smaller of two times.
Hour.min (time ( 8, 0, 0 )) (time ( 9, 0, 0 ))
|> toIsoString
--> "08:00"Get the larger of two times.
Hour.max (time ( 8, 0, 0 )) (time ( 9, 0, 0 ))
|> toIsoString
--> "09:00"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
Represents a local time