Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Duration

52 exports Since v2.0.0 Source

Constructors

Since v2.0.0

Signature

declare function days(days: number): Duration
Since v2.0.0

Signature

declare function hours(hours: number): Duration
Since v2.0.0

Signature

declare const infinity: Duration
Since v2.0.0

Signature

declare function micros(micros: bigint): Duration
Since v2.0.0

Signature

declare function millis(millis: number): Duration
Since v2.0.0

Signature

declare function minutes(minutes: number): Duration
Since v2.0.0

Signature

declare function nanos(nanos: bigint): Duration
Since v2.0.0

Signature

declare function seconds(seconds: number): Duration
Since v2.0.0

Signature

declare function weeks(weeks: number): Duration
Since v2.0.0

Signature

declare const zero: Duration

Conversions

Since v2.0.0

Converts a Duration to a human readable string.

Signature

declare function format(self: DurationInput): string

Example

import { Duration } from "effect"
Duration.format(Duration.millis(1000)) // "1s"
Duration.format(Duration.millis(1001)) // "1s 1ms"
Since v3.13.0

Formats a Duration into an ISO8601 duration string.

Months are assumed to be 30 days and years are assumed to be 365 days.

Returns Option.none() if the duration is infinite.

Signature

declare function formatIso(self: DurationInput): Option<string>

Example

import { Duration, Option } from "effect"
Duration.formatIso(Duration.days(1)) // => Option.some("P1D")
Duration.formatIso(Duration.minutes(90)) // => Option.some("PT1H30M")
Duration.formatIso(Duration.millis(1500)) // => Option.some("PT1.5S")
Duration.formatIso(Duration.infinity) // => Option.none()
Since v3.13.0

Parses an ISO8601 duration string into a Duration.

Months are assumed to be 30 days and years are assumed to be 365 days.

Signature

declare function fromIso(iso: string): Option<Duration>

Example

import { Duration, Option } from "effect"
Duration.fromIso("P1D") // => Option.some(Duration.days(1))
Duration.fromIso("PT1H") // => Option.some(Duration.hours(1))
Duration.fromIso("PT1M") // => Option.some(Duration.minutes(1))
Duration.fromIso("PT1.5S") // => Option.some(Duration.seconds(1.5))
Since v3.8.0

Converts a Duration to its parts.

Signature

declare function parts(self: DurationInput): {
days: number;
hours: number;
millis: number;
minutes: number;
nanos: number;
seconds: number;
}

Formats a Duration into an ISO8601 duration string.

Months are assumed to be 30 days and years are assumed to be 365 days.

Milliseconds and nanoseconds are expressed as fractional seconds.

Signature

declare function unsafeFormatIso(self: DurationInput): string

Example

import { Duration } from "effect"
Duration.unsafeFormatIso(Duration.days(1)) // => "P1D"
Duration.unsafeFormatIso(Duration.minutes(90)) // => "PT1H30M"
Duration.unsafeFormatIso(Duration.millis(1500)) // => "PT1.5S"

Getters

Since v3.8.0

Signature

declare function toDays(self: DurationInput): number
Since v3.8.0

Signature

declare function toHours(self: DurationInput): number
Since v2.0.0

Signature

declare function toHrTime(self: DurationInput): [seconds: number, nanos: number]
Since v2.0.0

Signature

declare function toMillis(self: DurationInput): number

Signature

declare function toMinutes(self: DurationInput): number
Since v2.0.0

Get the duration in nanoseconds as a bigint.

If the duration is infinite, returns Option.none()

Signature

declare function toNanos(self: DurationInput): Option<bigint>

Signature

declare function toSeconds(self: DurationInput): number
Since v3.8.0

Signature

declare function toWeeks(self: DurationInput): number

Get the duration in nanoseconds as a bigint.

If the duration is infinite, it throws an error.

Signature

declare function unsafeToNanos(self: DurationInput): bigint

Guards

Signature

declare function isDuration(u: unknown): u is Duration
Since v2.0.0

Signature

declare function isFinite(self: Duration): boolean
Since v3.5.0

Signature

declare function isZero(self: Duration): boolean

Instances

Signature

declare const Equivalence: equivalence.Equivalence<Duration>
Since v2.0.0

Signature

declare const Order: order.Order<Duration>

Math

Since v2.4.19

Signature

declare const divide: {
(by: number): (self: DurationInput) => Option<Duration>;
(self: DurationInput, by: number): Option<Duration>;
}
Since v2.0.0

