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 }. Useroundto rebalance. - Eq is structural:
hours 1.0 /= minutes 60.0becauseEqcompares component-by-component, not total elapsed time. - Calendar operations need context:
round,total, andadd/subtractmay returnNothingwhen 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 :: DurationFieldsAll fields set to 0.0. Use record update syntax:
duration (defaultDurationFields { hours = 2.0, minutes = 30.0 })
#duration Source
duration :: DurationFields -> Maybe DurationConstruct 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 DurationParse 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 }.
#milliseconds Source
milliseconds :: Number -> DurationCreate a duration of the given number of milliseconds.
#microseconds Source
microseconds :: Number -> DurationCreate a duration of the given number of microseconds.
#nanoseconds Source
nanoseconds :: Number -> DurationCreate a duration of the given number of nanoseconds.
#getMinutes Source
getMinutes :: Duration -> Number#getSeconds Source
getSeconds :: Duration -> Number#getMilliseconds Source
getMilliseconds :: Duration -> Number#getMicroseconds Source
getMicroseconds :: Duration -> Number#getNanoseconds Source
getNanoseconds :: Duration -> Number#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 DurationRound 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 NumberCompute 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
Re-exports from Temporal.Internal.Types
#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 }.