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
#from Source
from :: 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.
exampleFrom :: Effect Unit
exampleFrom = do
locale <- JS.Intl.Locale.new_ "en-US"
twoHours <- Duration.from { hours: 2 }
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter twoHours)
2 hours
#fromString Source
fromString :: String -> Effect DurationParses an ISO 8601 duration string (e.g. "PT1H30M"). Throws on invalid
input. Corresponds to Temporal.Duration.from().
exampleFromString :: Effect Unit
exampleFromString = do
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.fromString "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.
exampleMilliseconds :: Effect Unit
exampleMilliseconds = do
duration <- Duration.from { milliseconds: 500 }
Console.log ("Milliseconds: " <> show (Duration.milliseconds duration))
Milliseconds: 500
#microseconds Source
microseconds :: Duration -> IntMicrosecond component of the duration.
exampleMicroseconds :: Effect Unit
exampleMicroseconds = do
duration <- Duration.from { microseconds: 250 }
Console.log ("Microseconds: " <> show (Duration.microseconds duration))
Microseconds: 250
#nanoseconds Source
nanoseconds :: Duration -> IntNanosecond component of the duration.
exampleNanoseconds :: Effect Unit
exampleNanoseconds = do
duration <- Duration.from { nanoseconds: 100 }
Console.log ("Nanoseconds: " <> show (Duration.nanoseconds duration))
Nanoseconds: 100
#sign Source
sign :: Duration -> IntReturns 1 if positive, -1 if negative, 0 if zero.
exampleSign :: Effect Unit
exampleSign = do
positive <- Duration.from { hours: 2 }
Console.log ("Sign of 2h: " <> show (Duration.sign positive))
negated <- pure (Duration.negated positive)
Console.log ("Sign of -2h: " <> show (Duration.sign negated))
Sign of 2h: 1
Sign of -2h: -1
#blank Source
blank :: Duration -> BooleanTrue if all components are zero.
exampleBlank :: Effect Unit
exampleBlank = do
zero <- Duration.from { hours: 0 }
nonZero <- Duration.from { hours: 1 }
Console.log ("Zero is blank: " <> show (Duration.blank zero))
Console.log ("1h is blank: " <> show (Duration.blank nonZero))
Zero is blank: true
1h is blank: false
#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().
exampleAdd :: Effect Unit
exampleAdd = do
locale <- JS.Intl.Locale.new_ "en-US"
twoHours <- Duration.from { hours: 2 }
thirtyMinutes <- Duration.from { 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().
exampleSubtract :: Effect Unit
exampleSubtract = do
locale <- JS.Intl.Locale.new_ "en-US"
threeHours <- Duration.from { hours: 3 }
oneHour <- Duration.from { 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
#negated Source
negated :: Duration -> DurationReverses the sign of the duration. Pure, does not throw.
exampleNegated :: Effect Unit
exampleNegated = do
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.from { hours: 2, minutes: 30 }
let neg = Duration.negated duration
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter neg)
-2 hours, 30 minutes
#abs Source
abs :: Duration -> DurationReturns the duration with positive sign. Pure, does not throw.
exampleAbs :: Effect Unit
exampleAbs = do
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.from { hours: 2 }
let neg = Duration.negated duration
let positive = Duration.abs neg
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter positive)
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().
exampleWith :: Effect Unit
exampleWith = do
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.from { 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().
exampleCompare :: Effect Unit
exampleCompare = do
shorter <- Duration.from { hours: 1 }
longer <- Duration.from { 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().
exampleRound :: Effect Unit
exampleRound = do
roundedSource <- Duration.from { hours: 1, minutes: 30, seconds: 45 }
rounded <- Duration.round { largestUnit: TemporalUnit.Hour, smallestUnit: TemporalUnit.Second } roundedSource
Console.log (Duration.toString rounded)
PT1H30M45S
#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().
exampleTotal :: Effect Unit
exampleTotal = do
locale <- JS.Intl.Locale.new_ "en-US"
duration <- Duration.from { 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 :: Duration -> StringSame as toStringWithOptions with default options.
exampleToString :: Effect Unit
exampleToString = do
duration <- Duration.from { hours: 2, minutes: 30, seconds: 15, milliseconds: 400 }
Console.log (Duration.toString duration)
PT2H30M15.4S
#toStringWithOptions Source
toStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToDurationToStringOptions (Record DurationToStringOptions) (Record provided) (Record DurationToStringOptions) => Record provided -> Duration -> StringSerializes the duration to ISO 8601 format (e.g. "PT1H30M").
exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
duration <- Duration.from { hours: 2, minutes: 30, seconds: 15, milliseconds: 400 }
Console.log (Duration.toStringWithOptions { smallestUnit: TemporalUnit.Second } duration)
PT2H30M15S
#fromMilliseconds Source
fromMilliseconds :: Milliseconds -> Effect DurationCreates a Temporal Duration from purescript-datetime Milliseconds.
exampleFromMilliseconds :: Effect Unit
exampleFromMilliseconds = do
duration <- Duration.fromMilliseconds (Milliseconds 5000.0)
Console.log (Duration.toString duration)
PT5S
#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.
exampleToMilliseconds :: Effect Unit
exampleToMilliseconds = do
duration <- Duration.from { seconds: 5 }
case Duration.toMilliseconds duration of
Just (Milliseconds ms) -> Console.log ("Milliseconds: " <> show ms)
Nothing -> Console.log "Cannot convert (has calendar units)"
Milliseconds: 5000.0
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