Signature

declare const subtract: {
(that: DurationInput): (self: DurationInput) => Duration;
(self: DurationInput, that: DurationInput): Duration;
}
Since v2.0.0

Signature

declare const sum: {
(that: DurationInput): (self: DurationInput) => Duration;
(self: DurationInput, that: DurationInput): Duration;
}
Since v2.0.0

Signature

declare const times: {
(times: number): (self: DurationInput) => Duration;
(self: DurationInput, times: number): Duration;
}

Signature

declare const unsafeDivide: {
(by: number): (self: DurationInput) => Duration;
(self: DurationInput, by: number): Duration;
}

Models

Duration

interface
Since v2.0.0

Signature

interface Duration extends Equal, Pipeable, Inspectable {
readonly [TypeId]: typeof TypeId;
readonly value: DurationValue;
}
Since v2.0.0

Signature

type DurationInput = Duration | number | bigint | readonly [seconds: number, nanos: number] | `${number} ${Unit}`
Since v2.0.0

Signature

type DurationValue = {
readonly _tag: "Millis";
readonly millis: number;
} | {
readonly _tag: "Nanos";
readonly nanos: bigint;
} | {
readonly _tag: "Infinity";
}

Unit

type
Since v2.0.0

Signature

type Unit = "nano" | "nanos" | "micro" | "micros" | "milli" | "millis" | "second" | "seconds" | "minute" | "minutes" | "hour" | "hours" | "day" | "days" | "week" | "weeks"

Order

Since v2.0.0

Signature

declare const clamp: {
(options: {
maximum: DurationInput;
minimum: DurationInput;
}): (self: DurationInput) => Duration;
(self: DurationInput, options: {
maximum: DurationInput;
minimum: DurationInput;
}): Duration;
}
Since v2.0.0

Signature

declare const max: {
(that: DurationInput): (self: DurationInput) => Duration;
(self: DurationInput, that: DurationInput): Duration;
}

Other

Since v2.0.0

Signature

declare function decode(input: DurationInput): Duration

Signature

declare const decodeUnknown: (u: unknown) => Option.Option<Duration>
Since v2.0.0

Signature

declare const min: {
(that: DurationInput): (self: DurationInput) => Duration;
(self: DurationInput, that: DurationInput): Duration;
}

Pattern Matching

Since v2.0.0

Signature

declare const match: {
<A, B>(options: {
readonly onMillis: (millis: number) => A;
readonly onNanos: (nanos: bigint) => B;
}): (self: DurationInput) => A | B;
<A, B>(self: DurationInput, options: {
readonly onMillis: (millis: number) => A;
readonly onNanos: (nanos: bigint) => B;
}): A | B;
}

Signature

declare const matchWith: {
<A, B>(that: DurationInput, options: {
readonly onMillis: (self: number, that: number) => A;
readonly onNanos: (self: bigint, that: bigint) => B;
}): (self: DurationInput) => A | B;
<A, B>(self: DurationInput, that: DurationInput, options: {
readonly onMillis: (self: number, that: number) => A;
readonly onNanos: (self: bigint, that: bigint) => B;
}): A | B;
}

Predicates

Since v2.0.0

Checks if a Duration is between a minimum and maximum value.

Signature

declare const between: {
(options: {
maximum: DurationInput;
minimum: DurationInput;
}): (self: DurationInput) => boolean;
(self: DurationInput, options: {
maximum: DurationInput;
minimum: DurationInput;
}): boolean;
}
Since v2.0.0

Signature

declare const equals: {
(that: DurationInput): (self: DurationInput) => boolean;
(self: DurationInput, that: DurationInput): boolean;
}

Signature

declare const greaterThan: {
(that: DurationInput): (self: DurationInput) => boolean;
(self: DurationInput, that: DurationInput): boolean;
}

Signature

declare const greaterThanOrEqualTo: {
(that: DurationInput): (self: DurationInput) => boolean;
(self: DurationInput, that: DurationInput): boolean;
}
Since v2.0.0

Signature

declare const lessThan: {
(that: DurationInput): (self: DurationInput) => boolean;
(self: DurationInput, that: DurationInput): boolean;
}

Signature

declare const lessThanOrEqualTo: {
(that: DurationInput): (self: DurationInput) => boolean;
(self: DurationInput, that: DurationInput): boolean;
}

Symbol

Since v2.0.0

Signature

type TypeId = typeof TypeId