Hyperlinkv0.8.0-beta.28

Duration

Duration.toNanosUnsafeconsteffect/Duration.ts:928
(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"
getters
Source effect/Duration.ts:92812 lines
export const toNanosUnsafe = (input: Input): bigint => {
  const self = fromInputUnsafe(input)
  switch (self.value._tag) {
    case "Infinity":
    case "NegativeInfinity":
      throw new Error("Cannot convert infinite duration to nanos")
    case "Nanos":
      return self.value.nanos
    case "Millis":
      return roundMillisToNanos(self.value.millis)
  }
}
Referenced by 3 symbols