Module

Temporal.Duration

Package
purescript-temporal
Repository
philippedev101/purescript-temporal

Construction, arithmetic, and inspection of Duration values.

A Duration represents a length of time as a combination of date and time fields (years through nanoseconds). Key things to know:

  • Same-sign rule: all components must be zero or share the same sign. Constructing { years: 1, months: -1 } will fail.
  • Unbalanced by default: { hours: 36 } stays as-is and does not auto-convert to { days: 1, hours: 12 }. Use round to rebalance.
  • Eq is structural: hours 1.0 /= minutes 60.0 because Eq compares component-by-component, not total elapsed time.
  • Calendar operations need context: round, total, and add/subtract may return Nothing when calendar units (years, months, weeks) are involved, because their real-world length depends on a reference date.

#DurationFields Source

type DurationFields = { days :: Number, hours :: Number, microseconds :: Number, milliseconds :: Number, minutes :: Number, months :: Number, nanoseconds :: Number, seconds :: Number, weeks :: Number, years :: Number }

All fields of a Duration. Every field defaults to 0.0 — set only the fields you need.

All non-zero fields must share the same sign, or construction will fail.

#defaultDurationFields Source

defaultDurationFields :: DurationFields

All fields set to 0.0. Use record update syntax:

duration (defaultDurationFields { hours = 2.0, minutes = 30.0 })

#duration Source

duration :: DurationFields -> Maybe Duration

Construct a Duration from explicit fields. Returns Nothing if the fields have mixed signs (e.g. positive years with negative months).

#fromString Source

fromString :: String -> Maybe Duration

Parse an ISO 8601 duration string (e.g. "PT1H30M", "P1Y2M3D"). Returns Nothing for invalid strings.

Note: subsecond components are collapsed during serialization, so fromString (toString d) may produce a structurally different (but equal) duration. For example, { milliseconds: 1000 } serializes to "PT1S" and parses back as { seconds: 1 }.

#years Source

years :: Number -> Duration

Create a duration of the given number of years. Always succeeds for finite values.

#months Source

months :: Number -> Duration

Create a duration of the given number of months.

#weeks Source

weeks :: Number -> Duration

Create a duration of the given number of weeks.

#days Source

days :: Number -> Duration

Create a duration of the given number of days.

#hours Source

hours :: Number -> Duration

Create a duration of the given number of hours.

#minutes Source

minutes :: Number -> Duration

Create a duration of the given number of minutes.

#seconds Source

seconds :: Number -> Duration

Create a duration of the given number of seconds.

#milliseconds Source

milliseconds :: Number -> Duration

Create a duration of the given number of milliseconds.

#microseconds Source

microseconds :: Number -> Duration

Create a duration of the given number of microseconds.

#nanoseconds Source

nanoseconds :: Number -> Duration

Create a duration of the given number of nanoseconds.

#getMilliseconds Source

#getMicroseconds Source

#sign Source

sign :: Duration -> Int

The sign of the duration: 1 for positive, -1 for negative, 0 for blank.

#blank Source

blank :: Duration -> Boolean

Whether all fields are zero.

#add Source

add :: Duration -> Duration -> Maybe Duration

Add two durations. Returns Nothing if the durations involve calendar units (years, months, weeks) — use date arithmetic with a reference date instead: startDate # add dur1 >>= add dur2 >>= \d -> since' d startDate.

#subtract Source

subtract :: Duration -> Duration -> Maybe Duration

Subtract one duration from another. Same calendar-unit caveats as add.

#negated Source

negated :: Duration -> Duration

Return the duration with all component signs flipped. negated (negated d) == d for all durations.

#abs Source

abs :: Duration -> Duration

Return the duration with all components made non-negative. abs d == abs (negated d) for all durations.

#DurationRoundOptions Source

type DurationRoundOptions = { largestUnit :: DateTimeUnit, roundingIncrement :: Int, roundingMode :: RoundingMode, smallestUnit :: DateTimeUnit }

Options for round. The roundingIncrement must evenly divide the next-larger unit's maximum (e.g. for minutes: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30).

#round Source

round :: DurationRoundOptions -> Duration -> Maybe Duration

Round and/or rebalance a duration. Returns Nothing if calendar units are involved (this binding does not support the relativeTo option).

To rebalance without rounding, set largestUnit to the desired largest unit:

round (defaultDurationRoundOptions { largestUnit = TimeU Hours }) (minutes 90.0)
-- Just (Duration PT2H) — rebalanced from 90 minutes to 1h30m, then rounded to hours

#total Source

total :: DateTimeUnit -> Duration -> Maybe Number

Compute the total duration in the given unit as a fractional number. Returns Nothing if calendar units are involved (this binding does not support the relativeTo option).

total (TimeU Minutes) (hours 1.0)  -- Just 60.0

#toString Source

toString :: Duration -> String

Serialize to an ISO 8601 duration string (e.g. "PT1H30M").

Re-exports from Temporal.Internal.Types

#Duration Source

data Duration

A 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 }.

Instances