forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1import { Temporal as _polyfill } from "temporal-polyfill";
2
3export const Temporal = /** @type {any} */ (globalThis).Temporal ?? _polyfill;
4
5/**
6 * @param {string} a
7 * @param {string} b
8 *
9 * @example Returns 0 for equal timestamps, negative for earlier, positive for later
10 * ```js
11 * import { compareTimestamps } from "~/common/temporal.js";
12 *
13 * const eq = compareTimestamps("2024-01-01T00:00:00.000Z", "2024-01-01T00:00:00.000Z");
14 * if (eq !== 0) throw new Error("equal timestamps should return 0");
15 *
16 * const earlier = compareTimestamps("2024-01-01T00:00:00.000Z", "2024-06-01T00:00:00.000Z");
17 * if (earlier >= 0) throw new Error("earlier timestamp should return negative");
18 *
19 * const later = compareTimestamps("2024-06-01T00:00:00.000Z", "2024-01-01T00:00:00.000Z");
20 * if (later <= 0) throw new Error("later timestamp should return positive");
21 *
22 * const ms = compareTimestamps("2024-01-01T00:00:00.001Z", "2024-01-01T00:00:00.000Z");
23 * if (ms <= 0) throw new Error("millisecond precision should be respected");
24 * ```
25 */
26export function compareTimestamps(a, b) {
27 return Temporal.Instant.compare(
28 Temporal.Instant.from(a),
29 Temporal.Instant.from(b),
30 );
31}