Module

JS.Temporal.PlainTime

Package
purescript-js-temporal
Repository
pete-murphy/purescript-js-temporal

A wall-clock time (hour, minute, second, etc.) without date or time zone. Use for recurring times (e.g. "3:30 PM") or when combining with PlainDate.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime

#PlainTimeComponents Source

type PlainTimeComponents :: Row Typetype PlainTimeComponents = (hour :: Int, microsecond :: Int, millisecond :: Int, minute :: Int, nanosecond :: Int, second :: Int)

#fromWithOptions Source

fromWithOptions :: forall optsProvided provided rest. Union provided rest PlainTimeComponents => ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record optsProvided) (Record OverflowOptions) => Record optsProvided -> Record provided -> Effect PlainTime

Creates a PlainTime from component fields. Options: overflow.

exampleFromWithOptions :: Effect Unit
exampleFromWithOptions = do
  time <- PlainTime.fromWithOptions { overflow: Overflow.Constrain }
    { hour: 9, minute: 30, second: 0 }
  Console.log (PlainTime.toString time)

09:30:00

#from Source

from :: forall provided rest. Union provided rest PlainTimeComponents => Record provided -> Effect PlainTime

Same as fromWithOptions with default options.

exampleFrom :: Effect Unit
exampleFrom = do
  locale <- JS.Intl.Locale.new_ "en-US"
  time <- PlainTime.from { hour: 9, minute: 30, second: 0 }
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { timeStyle: "medium" }
  Console.log (JS.Intl.DateTimeFormat.format formatter time)

9:30:00 AM

#fromStringWithOptions Source

fromStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> String -> Effect PlainTime

Parses a time string (e.g. "15:30:00"). Options: overflow. Throws on invalid input.

exampleFromStringWithOptions :: Effect Unit
exampleFromStringWithOptions = do
  time <- PlainTime.fromStringWithOptions { overflow: Overflow.Constrain } "15:30:00"
  Console.log (PlainTime.toString time)

15:30:00

#fromString Source

fromString :: String -> Effect PlainTime

Same as fromStringWithOptions with default options.

exampleFromString :: Effect Unit
exampleFromString = do
  locale <- JS.Intl.Locale.new_ "en-US"
  time <- PlainTime.fromString "15:30:00"
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { timeStyle: "medium" }
  Console.log (JS.Intl.DateTimeFormat.format formatter time)

3:30:00 PM

#hour Source

hour :: PlainTime -> Int

Hour component.

exampleHour :: Effect Unit
exampleHour = do
  time <- PlainTime.fromString "14:30:45"
  Console.log ("Hour: " <> show (PlainTime.hour time))

Hour: 14

#minute Source

minute :: PlainTime -> Int

Minute component.

exampleMinute :: Effect Unit
exampleMinute = do
  time <- PlainTime.fromString "14:30:45"
  Console.log ("Minute: " <> show (PlainTime.minute time))

Minute: 30

#second Source

second :: PlainTime -> Int

Second component.

exampleSecond :: Effect Unit
exampleSecond = do
  time <- PlainTime.fromString "14:30:45"
  Console.log ("Second: " <> show (PlainTime.second time))

Second: 45

#millisecond Source

millisecond :: PlainTime -> Int

Millisecond component.

exampleMillisecond :: Effect Unit
exampleMillisecond = do
  time <- PlainTime.fromString "14:30:45.123"
  Console.log ("Millisecond: " <> show (PlainTime.millisecond time))

Millisecond: 123

#microsecond Source

microsecond :: PlainTime -> Int

Microsecond component.

exampleMicrosecond :: Effect Unit
exampleMicrosecond = do
  time <- PlainTime.fromString "14:30:45.123456"
  Console.log ("Microsecond: " <> show (PlainTime.microsecond time))

Microsecond: 456

#nanosecond Source

nanosecond :: PlainTime -> Int

Nanosecond component.

exampleNanosecond :: Effect Unit
exampleNanosecond = do
  time <- PlainTime.fromString "14:30:45.123456789"
  Console.log ("Nanosecond: " <> show (PlainTime.nanosecond time))

Nanosecond: 789

#add Source

add :: Duration -> PlainTime -> Effect PlainTime

Adds a duration. Wraps at 24 hours. Throws for calendar durations.

exampleAdd :: Effect Unit
exampleAdd = do
  time <- PlainTime.fromString "14:30:00"
  twoHours <- Duration.from { hours: 2 }
  later <- PlainTime.add twoHours time
  Console.log (PlainTime.toString later)

16:30:00

#subtract Source

subtract :: Duration -> PlainTime -> Effect PlainTime

Subtracts a duration. Arg order: subtract duration subject. Wraps at 24 hours.

exampleSubtract :: Effect Unit
exampleSubtract = do
  time <- PlainTime.fromString "14:30:00"
  twoHours <- Duration.from { hours: 2 }
  earlier <- PlainTime.subtract twoHours time
  Console.log (PlainTime.toString earlier)

12:30:00

#withWithOptions Source

