arowM / elm-zoned-time / ZonedTime

A module to handle zoned time.

Core


type ZonedTime

fromPosix : Time.Zone -> Time.Posix -> ZonedTime

fromGregorianUtc : { year : Basics.Int, month : Time.Month, day : Basics.Int } -> Maybe ZonedTime

Construct ZonedTime from proleptic Gregorian calendar day in UTC. Invalid values will return Nothing.

import Time

sample : Maybe ZonedTime
sample =
    fromGregorianUtc
        { year = 2000
        , month = Time.Jun
        , day = 10
        }
        |> Maybe.map (addHours 22)
        |> Maybe.map (addMinutes 33)
        |> Maybe.map (addSeconds 44)
        |> Maybe.map (addMillis 55)

Maybe.map toZone sample
--> Just Time.utc

Maybe.map getYear sample
--> Just 2000

Maybe.map getMonth sample
--> Just Time.Jun

Maybe.map getDay sample
--> Just 10

Maybe.map getHour sample
--> Just 22

Maybe.map getMinute sample
--> Just 33

Maybe.map getSecond sample
--> Just 44

Maybe.map getMillis sample
--> Just 55

fromGregorianUtc
    { year = 1999
    , month = Time.Feb
    , day = 29
    }
--> Nothing

now : Task x ZonedTime

toPosix : ZonedTime -> Time.Posix

toZone : ZonedTime -> Time.Zone

Operators

overwriteZone : Time.Zone -> ZonedTime -> ZonedTime

Overwrite time zone. Make sure it will not change internal POSIX time value.

import Time

-- 1970-01-04T13:54:12.123Z
original : ZonedTime
original = fromPosix Time.utc (Time.millisToPosix 309252123)

fooZone : Time.Zone
fooZone = Time.customZone (9 * 60) []

sample1 : ZonedTime
sample1 =
    original
        |> overwriteZone fooZone

sample2 : ZonedTime
sample2 =
    original
        |> addHours -9
        |> overwriteZone fooZone

toZone sample1
--> fooZone

toZone sample2
--> fooZone

getHour sample1
--> 22

getHour sample2
--> 13

mapPosix : (Time.Posix -> Time.Posix) -> ZonedTime -> ZonedTime

Modify internal POSIX time value.

setToMidnight : ZonedTime -> ZonedTime

Set time to midnight of the day in the time zone. Shorthand for resetMillis >> resetSecond >> resetMinute >> resetHour.

import Time

-- 1970-01-04T13:54:12.123Z
original : ZonedTime
original = fromPosix Time.utc (Time.millisToPosix 309252123)

midnight : ZonedTime
midnight =
    setToMidnight original

getYear original
--> 1970

getYear midnight
--> 1970

getMonth original
--> Time.Jan

getMonth midnight
--> Time.Jan

getDay original
--> 4

getDay midnight
--> 4

getHour midnight
--> 0

getMinute midnight
--> 0

getSecond midnight
--> 0

getMillis midnight
--> 0

addDays : Basics.Int -> ZonedTime -> ZonedTime

addHours : Basics.Int -> ZonedTime -> ZonedTime

addMinutes : Basics.Int -> ZonedTime -> ZonedTime

addSeconds : Basics.Int -> ZonedTime -> ZonedTime

addMillis : Basics.Int -> ZonedTime -> ZonedTime

Getters

getYear : ZonedTime -> Basics.Int

getMonth : ZonedTime -> Time.Month

getDay : ZonedTime -> Basics.Int

getWeekday : ZonedTime -> Time.Weekday

getHour : ZonedTime -> Basics.Int

getMinute : ZonedTime -> Basics.Int

getSecond : ZonedTime -> Basics.Int

getMillis : ZonedTime -> Basics.Int

Reset functions

resetHour : ZonedTime -> ZonedTime

Reset the hour part of ZonedTime to zero.

Shorthand for \time -> addHours (negate <| getHour time) time.

resetMinute : ZonedTime -> ZonedTime

Reset the minute part of ZonedTime to zero.

Shorthand for \time -> addMinutes (negate <| getMinute time) time.

resetSecond : ZonedTime -> ZonedTime

Reset the second part of ZonedTime to zero.

Shorthand for \time -> addSeconds (negate <| getSecond time) time.

resetMillis : ZonedTime -> ZonedTime

Reset the millis part of ZonedTime to zero.

Shorthand for \time -> addMillis (negate <| getMillis time) time.

Utilities

isLeapYear : Basics.Int -> Basics.Bool

Check if the year of given number is a leap year.

daysInYear : Basics.Int -> Basics.Int

Return the number of days in the year specified as an argument.

daysInMonth : Basics.Int -> Time.Month -> Basics.Int

Return the number of days in the month specified by the given year and month arguments.