(input: Input): [seconds: number, nanos: number]Converts a Duration to high-resolution time format [seconds, nanoseconds].
Example (Converting durations to high-resolution time)
import { Duration } from "effect"
const duration = Duration.millis(1500)
const hrtime = Duration.toHrTime(duration)
console.log(hrtime) // [1, 500000000]export const const toHrTime: (
input: Input
) => [seconds: number, nanos: number]
Converts a Duration to high-resolution time format [seconds, nanoseconds].
Example (Converting durations to high-resolution time)
import { Duration } from "effect"
const duration = Duration.millis(1500)
const hrtime = Duration.toHrTime(duration)
console.log(hrtime) // [1, 500000000]
toHrTime = (input: Inputinput: 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): [numberseconds: number, numbernanos: number] => {
const const self: Durationconst self: {
value: DurationValue;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self = 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(input: Inputinput)
switch (const self: Durationconst self: {
value: DurationValue;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Duration.value: DurationValuevalue._tag: | "Millis"
| "Nanos"
| "Infinity"
| "NegativeInfinity"
_tag) {
case "Infinity":
return [var Infinity: numberInfinity, 0]
case "NegativeInfinity":
return [-var Infinity: numberInfinity, 0]
case "Nanos":
return const nanosToHrTime: (
nanos: bigint
) => [seconds: number, nanos: number]
nanosToHrTime(const self: Durationconst self: {
value: DurationValue;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Duration.value: DurationValuevalue.nanos: bigintnanos)
case "Millis":
return const nanosToHrTime: (
nanos: bigint
) => [seconds: number, nanos: number]
nanosToHrTime(const roundMillisToNanos: (
millis: number
) => bigint
roundMillisToNanos(const self: Durationconst self: {
value: DurationValue;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Duration.value: DurationValuevalue.millis: numbermillis))
}
}