withWithOptions :: forall optsProvided fields rest. Union fields rest WithFields => ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record optsProvided) (Record OverflowOptions) => Record optsProvided -> Record fields -> PlainTime -> Effect PlainTime

Returns a new PlainTime with specified fields replaced. Options: overflow.

exampleWithWithOptions :: Effect Unit
exampleWithWithOptions = do
  time <- PlainTime.fromString "15:30:45"
  noon <- PlainTime.withWithOptions { overflow: Overflow.Constrain } { hour: 12, minute: 0, second: 0 } time
  Console.log (PlainTime.toString noon)

12:00:00

#with Source

with :: forall fields rest. Union fields rest WithFields => Record fields -> PlainTime -> Effect PlainTime

Same as withWithOptions with default options.

exampleWith :: Effect Unit
exampleWith = do
  time <- PlainTime.fromString "15:30:45"
  noon <- PlainTime.with { hour: 12, minute: 0, second: 0 } time
  Console.log (PlainTime.toString noon)

12:00:00

#untilWithOptions Source

untilWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainTime -> PlainTime -> Effect Duration

Duration from subject (last arg) until other (second arg). Arg order: untilWithOptions options other subject.

exampleUntilWithOptions :: Effect Unit
exampleUntilWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  start <- PlainTime.fromString "09:00:00"
  end <- PlainTime.fromString "17:30:00"
  duration <- PlainTime.untilWithOptions { largestUnit: TemporalUnit.Hour } end start
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log (JS.Intl.DurationFormat.format formatter duration)

8 hours, 30 minutes

#until Source

until :: PlainTime -> PlainTime -> Effect Duration

Same as untilWithOptions with default options.

exampleUntil :: Effect Unit
exampleUntil = do
  start <- PlainTime.fromString "09:00:00"
  end <- PlainTime.fromString "17:30:00"
  duration <- PlainTime.until end start
  Console.log (Duration.toString duration)

PT8H30M

#sinceWithOptions Source

sinceWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainTime -> PlainTime -> Effect Duration

Duration from other to subject (inverse of untilWithOptions). Arg order: sinceWithOptions options other subject.

exampleSinceWithOptions :: Effect Unit
exampleSinceWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  earlier <- PlainTime.fromString "08:00:00"
  later <- PlainTime.fromString "12:30:00"
  duration <- PlainTime.sinceWithOptions { largestUnit: TemporalUnit.Hour } earlier later
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log (JS.Intl.DurationFormat.format formatter duration)

4 hours, 30 minutes

#since Source

since :: PlainTime -> PlainTime -> Effect Duration

Same as sinceWithOptions with default options.

exampleSince :: Effect Unit
exampleSince = do
  earlier <- PlainTime.fromString "08:00:00"
  later <- PlainTime.fromString "12:30:00"
  duration <- PlainTime.since earlier later
  Console.log (Duration.toString duration)

PT4H30M

#round Source

round :: forall provided. ConvertOptionsWithDefaults ToRoundOptions (Record RoundOptions) (Record provided) (Record RoundOptions) => Record provided -> PlainTime -> Effect PlainTime

Rounds the time to the given smallest unit.

exampleRound :: Effect Unit
exampleRound = do
  time <- PlainTime.fromString "09:30:45.123"
  rounded <- PlainTime.round { smallestUnit: TemporalUnit.Minute } time
  Console.log (PlainTime.toString rounded)

09:31:00

#fromTime Source

fromTime :: Time -> Effect PlainTime

Converts a purescript-datetime Time to a PlainTime. Microsecond and nanosecond components are set to zero.

exampleFromTime :: Effect Unit
exampleFromTime = do
  time <- PlainTime.fromString "14:30:00"
  roundTripped <- PlainTime.fromTime (PlainTime.toTime time)
  Console.log (PlainTime.toString roundTripped)

14:30:00

#toTime Source

toTime :: PlainTime -> Time

Converts a PlainTime to a purescript-datetime Time. Microsecond and nanosecond are dropped (treated as zero).

exampleToTime :: Effect Unit
exampleToTime = do
  time <- PlainTime.fromString "14:30:00"
  Console.log (show (PlainTime.toTime time))

(Time (Hour 14) (Minute 30) (Second 0) (Millisecond 0))

#toStringWithOptions Source

toStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToToStringOptions (Record ToStringOptions) (Record provided) (Record ToStringOptions) => Record provided -> PlainTime -> String

Serializes to ISO 8601 time format. Options: fractionalSecondDigits, smallestUnit, roundingMode.

exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
  time <- PlainTime.fromString "14:30:45.123"
  Console.log (PlainTime.toStringWithOptions { smallestUnit: TemporalUnit.Millisecond } time)

14:30:45.123

#toString Source

toString :: PlainTime -> String

Same as toStringWithOptions with default options.

exampleToString :: Effect Unit
exampleToString = do
  time <- PlainTime.fromString "14:30:45.123"
  Console.log (PlainTime.toString time)

14:30:45.123

Re-exports from JS.Temporal.PlainTime.Internal