Temporal.Instant
- Package
- purescript-temporal
- Repository
- philippedev101/purescript-temporal
Construction, arithmetic, and inspection of Instant values.
An Instant represents an exact moment on the UTC timeline with nanosecond
precision, independent of any time zone or calendar.
Key things to know:
- Time-only arithmetic:
addandsubtractonly accept durations with time components (hours and below). Passing years, months, weeks, or days returnsNothing, because their real-world length varies with time zone and calendar. Convert toZonedDateTimefirst for calendar arithmetic. - Parsing requires UTC offset:
fromString "2024-03-15T10:30:00"returnsNothing— you must includeZor an offset like+00:00. - Diff returns time-only:
since/untilreturn durations withHoursas the default largest unit. The result may have hours exceeding 24.
#fromEpochNanoseconds Source
fromEpochNanoseconds :: BigInt -> Maybe InstantConstruct an Instant from nanoseconds since the Unix epoch.
Returns Nothing if the value is outside the representable range
(approximately ±10^8 days from epoch).
#fromEpochMilliseconds Source
fromEpochMilliseconds :: Number -> Maybe InstantConstruct an Instant from milliseconds since the Unix epoch.
Returns Nothing if the value is outside the representable range.
The fractional part is truncated (not rounded).
#fromString Source
fromString :: String -> Maybe InstantParse an ISO 8601 string with a UTC offset (e.g. "2024-03-15T10:30:00Z",
"2024-03-15T10:30:00+05:30"). Returns Nothing for invalid strings or
strings without a UTC offset.
#getEpochMilliseconds Source
getEpochMilliseconds :: Instant -> NumberMilliseconds since the Unix epoch. This may lose sub-millisecond precision.
#getEpochNanoseconds Source
getEpochNanoseconds :: Instant -> BigIntNanoseconds since the Unix epoch (full precision).
#InstantDiffOptions Source
type InstantDiffOptions = { largestUnit :: TimeUnit, roundingIncrement :: Int, roundingMode :: RoundingMode, smallestUnit :: TimeUnit }Options for since and until. Only time units (hours and below) are
allowed. Default: largest unit is Hours, smallest unit is Nanoseconds.
#since Source
since :: InstantDiffOptions -> Instant -> Instant -> Durationsince opts a b returns the duration from b to a
(i.e., a.since(b) in JS). The result is positive when a is after b.
#until Source
until :: InstantDiffOptions -> Instant -> Instant -> Durationuntil opts a b returns the duration from a to b
(i.e., a.until(b) in JS). The result is positive when b is after a.
#InstantRoundOptions Source
type InstantRoundOptions = { roundingIncrement :: Int, roundingMode :: RoundingMode, smallestUnit :: TimeUnit }Options for round. Only time units are allowed.
#round Source
round :: InstantRoundOptions -> Instant -> InstantRound the instant to the given precision.
#toZonedDateTimeISO Source
toZonedDateTimeISO :: String -> Instant -> ZonedDateTimeConvert to a ZonedDateTime in the given IANA time zone using the
ISO 8601 calendar.
toZonedDateTimeISO "America/New_York" instant
Re-exports from Temporal.Internal.Types
#ZonedDateTime Source
data ZonedDateTimeA date and time in a specific time zone, with full awareness of UTC offset and DST transitions (e.g. 2024-03-15T10:30:00-04:00[America/New_York]).
This is the richest Temporal type. Use it for scheduling future events
where wall-clock time should be preserved even if DST rules change.
For past events or timestamps, Instant is usually sufficient.
Instances
#Instant Source
data InstantAn exact moment on the UTC timeline with nanosecond precision, independent of any time zone or calendar.
Use Instant for timestamps, event logs, and any case where you need
an unambiguous point in time. Convert to ZonedDateTime when you need
wall-clock representation in a specific time zone.
Instances
#Duration Source
data DurationA length of time expressed as a combination of date and time components (years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). All components must share the same sign.
Durations are unbalanced — { hours: 36 } is not automatically
converted to { days: 1, hours: 12 }. Use round to balance.
Eq compares component-by-component (not total elapsed time), so
{ hours: 1 } is not equal to { minutes: 60 }.