APITime.DateTime

Time.DateTime

An exact moment viewed in a timezone. Equality and ordering compare the moment; sameRepresentation also compares the zone identifier. Calendar calculations use the stored zone, so equal moments in different zones can advance to different moments. Viewing a moment in another zone never fails: see Time.Instant for the supported range that makes this so.

add moves by elapsed time; advance moves local calendar fields. Compatible resolution chooses the earlier overlap and moves forward by a gap. Zero adjustments and unchanged field replacements preserve the original moment. ISO output is the local reading with its offset, which parse accepts, so a round trip keeps the moment and the local view but not an IANA zone name: store that identifier separately when the zone must survive serialization.

import Time.DateTime as DT
import Time.Zone as Zone
import Fail
import Result exposing (Result(..))

result = Fail.attempt {
    local = DT.parse "2026-10-07T09:00:00-03:00"
    utc = DT.inZone Zone.utc local
    (DT.toIso utc, local == utc, DT.sameRepresentation local utc)
    }
result == Ok ("2026-10-07T12:00:00Z", True, False)

DateTime

type DateTimeSource

The moment, its zone, and the local date and clock reading in that zone. The local fields are derived once at construction so accessors are pure.

Instances:

Eq DateTime
Ord DateTime
Show DateTime

add

add : Duration -> DateTime ->{Fail Error} DateTimeSource

Moves by exact elapsed time while retaining the zone. Fails with OutOfRange.

addDays

addDays : Int -> DateTime ->{Fail Error} DateTimeSource

Moves by local calendar days, preserving the clock reading where possible.

addMonths

addMonths : Int -> DateTime ->{Fail Error} DateTimeSource

Moves by calendar months with day clamping.

addWeeks

addWeeks : Int -> DateTime ->{Fail Error} DateTimeSource

Moves by local seven-day weeks.

addYears

addYears : Int -> DateTime ->{Fail Error} DateTimeSource

Moves by calendar years with day clamping.

advance

advance : Period -> DateTime ->{Fail Error} DateTimeSource

Applies calendar months with clamping, then days, resolving once at the end with Compatible resolution. Fails with OutOfRange, or Nonexistent for a skipped date.

advanceWith

advanceWith : Resolution -> Period -> DateTime ->{Fail Error} DateTimeSource

Calendar movement with an explicit resolution policy. Unchanged local fields preserve the original occurrence even during a repeated hour.

beginningOfDay

beginningOfDay : DateTime ->{Fail Error} DateTimeSource

The first valid instant of this local day; fails as startOfDay.

beginningOfMonth

beginningOfMonth : DateTime ->{Fail Error} DateTimeSource

The first valid instant within this local month; fails as startOfDay.

beginningOfQuarter

beginningOfQuarter : DateTime ->{Fail Error} DateTimeSource

The first valid instant within this local quarter; fails as startOfDay.

beginningOfWeek

beginningOfWeek : DateTime ->{Fail Error} DateTimeSource

The first valid instant of this Monday-based week; fails as startOfDay.

beginningOfWeekOn

beginningOfWeekOn : Weekday -> DateTime ->{Fail Error} DateTimeSource

The first valid instant of a week starting on the specified weekday; fails as startOfDay.

beginningOfYear

beginningOfYear : DateTime ->{Fail Error} DateTimeSource

The first valid instant within this local year; fails as startOfDay.

between

between : DateTime -> DateTime -> DurationSource

Signed elapsed duration from the first moment to the second.

date

date : DateTime -> DateSource

Local calendar date in the stored zone.

day

day : DateTime -> IntSource

Local day of month.

dayRange

dayRange : DateTime ->{Fail Error} (DateTime, DateTime)Source

Half-open [start, nextStart) bounds of this local day. Fails with Nonexistent for a skipped date and OutOfRange when the upper bound leaves the supported moments.

format

format : String -> DateTime -> StringSource

Local output formatting: Date and TimeOfDay tokens plus %z and %:z for offsets, including offset seconds when nonzero. Other tokens are copied verbatim.

