|
| 1 | +# About |
| 2 | + |
| 3 | +The `Time` module is used to display human-readable dates / times. |
| 4 | +While it's technically possibly to manipulate times with `Time`, there are Elm community packages that are far more suited to the task, such as [`justinmimbs/date`][date] or [`CoderDennis/elm-time-format`][elm-time-format]. |
| 5 | + |
| 6 | +## Posix |
| 7 | + |
| 8 | +Times are represented by the opaque type `Posix`, which wraps Unix epoch time, the number of milliseconds passed since January 1st, 1970 at midnight UTC. |
| 9 | +A `Posix` is an non-ambiguous value that does not vary between time zones, therefore a good choice for keeping time. |
| 10 | +In an Elm application, `Posix` times will typically be provided by external sources, such as the browser. |
| 11 | + |
| 12 | +It is possible to transform a `Posix` into a number of millisecond and vice-versa with the following functions: |
| 13 | + |
| 14 | +```elm |
| 15 | +posixToMillis : Posix -> Int |
| 16 | +millisToPosix : Int -> Posix |
| 17 | +``` |
| 18 | + |
| 19 | +## Time Zones |
| 20 | + |
| 21 | +In our day-to-day lives, we humans usually don't read `Posix` and instead prefer local time, which depends on our location on Earth, and local daylight-saving rules. |
| 22 | +In Elm, this information is encoded in the opaque type `Zone`, usually provided by the browser. |
| 23 | + |
| 24 | +There is a special `Zone` that is always accessible: |
| 25 | + |
| 26 | +```elm |
| 27 | +utc : Zone |
| 28 | +``` |
| 29 | + |
| 30 | +## Human times |
| 31 | + |
| 32 | +With a `Posix` and a `Zone`, we have enough information to show local time in any level of details. |
| 33 | +Here is a list of the functions that can be used for that: |
| 34 | + |
| 35 | +```elm |
| 36 | +toYear : Zone -> Posix -> Int |
| 37 | +toMonth : Zone -> Posix -> Month |
| 38 | +toDay : Zone -> Posix -> Int |
| 39 | +toWeekday : Zone -> Posix -> Weekday |
| 40 | +toHour : Zone -> Posix -> Int |
| 41 | +toMinute : Zone -> Posix -> Int |
| 42 | +toSecond : Zone -> Posix -> Int |
| 43 | +toMillis : Zone -> Posix -> Int |
| 44 | +``` |
| 45 | + |
| 46 | +You will have spotted two more `Time` custom types: |
| 47 | + |
| 48 | +```elm |
| 49 | +type Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
| 50 | +type Weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun |
| 51 | +``` |
| 52 | + |
| 53 | +It is left to users to convert these custom types to human-readable values. |
| 54 | + |
| 55 | +[date]: https://package.elm-lang.org/packages/justinmimbs/date/latest |
| 56 | +[elm-time-format]: https://package.elm-lang.org/packages/CoderDennis/elm-time-format/latest |
0 commit comments