MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseNanos

Function parseNanos

packages/effect/src/Duration.ts:43–56  ·  view source on GitHub ↗
(input: string, scale: bigint)

Source from the content-addressed store, hash-verified

41const roundMillisToNanos = (millis: number): bigint => roundTiesAwayFromZero(millis * 1_000_000)
42
43const parseNanos = (input: string, scale: bigint): bigint => {
44 const decimalIndex = input.indexOf(".")
45 if (decimalIndex === -1) return BigInt(input) * scale
46
47 const isNegative = input[0] === "-"
48 const fractional = input.slice(decimalIndex + 1)
49 const fractionalScale = bigint10 ** BigInt(fractional.length)
50 const scaled = (
51 BigInt(input.slice(isNegative ? 1 : 0, decimalIndex)) * fractionalScale + BigInt(fractional)
52 ) * scale
53 const rounded = scaled / fractionalScale +
54 (scaled % fractionalScale * bigint2 >= fractionalScale ? bigint1 : bigint0)
55 return isNegative ? -rounded : rounded
56}
57
58const nanosToHrTime = (nanos: bigint): [seconds: number, nanos: number] => {
59 const sign = nanos < bigint0 ? -bigint1 : bigint1

Callers 1

fromInputUnsafeFunction · 0.85

Calls 1

BigIntInterface · 0.70

Tested by

no test coverage detected