fromInstant

fromInstant : Zone -> Instant -> DateTimeSource

Views an instant in a zone. Every supported instant has a local date within the calendar for every zone, so this never fails.

hour

hour : DateTime -> IntSource

Local hour.

inZone

inZone : Zone -> DateTime -> DateTimeSource

Preserves the moment while changing its zone and local fields.

make

make : Zone -> Date -> TimeOfDay ->{Fail Error} DateTimeSource

Constructs local fields with Compatible resolution. Fails with Nonexistent for a date the zone skips entirely and OutOfRange at the calendar’s ends.

makeWith

makeWith : Resolution -> Zone -> Date -> TimeOfDay ->{Fail Error} DateTimeSource

Constructs local fields with an explicit transition resolution policy. Reject additionally fails with Ambiguous or Nonexistent at a transition.

minute

minute : DateTime -> IntSource

Local minute.

month

month : DateTime -> IntSource

Local calendar month.

monthRange

monthRange : DateTime ->{Fail Error} (DateTime, DateTime)Source

Half-open bounds of this local month; fails as dayRange.

nanosecond

nanosecond : DateTime -> IntSource

Nanosecond fraction.

offsetSeconds

offsetSeconds : DateTime -> IntSource

The actual offset at this moment, in seconds east of UTC.

parse

parse : String ->{Fail Error} DateTimeSource

Strict ISO timestamp: YYYY-MM-DDTHH:MM:SS[.1–9 digits] followed by Z or ±HH:MM[:SS]. The result carries UTC or a fixed-offset zone. Offsets are required; unknown offset -00:00, leap seconds, 24:00, and moments outside the supported range fail with InvalidText.

quarterRange

quarterRange : DateTime ->{Fail Error} (DateTime, DateTime)Source

Half-open bounds of this local quarter; fails as dayRange.

sameRepresentation

sameRepresentation : DateTime -> DateTime -> BoolSource

Whether both moment and stored zone identifier agree.

second

second : DateTime -> IntSource

Local second.

startOfDay

startOfDay : Zone -> Date ->{Fail Error} DateTimeSource

The first valid instant of a date, which need not be midnight. Fails with Nonexistent for a date the zone skips and OutOfRange at the calendar’s ends.

timeOfDay

timeOfDay : DateTime -> TimeOfDaySource

Local clock reading in the stored zone.

toInstant

toInstant : DateTime -> InstantSource

The moment, without the zone.

toIso

toIso : DateTime -> StringSource

The local reading with its offset, as RFC 3339 text: Z for the UTC zone, otherwise ±HH:MM, extended with :SS for historical sub-minute offsets. Trailing fractional zeros are trimmed. The zone name is not included.

weekday

weekday : DateTime -> WeekdaySource

Local ISO weekday.

weekRange

weekRange : DateTime ->{Fail Error} (DateTime, DateTime)Source

Half-open bounds of this Monday-based week; fails as dayRange.

withDate

withDate : Date -> DateTime ->{Fail Error} DateTimeSource

Replaces the local date, retaining the zone and using Compatible resolution.

withDateWith

withDateWith : Resolution -> Date -> DateTime ->{Fail Error} DateTimeSource

Replaces the local date with an explicit resolution policy; fails as makeWith.

withTime

withTime : TimeOfDay -> DateTime ->{Fail Error} DateTimeSource

Replaces the local clock reading, retaining the zone and resolving compatibly.

withTimeWith

withTimeWith : Resolution -> TimeOfDay -> DateTime ->{Fail Error} DateTimeSource

Replaces the local clock reading with an explicit resolution policy; fails as makeWith.

year

year : DateTime -> IntSource

Local calendar year.

yearRange

yearRange : DateTime ->{Fail Error} (DateTime, DateTime)Source

Half-open bounds of this local year; fails as dayRange.

zone

zone : DateTime -> ZoneSource

The stored timezone rules.