arowM / tepa / Tepa.Time

Library for working with time and time zones.

You should not use the Time module with TEPA because the functions that the module exposes cannot recognize the emulated time lapse during Scenario testing.

Time


type alias Posix =
Time.Posix

A computer representation of time. It is the same all over Earth, so if we have a phone call or meeting at a certain POSIX time, there is no ambiguity.

It is very hard for humans to read a POSIX time though, so we use functions like toHour and toMinute to view them.

This is an alias for Time.Posix.

sleep : Basics.Int -> Tepa.Promise m ()

Block progress on the current procedure for the given number of milliseconds. The JavaScript equivalent of this is setTimeout which lets you delay work until later.

This is the TEPA version of Process.sleep.

now : Tepa.Promise m Posix

Get the POSIX time at the moment when this Promise is evaluated.

This is the TEPA version of Time.now.

every : Basics.Int -> (Posix -> List (Tepa.Promise m ())) -> Tepa.Promise m ()

Get the current time periodically at the specified interval in milliseconds (like 1000 for a second or 60 * 1000 for a minute or 60 * 60 * 1000 for an hour).

This is the TEPA version of Time.every.

tick : Basics.Int -> Tepa.Promise m (Tepa.Stream.Stream Posix)

Similar to every, but returns Stream.

The Stream produces current time periodically at the specified interval in milliseconds (like 1000 for a second or 60 * 1000 for a minute or 60 * 60 * 1000 for an hour).

posixToMillis : Posix -> Basics.Int

Turn a Posix time into the number of milliseconds since 1970 January 1 at 00:00:00 UTC. It was a Thursday.

This is same as Time.posixToMillis.

millisToPosix : Basics.Int -> Posix

Turn milliseconds into a Posix time.

This is same as Time.millisToPosix.

Time Zones


type alias Zone =
Time.Zone

Information about a particular time zone.

Refer to the Time.utc documentation for more detailed notes.

See utc, here, and Browser.Env to learn how to obtain Zone values.

This is an alias for Time.Zone.

utc : Zone

The time zone for Coordinated Universal Time ([UTC])

Refer to the Time.utc documentation for more detailed notes.

This is an alias for Time.utc.

here : Tepa.Promise m Zone

Produce a Zone based on the current UTC offset. You can use this to figure

out what day it is where you are:

import Tepa exposing (Promise)
import Tepa.Time as Time

whatDayIsIt : Promise m Int
whatDayIsIt =
    Tepa.suceed Time.toDay
        |> Tepa.sync Time.here
        |> Tepa.sync Time.now

Refer to the Time.here documentation for more detailed notes.

This is the TEPA version of here.

Human Times

toYear : Zone -> Posix -> Basics.Int

What year is it?!

import Tepa.Time exposing (toYear, utc, millisToPosix)

toYear utc (millisToPosix 0) == 1970
toYear nyc (millisToPosix 0) == 1969

-- pretend `nyc` is the `Zone` for America/New_York.

This is an alias for Time.toYear.

toMonth : Zone -> Posix -> Month

What month is it?!

import Tepa.Time exposing (toMonth, utc, millisToPosix)

toMonth utc (millisToPosix 0) == Jan
toMonth nyc (millisToPosix 0) == Dec

-- pretend `nyc` is the `Zone` for America/New_York.

This is an alias for Time.toMonth.

toDay : Zone -> Posix -> Basics.Int

What day is it?! (Days go from 1 to 31)

import Tepa.Time exposing (toDay, utc, millisToPosix)

toDay utc (millisToPosix 0) == 1
toDay nyc (millisToPosix 0) == 31

-- pretend `nyc` is the `Zone` for America/New_York.

This is an alias for Time.toDay.

toWeekday : Zone -> Posix -> Weekday

What day of the week is it?

import Tepa.Time exposing (toWeekday, utc, millisToPosix)

toWeekday utc (millisToPosix 0) == Thu
toWeekday nyc (millisToPosix 0) == Wed

-- pretend `nyc` is the `Zone` for America/New_York.

This is the TEPA version of Time.toWeekday.

toHour : Zone -> Posix -> Basics.Int

What hour is it? (From 0 to 23)

import Tepa.Time exposing (toHour, utc, millisToPosix)

toHour utc (millisToPosix 0) == 0  -- 12am
toHour nyc (millisToPosix 0) == 19 -- 7pm

-- pretend `nyc` is the `Zone` for America/New_York.

This is an alias for Time.toHour.

toMinute : Zone -> Posix -> Basics.Int

What minute is it? (From 0 to 59)

import Tepa.Time exposing (toMinute, utc, millisToPosix)

toMinute utc (millisToPosix 0) == 0

This can be different in different time zones. Some time zones are offset by 30 or 45 minutes!

This is an alias for Time.toMinute.

toSecond : Zone -> Posix -> Basics.Int

What second is it?

import Time exposing (toSecond, utc, millisToPosix)

toSecond utc (millisToPosix    0) == 0
toSecond utc (millisToPosix 1234) == 1
toSecond utc (millisToPosix 5678) == 5

This is an alias for Time.toSecond.

toMillis : Zone -> Posix -> Basics.Int

import Tepa.Time exposing (toMillis, utc, millisToPosix)

toMillis utc (millisToPosix    0) == 0
toMillis utc (millisToPosix 1234) == 234
toMillis utc (millisToPosix 5678) == 678

This is an alias for Time.toMillis.

Weeks and Months


type Weekday
    = Mon
    | Tue
    | Wed
    | Thu
    | Fri
    | Sat
    | Sun

Represents a Weekday so that you can convert it to a String or Int however you please. For example, if you need the Japanese representation, you can say:

toJapaneseWeekday : Weekday -> String
toJapaneseWeekday weekday =
    case weekday of
        Mon ->
            "月"

        Tue ->
            "火"

        Wed ->
            "水"

        Thu ->
            "木"

        Fri ->
            "金"

        Sat ->
            "土"

        Sun ->
            "日"

This is the TEPA version of Time.Weekday.


type Month
    = Jan
    | Feb
    | Mar
    | Apr
    | May
    | Jun
    | Jul
    | Aug
    | Sep
    | Oct
    | Nov
    | Dec

Represents a Month so that you can convert it to a String or Int however you please. For example, if you need the Danish representation, you can say:

toDanishMonth : Month -> String
toDanishMonth month =
    case month of
        Jan ->
            "januar"

        Feb ->
            "februar"

        Mar ->
            "marts"

        Apr ->
            "april"

        May ->
            "maj"

        Jun ->
            "juni"

        Jul ->
            "juli"

        Aug ->
            "august"

        Sep ->
            "september"

        Oct ->
            "oktober"

        Nov ->
            "november"

        Dec ->
            "december"

This is the TEPA version of Time.Month.