postgres-interval
Advanced tools
+18
-0
@@ -64,2 +64,20 @@ declare namespace PostgresInterval { | ||
| toISOStringShort(): string; | ||
| /** | ||
| * Returns a [`Temporal.Duration`](https://tc39.es/proposal-temporal/docs/duration.html) representing the interval. | ||
| * | ||
| * Requires `globalThis.Temporal` (Node 26+, or a polyfill assigned to the global). Throws otherwise. | ||
| * | ||
| * Postgres mixed-sign intervals (e.g. `1 mon -1 days`) throw a `RangeError`, because `Temporal.Duration` requires a single sign across all fields. | ||
| * | ||
| * The `Temporal` types are not yet in the default TypeScript lib. For this signature to resolve, your project needs Temporal lib types (the TypeScript lib once available, or `@js-temporal/polyfill` types). | ||
| * | ||
| * ```js | ||
| * var parse = require('postgres-interval') | ||
| * var interval = parse('01:02:03') | ||
| * // => { hours: 1, minutes: 2, seconds: 3 } | ||
| * interval.toTemporalDuration().toString() | ||
| * // PT1H2M3S | ||
| * ``` | ||
| */ | ||
| toTemporalDuration(): Temporal.Duration; | ||
| } | ||
@@ -66,0 +84,0 @@ } |
+20
-7
@@ -96,2 +96,21 @@ 'use strict' | ||
| PostgresInterval.prototype.toTemporalDuration = function () { | ||
| if (typeof globalThis.Temporal === 'undefined') { | ||
| throw new Error('Temporal is not available. It ships unflagged in Node 26+. On older runtimes, install a polyfill (e.g. @js-temporal/polyfill) and assign it to globalThis.Temporal.') | ||
| } | ||
| const totalMicroseconds = Math.round(this.milliseconds * 1000) | ||
| return globalThis.Temporal.Duration.from({ | ||
| years: this.years, | ||
| months: this.months, | ||
| days: this.days, | ||
| hours: this.hours, | ||
| minutes: this.minutes, | ||
| seconds: this.seconds, | ||
| milliseconds: Math.trunc(totalMicroseconds / 1000), | ||
| microseconds: totalMicroseconds % 1000 | ||
| }) | ||
| } | ||
| function toISOString ({ short }) { | ||
@@ -202,11 +221,5 @@ let datePart = '' | ||
| continue | ||
| } else if (char === '+') { | ||
| } else if (!(char >= '0' && char <= '9')) { | ||
| position.value++ | ||
| continue | ||
| } else if (char === ' ') { | ||
| position.value++ | ||
| continue | ||
| } else if (char < '0' || char > '9') { | ||
| position.value++ | ||
| continue | ||
| } else { | ||
@@ -213,0 +226,0 @@ currentValue = readNextNum(interval) |
+1
-1
| { | ||
| "name": "postgres-interval", | ||
| "main": "index.js", | ||
| "version": "4.0.2", | ||
| "version": "4.1.0", | ||
| "description": "Parse Postgres interval columns", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
+10
-0
@@ -60,4 +60,14 @@ # postgres-interval [](https://github.com/bendrucker/postgres-interval/actions?query=workflow%3Atests) | ||
| #### `interval.toTemporalDuration()` -> `Temporal.Duration` | ||
| Returns a [`Temporal.Duration`](https://tc39.es/proposal-temporal/docs/duration.html) representing the interval. | ||
| Requires `globalThis.Temporal`. It ships unflagged in Node 26+. On older runtimes, install a polyfill such as [`@js-temporal/polyfill`](https://www.npmjs.com/package/@js-temporal/polyfill) and assign it to `globalThis.Temporal`. The method throws if `Temporal` is unavailable. | ||
| Postgres mixed-sign intervals (e.g. `1 mon -1 days`) throw a `RangeError`. `Temporal.Duration` requires all fields to share a single sign, which these intervals violate. | ||
| The `Temporal` types are not yet in the default TypeScript lib. To resolve the return type, your project needs Temporal lib types: the TypeScript lib once available, or the `@js-temporal/polyfill` types. This package adds no type dependency. | ||
| ## License | ||
| MIT © [Ben Drucker](http://bendrucker.me) |
14318
19.25%318
9.66%73
15.87%