diff --git a/lib/shared/dateHelper.js b/lib/shared/dateHelper.js index bde93512..ca082f20 100644 --- a/lib/shared/dateHelper.js +++ b/lib/shared/dateHelper.js @@ -1,28 +1,29 @@ -import { Duration, ZonedDateTime } from '@js-joda/core' +import { toTime } from '@secvisogram/is-leap-second' /** - * compare ZonedDateTimes from js-joda - * returns a negative number if a is less than b, positive if a is greater than b, and zero if they are equal. - * This function also returns 0 if one of the given values could not be parsed. - * - * @param {ZonedDateTime | string} a - * @param {ZonedDateTime | string} b - * @returns {0|1|-1} + * Compares two date-time strings using `toTime` from `@secvisogram/is-leap-second`. + * Returns a negative number if `a` is less than `b`, a positive number if `a` is greater + * than `b`, and zero if they are equal. Also returns 0 if either value cannot be parsed. + * Follows the comparator convention used by `Array.prototype.sort`. * + * @param {string} a - The first date-time string to compare. + * @param {string} b - The second date-time string to compare. + * @returns {0|1|-1} Negative if `a < b`, positive if `a > b`, zero if equal or unparseable. */ export const compareZonedDateTimes = (a, b) => { - // catch js-joda exception if a or b can't be parsed + // catch TypeError exception if a or b can't be parsed try { - const date1 = a instanceof ZonedDateTime ? a : ZonedDateTime.parse(a) - const date2 = b instanceof ZonedDateTime ? b : ZonedDateTime.parse(b) - const duration = Duration.between(date1, date2) + const date1 = toTime(a) + const date2 = toTime(b) + if (date1 === null || date2 === null) return 0 + const duration = date2 - date1 // return number based on js sort function // > negative if a is less than b, positive if a is greater than b, and zero if they are equal. // [Sort Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#comparefn) - if (duration.isZero()) { + if (duration === 0n) { return 0 - } else if (duration.isNegative()) { + } else if (duration < 0n) { return 1 } else { return -1 diff --git a/package-lock.json b/package-lock.json index 09f766ed..3cb1fd05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@js-joda/core": "^5.6.1", "@js-joda/timezone": "^2.18.2", + "@secvisogram/is-leap-second": "^0.1.0", "ajv": "^8.11.2", "ajv-formats": "^3.0.1", "bcp47": "^1.1.2", @@ -216,6 +217,12 @@ "node": ">=14" } }, + "node_modules/@secvisogram/is-leap-second": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@secvisogram/is-leap-second/-/is-leap-second-0.1.0.tgz", + "integrity": "sha512-Z+wH2KgDwMLqLVQF1Nb8P0EXGVByaFWe3st40L9Lj+EOIumaQbQ3r2ZJbuDSVz6fyxyMOe+PxDoD7v5j2vnPAg==", + "license": "Apache-2.0" + }, "node_modules/@types/chai": { "version": "4.3.20", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.20.tgz", diff --git a/package.json b/package.json index 2f346ba4..8e0e2a3b 100644 --- a/package.json +++ b/package.json @@ -120,6 +120,7 @@ "dependencies": { "@js-joda/core": "^5.6.1", "@js-joda/timezone": "^2.18.2", + "@secvisogram/is-leap-second": "^0.1.0", "ajv": "^8.11.2", "ajv-formats": "^3.0.1", "bcp47": "^1.1.2",