(input: Input): DurationDecodes 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 500msexport const 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: 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): Duration => {
switch (typeof input: Inputinput) {
case "number":
return const millis: (millis: number) => DurationCreates a Duration from milliseconds.
Example (Creating durations from milliseconds)
import { Duration } from "effect"
const duration = Duration.millis(1000)
console.log(Duration.toMillis(duration)) // 1000
millis(input: Inputinput)
case "bigint":
return const nanos: (nanos: bigint) => DurationCreates a Duration from nanoseconds.
Example (Creating durations from nanoseconds)
import { Duration } from "effect"
const duration = Duration.nanos(BigInt(500_000_000))
console.log(Duration.toMillis(duration)) // 500
nanos(input: Inputinput)
case "string": {
if (input: Inputinput === "Infinity") {
return const infinity: Durationconst infinity: {
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;
}
A Duration representing infinite time.
Example (Referencing infinite duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.infinity)) // Infinity
infinity
}
if (input: Inputinput === "-Infinity") {
return const negativeInfinity: Durationconst negativeInfinity: {
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;
}
A Duration representing negative infinite time.
Example (Referencing negative infinite duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.negativeInfinity)) // -Infinity
negativeInfinity
}
const const match: RegExpExecArray | nullmatch = const DURATION_REGEXP: RegExpDURATION_REGEXP.RegExp.exec(string: string): RegExpExecArray | nullExecutes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
exec(input: Inputinput)
if (!const match: RegExpExecArray | nullmatch) break
const [const _: string_, const valueStr: stringvalueStr, const unit: stringunit] = const match: RegExpExecArraymatch
if (const unit: stringunit === "nano" || const unit: stringunit === "nanos") {
return const nanos: (nanos: bigint) => DurationCreates a Duration from nanoseconds.
Example (Creating durations from nanoseconds)
import { Duration } from "effect"
const duration = Duration.nanos(BigInt(500_000_000))
console.log(Duration.toMillis(duration)) // 500
nanos(const parseNanos: (
input: string,
scale: bigint
) => bigint
parseNanos(const valueStr: stringvalueStr, const bigint1: bigintbigint1))
}
if (const unit: stringunit === "micro" || const unit: stringunit === "micros") {
return const nanos: (nanos: bigint) => DurationCreates a Duration from nanoseconds.
Example (Creating durations from nanoseconds)
import { Duration } from "effect"
const duration = Duration.nanos(BigInt(500_000_000))
console.log(Duration.toMillis(duration)) // 500
nanos(const parseNanos: (
input: string,
scale: bigint
) => bigint
parseNanos(const valueStr: stringvalueStr, const bigint1e3: bigintbigint1e3))
}
const const value: numbervalue = 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(const valueStr: stringvalueStr)
switch (const unit: stringunit) {
case "milli":
case "millis":
return const millis: (millis: number) => DurationCreates a Duration from milliseconds.
Example (Creating durations from milliseconds)
import { Duration } from "effect"
const duration = Duration.millis(1000)
console.log(Duration.toMillis(duration)) // 1000
millis(const value: numbervalue)
case "second":
case "seconds":
return const seconds: (
seconds: number
) => Duration
Creates a Duration from seconds.
Example (Creating durations from seconds)
import { Duration } from "effect"
const duration = Duration.seconds(30)
console.log(Duration.toMillis(duration)) // 30000
seconds(const value: numbervalue)
case "minute":
case "minutes":
return const minutes: (
minutes: number
) => Duration
Creates a Duration from minutes.
Example (Creating durations from minutes)
import { Duration } from "effect"
const duration = Duration.minutes(5)
console.log(Duration.toMillis(duration)) // 300000
minutes(const value: numbervalue)
case "hour":
case "hours":
return const hours: (hours: number) => DurationCreates a Duration from hours.
Example (Creating durations from hours)
import { Duration } from "effect"
const duration = Duration.hours(2)
console.log(Duration.toMillis(duration)) // 7200000
hours(const value: numbervalue)
case "day":
case "days":
return const days: (days: number) => DurationCreates a Duration from days.
Example (Creating durations from days)
import { Duration } from "effect"
const duration = Duration.days(1)
console.log(Duration.toMillis(duration)) // 86400000
days(const value: numbervalue)
case "week":
case "weeks":
return const weeks: (weeks: number) => DurationCreates a Duration from weeks.
Example (Creating durations from weeks)
import { Duration } from "effect"
const duration = Duration.weeks(1)
console.log(Duration.toMillis(duration)) // 604800000
weeks(const value: numbervalue)
}
break
}
case "object": {
if (input: Inputinput === null) break
if (const TypeId: "~effect/time/Duration"TypeId in input: Inputinput) return input: Input(parameter) input: {
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;
}
input as Duration
if (var Array: ArrayConstructorArray.ArrayConstructor.isArray(arg: any): arg is any[]isArray(input: Inputinput)) {
if (input: Inputinput.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 2 || !input: Inputinput.Array<T>.every<number>(predicate: (value: any, index: number, array: any[]) => value is number, thisArg?: any): this is S[] (+1 overload)Determines whether all the members of an array satisfy the specified test.
every(function isNumber(input: unknown): input is numberChecks whether a value is a number.
When to use
Use when you need a Predicate guard to narrow an unknown value to a
number.
Details
Uses typeof input === "number" and does not exclude NaN or Infinity.
Example (Guarding numbers)
import { Predicate } from "effect"
const data: unknown = 42
if (Predicate.isNumber(data)) {
console.log(data + 1)
}
isNumber)) {
return const invalid: (input: unknown) => neverinvalid(input: Inputinput)
}
if (var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.isNaN(number: unknown): booleanReturns a Boolean value that indicates whether a value is the reserved value NaN (not a
number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
to a number. Only values of the type number, that are also NaN, result in true.
isNaN(input: Inputinput[0]) || var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.isNaN(number: unknown): booleanReturns a Boolean value that indicates whether a value is the reserved value NaN (not a
number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
to a number. Only values of the type number, that are also NaN, result in true.
isNaN(input: Inputinput[1])) {
return const zero: Durationconst zero: {
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;
}
A Duration representing zero time.
Example (Referencing the zero duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.zero)) // 0
zero
}
if (input: Inputinput[0] === -var Infinity: numberInfinity || input: Inputinput[1] === -var Infinity: numberInfinity) {
return const negativeInfinity: Durationconst negativeInfinity: {
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;
}
A Duration representing negative infinite time.
Example (Referencing negative infinite duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.negativeInfinity)) // -Infinity
negativeInfinity
}
if (input: Inputinput[0] === var Infinity: numberInfinity || input: Inputinput[1] === var Infinity: numberInfinity) {
return const infinity: Durationconst infinity: {
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;
}
A Duration representing infinite time.
Example (Referencing infinite duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.infinity)) // Infinity
infinity
}
return const make: (
input: number | bigint
) => Duration
make(const roundTiesAwayFromZero: (
input: number
) => bigint
roundTiesAwayFromZero(input: Inputinput[0] * 1_000_000_000 + input: Inputinput[1]))
}
const const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj = input: Inputinput as DurationObject
let let millis: numbermillis = 0
// we can use truthy checks here, because 0 can be ignored
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.weeks?: number | undefinedweeks) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.weeks?: numberweeks * 604_800_000
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.days?: number | undefineddays) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.days?: numberdays * 86_400_000
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.hours?: number | undefinedhours) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.hours?: numberhours * 3_600_000
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.minutes?: number | undefinedminutes) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.minutes?: numberminutes * 60_000
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.seconds?: number | undefinedseconds) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.seconds?: numberseconds * 1_000
if (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.milliseconds?: number | undefinedmilliseconds) let millis: numbermillis += const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.milliseconds?: numbermilliseconds
if (!const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.microseconds?: number | undefinedmicroseconds && !const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.nanoseconds?: number | undefinednanoseconds) return const make: (
input: number | bigint
) => Duration
make(let millis: numbermillis)
return const make: (
input: number | bigint
) => Duration
make(const roundTiesAwayFromZero: (
input: number
) => bigint
roundTiesAwayFromZero(
let millis: numbermillis * 1_000_000 + (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.microseconds?: number | undefinedmicroseconds ?? 0) * 1_000 + (const obj: DurationObjectconst obj: {
weeks: number | undefined;
days: number | undefined;
hours: number | undefined;
minutes: number | undefined;
seconds: number | undefined;
milliseconds: number | undefined;
microseconds: number | undefined;
nanoseconds: number | undefined;
}
obj.DurationObject.nanoseconds?: number | undefinednanoseconds ?? 0)
))
}
}
return const invalid: (input: unknown) => neverinvalid(input: Inputinput)
}