(self: Duration): {
days: number
hours: number
minutes: number
seconds: number
millis: number
nanos: number
}Decomposes a Duration into normalized signed components.
Details
Finite durations are returned as { days, hours, minutes, seconds, millis,
nanos }. Infinite durations return every component as Infinity or
-Infinity.
Example (Decomposing durations into parts)
import { Duration } from "effect"
// Create a complex duration by adding multiple parts
const duration = Duration.sum(
Duration.sum(
Duration.sum(Duration.days(1), Duration.hours(2)),
Duration.sum(Duration.minutes(30), Duration.seconds(45))
),
Duration.millis(123)
)
const components = Duration.parts(duration)
console.log(components)
// {
// days: 1,
// hours: 2,
// minutes: 30,
// seconds: 45,
// millis: 123,
// nanos: 0
// }
const complex = Duration.sum(Duration.hours(25), Duration.minutes(90))
const complexParts = Duration.parts(complex)
console.log(complexParts)
// {
// days: 1,
// hours: 2,
// minutes: 30,
// seconds: 0,
// millis: 0,
// nanos: 0
// }export const const parts: (self: Duration) => {
days: number
hours: number
minutes: number
seconds: number
millis: number
nanos: number
}
Decomposes a Duration into normalized signed components.
Details
Finite durations are returned as { days, hours, minutes, seconds, millis,
nanos }. Infinite durations return every component as Infinity or
-Infinity.
Example (Decomposing durations into parts)
import { Duration } from "effect"
// Create a complex duration by adding multiple parts
const duration = Duration.sum(
Duration.sum(
Duration.sum(Duration.days(1), Duration.hours(2)),
Duration.sum(Duration.minutes(30), Duration.seconds(45))
),
Duration.millis(123)
)
const components = Duration.parts(duration)
console.log(components)
// {
// days: 1,
// hours: 2,
// minutes: 30,
// seconds: 45,
// millis: 123,
// nanos: 0
// }
const complex = Duration.sum(Duration.hours(25), Duration.minutes(90))
const complexParts = Duration.parts(complex)
console.log(complexParts)
// {
// days: 1,
// hours: 2,
// minutes: 30,
// seconds: 0,
// millis: 0,
// nanos: 0
// }
parts = (self: Duration(parameter) 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): {
days: numberdays: number
hours: numberhours: number
minutes: numberminutes: number
seconds: numberseconds: number
millis: numbermillis: number
nanos: numbernanos: number
} => {
if (self: Duration(parameter) 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 === "Infinity") {
return {
days: numberdays: var Infinity: numberInfinity,
hours: numberhours: var Infinity: numberInfinity,
minutes: numberminutes: var Infinity: numberInfinity,
seconds: numberseconds: var Infinity: numberInfinity,
millis: numbermillis: var Infinity: numberInfinity,
nanos: numbernanos: var Infinity: numberInfinity
}
}
if (self: Duration(parameter) 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" | "NegativeInfinity"_tag === "NegativeInfinity") {
return {
days: numberdays: -var Infinity: numberInfinity,
hours: numberhours: -var Infinity: numberInfinity,
minutes: numberminutes: -var Infinity: numberInfinity,
seconds: numberseconds: -var Infinity: numberInfinity,
millis: numbermillis: -var Infinity: numberInfinity,
nanos: numbernanos: -var Infinity: numberInfinity
}
}
const const n: bigintn = const toNanosUnsafe: (
input: Input
) => bigint
Gets the duration in nanoseconds as a bigint.
When to use
Use when the duration is known to be finite and you need the nanosecond value
as a bigint.
Details
Millisecond-backed fractional durations are rounded to the nearest
nanosecond, with ties away from zero.
Gotchas
If the duration is infinite, it throws an error.
Example (Reading nanoseconds unsafely)
import { Duration } from "effect"
const duration = Duration.seconds(2)
const nanos = Duration.toNanosUnsafe(duration)
console.log(nanos) // 2000000000n
// Duration.toNanosUnsafe(Duration.infinity)
// throws Error: "Cannot convert infinite duration to nanos"
toNanosUnsafe(self: Duration(parameter) 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 const neg: booleanneg = const n: bigintn < const bigint0: bigintbigint0
const const a: biginta = const neg: booleanneg ? -const n: bigintn : const n: bigintn
const const ms: bigintms = const a: biginta / const bigint1e6: bigintbigint1e6
const const sec: bigintsec = const ms: bigintms / const bigint1e3: bigintbigint1e3
const const min: bigintmin = const sec: bigintsec / const bigint60: bigintbigint60
const const hr: biginthr = const min: bigintmin / const bigint60: bigintbigint60
const const d: bigintd = const hr: biginthr / const bigint24: bigintbigint24
const const sign: 1 | -1sign = const neg: booleanneg ? -1 : 1
return {
days: numberdays: const sign: 1 | -1sign * 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 d: bigintd),
hours: numberhours: const sign: 1 | -1sign * 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 hr: biginthr % const bigint24: bigintbigint24),
minutes: numberminutes: const sign: 1 | -1sign * 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 min: bigintmin % const bigint60: bigintbigint60),
seconds: numberseconds: const sign: 1 | -1sign * 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 sec: bigintsec % const bigint60: bigintbigint60),
millis: numbermillis: const sign: 1 | -1sign * 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 ms: bigintms % const bigint1e3: bigintbigint1e3),
nanos: numbernanos: const sign: 1 | -1sign * 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 a: biginta % const bigint1e6: bigintbigint1e6)
}
}