JS.Temporal.Duration
- Package
- purescript-js-temporal
- Repository
- pete-murphy/purescript-js-temporal
A duration representing the difference between two time points. Can be used in date/time arithmetic. Represented as years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.
Calendar durations (years, months, weeks) require a reference date for
arithmetic; use PlainDate.add, PlainDate.subtract, PlainDateTime.add,
or PlainDateTime.subtract for those. Non-calendar
durations can be added/subtracted directly.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration
#new Source
new :: forall provided rest. Union provided rest DurationComponents => Record provided -> Effect DurationCreates a Duration from component fields. At least one component must be provided. Mixed signs are invalid.
locale <- JS.Intl.Locale.new_ "en-US"
twoHours <- Duration.new { hours: 2 }
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter twoHours)
2 hours
#from Source
from :: String -> Effect DurationParses an ISO 8601 duration string (e.g. "PT1H30M"). Throws on invalid
input. Corresponds to Temporal.Duration.from().
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.from "PT2H30M"
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter duration)
2 hours, 30 minutes
#milliseconds Source
milliseconds :: Duration -> IntMillisecond component of the duration.
#microseconds Source
microseconds :: Duration -> IntMicrosecond component of the duration.
#nanoseconds Source
nanoseconds :: Duration -> IntNanosecond component of the duration.
#add Source
add :: Duration -> Duration -> Effect DurationAdds two durations. Result is balanced to the largest unit of the inputs.
Throws if either duration contains calendar units (years, months, weeks).
Corresponds to Temporal.Duration.prototype.add().
locale <- JS.Intl.Locale.new_ "en-US"
twoHours <- Duration.new { hours: 2 }
thirtyMinutes <- Duration.new { minutes: 30 }
combined <- Duration.add twoHours thirtyMinutes
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter combined)
2 hours, 30 minutes
#subtract Source
subtract :: Duration -> Duration -> Effect DurationSubtracts the second duration from the first. Same balancing/constraints as add.
Corresponds to Temporal.Duration.prototype.subtract().
locale <- JS.Intl.Locale.new_ "en-US"
threeHours <- Duration.new { hours: 3 }
oneHour <- Duration.new { hours: 1 }
remainder <- Duration.subtract threeHours oneHour
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter remainder)
-2 hours
#with Source
with :: forall provided rest. Union provided rest DurationComponents => Record provided -> Duration -> Effect DurationReturns a new duration with specified fields replaced. Mixed signs invalid.
Corresponds to Temporal.Duration.prototype.with().
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.new { hours: 2, minutes: 30 }
updated <- Duration.with { minutes: 45 } duration
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter updated)
2 hours, 45 minutes
#compare Source
compare :: Duration -> Duration -> Effect OrderingCompares two durations. Returns LT if first is shorter, GT if longer, EQ if equal.
Throws for calendar durations without relativeTo. Corresponds to
Temporal.Duration.compare().
shorter <- Duration.new { hours: 1 }
longer <- Duration.new { hours: 2 }
ordering <- Duration.compare longer shorter
Console.log ("Comparison result: " <> show ordering)
Comparison result: GT
#round Source
round :: forall provided. ConvertOptionsWithDefaults ToDurationRoundOptions (Record DurationRoundOptions) (Record provided) (Record DurationRoundOptions) => Record provided -> Duration -> Effect DurationRounds the duration to the given smallest/largest units. Use relativeTo for
calendar durations. Corresponds to Temporal.Duration.prototype.round().
roundedSource <- Duration.new { hours: 1, minutes: 30, seconds: 45 }
rounded <- Duration.round { smallestUnit: TemporalUnit.Minute } roundedSource
Console.log (Duration.toString_ rounded)
PT1H31M
#total Source
total :: forall provided. ConvertOptionsWithDefaults ToDurationTotalOptions (Record DurationTotalOptions) (Record provided) (Record DurationTotalOptions) => Record provided -> Duration -> Effect NumberReturns the total length of the duration in the given unit. Use relativeTo
for calendar durations. Corresponds to Temporal.Duration.prototype.total().
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.new { hours: 2, minutes: 30 }
totalHours <- Duration.total { unit: TemporalUnit.Hour } duration
numberFormatter <- JS.Intl.NumberFormat.new [ locale ] { minimumFractionDigits: 1, maximumFractionDigits: 1 }
Console.log ("Total hours: " <> JS.Intl.NumberFormat.format numberFormatter totalHours)
Total hours: 2.5
#DurationRoundOptions Source
type DurationRoundOptions :: Row Typetype DurationRoundOptions = (largestUnit :: String, relativeTo :: Foreign, roundingIncrement :: Int, roundingMode :: String, smallestUnit :: String)
Options for round: largestUnit, smallestUnit,
roundingIncrement, roundingMode,
relativeTo (PlainDate, PlainDateTime, or ZonedDateTime for calendar
units).
#ToDurationRoundOptions Source
data ToDurationRoundOptionsInstances
ConvertOption ToDurationRoundOptions "largestUnit" TemporalUnit StringConvertOption ToDurationRoundOptions "largestUnit" String StringConvertOption ToDurationRoundOptions "smallestUnit" TemporalUnit StringConvertOption ToDurationRoundOptions "smallestUnit" String StringConvertOption ToDurationRoundOptions "roundingIncrement" Int IntConvertOption ToDurationRoundOptions "roundingMode" RoundingMode StringConvertOption ToDurationRoundOptions "roundingMode" String StringConvertOption ToDurationRoundOptions "relativeTo" Foreign ForeignConvertOption ToDurationRoundOptions "relativeTo" String Foreign
#DurationTotalOptions Source
type DurationTotalOptions :: Row Typetype DurationTotalOptions = (relativeTo :: Foreign, unit :: String)
Options for total: unit (required), relativeTo for calendar units.
#ToDurationTotalOptions Source
data ToDurationTotalOptionsInstances
ConvertOption ToDurationTotalOptions "unit" TemporalUnit StringConvertOption ToDurationTotalOptions "unit" String StringConvertOption ToDurationTotalOptions "relativeTo" Foreign ForeignConvertOption ToDurationTotalOptions "relativeTo" String Foreign
#DurationToStringOptions Source
type DurationToStringOptions :: Row Typetype DurationToStringOptions = (fractionalSecondDigits :: Foreign, smallestUnit :: String)
Options: fractionalSecondDigits, smallestUnit.
#ToDurationToStringOptions Source
data ToDurationToStringOptionsInstances
ConvertOption ToDurationToStringOptions "fractionalSecondDigits" Int ForeignConvertOption ToDurationToStringOptions "fractionalSecondDigits" String ForeignConvertOption ToDurationToStringOptions "smallestUnit" TemporalUnit StringConvertOption ToDurationToStringOptions "smallestUnit" String String
#toString Source
toString :: forall provided. ConvertOptionsWithDefaults ToDurationToStringOptions (Record DurationToStringOptions) (Record provided) (Record DurationToStringOptions) => Record provided -> Duration -> StringSerializes the duration to ISO 8601 format (e.g. "PT1H30M").
duration <- Duration.new { hours: 2, minutes: 30, seconds: 15, milliseconds: 400 }
Console.log (Duration.toString { smallestUnit: TemporalUnit.Second } duration)
PT2H30M15S
#fromMilliseconds Source
fromMilliseconds :: Milliseconds -> Effect DurationCreates a Temporal Duration from purescript-datetime Milliseconds. See
./docs/purescript-datetime-interop.md.
#toMilliseconds Source
toMilliseconds :: Duration -> Maybe MillisecondsConverts a Temporal Duration to purescript-datetime Milliseconds. Returns
Nothing if the duration contains calendar units (years, months, weeks).
Microseconds and nanoseconds are dropped. See
./docs/purescript-datetime-interop.md.
Re-exports from JS.Temporal.Duration.Internal
- Modules
- JS.
Temporal. Duration - JS.
Temporal. Duration. Internal - JS.
Temporal. Instant - JS.
Temporal. Instant. Internal - JS.
Temporal. Internal - JS.
Temporal. Now - JS.
Temporal. Options. CalendarName - JS.
Temporal. Options. Disambiguation - JS.
Temporal. Options. OffsetDisambiguation - JS.
Temporal. Options. Overflow - JS.
Temporal. Options. RoundingMode - JS.
Temporal. Options. TemporalUnit - JS.
Temporal. PlainDate - JS.
Temporal. PlainDate. Internal - JS.
Temporal. PlainDateTime - JS.
Temporal. PlainDateTime. Internal - JS.
Temporal. PlainMonthDay - JS.
Temporal. PlainMonthDay. Internal - JS.
Temporal. PlainTime - JS.
Temporal. PlainTime. Internal - JS.
Temporal. PlainYearMonth - JS.
Temporal. PlainYearMonth. Internal - JS.
Temporal. ZonedDateTime - JS.
Temporal. ZonedDateTime. Internal