(self: Input): numberConverts a Duration to seconds.
Example (Converting durations to seconds)
import { Duration } from "effect"
console.log(Duration.toSeconds(Duration.millis(5000))) // 5
console.log(Duration.toSeconds(Duration.minutes(2))) // 120export const const toSeconds: (self: Input) => numberConverts a Duration to seconds.
Example (Converting durations to seconds)
import { Duration } from "effect"
console.log(Duration.toSeconds(Duration.millis(5000))) // 5
console.log(Duration.toSeconds(Duration.minutes(2))) // 120
toSeconds = (self: Inputself: type Input =
| number
| bigint
| Duration
| "Infinity"
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "-Infinity"
| DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input): number =>
const match: {
<A, B, C, D = C>(options: {
readonly onMillis: (millis: number) => A
readonly onNanos: (nanos: bigint) => B
readonly onInfinity: () => C
readonly onNegativeInfinity?: () => D
}): (self: Duration) => A | B | C | D
<A, B, C, D = C>(
self: Duration,
options: {
readonly onMillis: (millis: number) => A
readonly onNanos: (nanos: bigint) => B
readonly onInfinity: () => C
readonly onNegativeInfinity?: () => D
}
): A | B | C | D
}
match(const fromInputUnsafe: (
input: Input
) => Duration
Decodes a Duration.Input into a Duration.
When to use
Use when the input has already been validated or comes from a trusted source
and throwing is acceptable for invalid duration syntax.
Gotchas
If the input is not a valid Duration.Input, it throws an error.
Example (Decoding duration inputs)
import { Duration } from "effect"
const duration1 = Duration.fromInputUnsafe(1000) // 1000 milliseconds
const duration2 = Duration.fromInputUnsafe("5 seconds")
const duration3 = Duration.fromInputUnsafe("Infinity")
const duration4 = Duration.fromInputUnsafe([2, 500_000_000]) // 2 seconds and 500ms
fromInputUnsafe(self: Inputself), {
onMillis: (millis: number) => numberonMillis: (millis: numbermillis) => millis: numbermillis / 1_000,
onNanos: (nanos: bigint) => numberonNanos: (nanos: bigintnanos) => var Number: NumberConstructor
;(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number(nanos: bigintnanos) / 1_000_000_000,
onInfinity: () => numberonInfinity: () => var Infinity: numberInfinity,
onNegativeInfinity?: (() => number) | undefinedonNegativeInfinity: () => -var Infinity: numberInfinity
})