UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

42 lines (40 loc) • 2.31 kB
import { NANOSECONDS_PER_SECOND, PROTO_DURATION_MAX_SECONDS } from "../../internal/foreignEpochs.js"; import { exactDurationNanoseconds } from "../../internal/exactDuration.js"; // src/precision/convert/toProtoDuration.ts /** * Convert an ISO 8601 duration string to a protobuf `google.protobuf.Duration` record. * * - **Same sign.** Unlike `Timestamp`, a negative `Duration` has a negative `nanos` too: * `-PT1.5S` is `{ seconds: -1n, nanos: -500000000 }`, not `{ seconds: -2n, nanos: 500000000 }`. * - A day is exactly 24 hours, as TC39 Temporal reads a day without a `relativeTo`; protobuf has * no day unit. `P1D` is 86400 seconds. * - Years, months and weeks have no fixed length and return `null`. * - Exact: nothing is rounded. `seconds` is a `bigint` (`int64`); `seconds` beyond ±315576000000, * the range `duration.proto` states, returns `null`. `seconds` and `nanos` are bounded separately. * - Returns `null` on invalid input. * * @param isoDuration ISO 8601 duration string (e.g. "PT1H30M1.5S") * @returns `{ seconds, nanos }`, or `null` on invalid input * * @example toProtoDuration("PT1H30M1.5S") // { seconds: 5401n, nanos: 500000000 } * @example toProtoDuration("-PT1.5S") // { seconds: -1n, nanos: -500000000 } — nanos take seconds' sign * @example toProtoDuration("-PT0.5S") // { seconds: 0n, nanos: -500000000 } * @example toProtoDuration("P1D") // { seconds: 86400n, nanos: 0 } — a day is 24 hours * @example toProtoDuration("PT0S") // { seconds: 0n, nanos: 0 } * @example toProtoDuration("PT315576000000S") // { seconds: 315576000000n, nanos: 0 } * @example toProtoDuration("PT315576000000.000000001S") // { seconds: 315576000000n, nanos: 1 } — seconds and nanos are bounded separately * @example toProtoDuration("PT315576000001S") // null — seconds past the protobuf range * @example toProtoDuration("P1M") // null — a month has no fixed length * @example toProtoDuration("invalid") // null */ function toProtoDuration(isoDuration) { const nanoseconds = exactDurationNanoseconds(isoDuration); if (nanoseconds === null) return null; const seconds = nanoseconds / NANOSECONDS_PER_SECOND; if (seconds < -315576000000n || seconds > 315576000000n) return null; return { seconds, nanos: Number(nanoseconds % NANOSECONDS_PER_SECOND) }; } export { toProtoDuration };