|
| 1 | +/** |
| 2 | + * Temporal API types for representing dates/times and durations. |
| 3 | + * |
| 4 | + * This module re-exports the Temporal types from temporal-polyfill and provides |
| 5 | + * helper functions for working with them in the context of SQLite. |
| 6 | + * |
| 7 | + * @see https://tc39.es/proposal-temporal/docs/ |
| 8 | + * @see https://github.com/fullcalendar/temporal-polyfill |
| 9 | + */ |
| 10 | + |
| 11 | +import { Temporal } from "temporal-polyfill"; |
| 12 | + |
| 13 | +// Re-export the Temporal namespace for use with this SDK |
| 14 | +export { Temporal }; |
| 15 | + |
| 16 | +/** |
| 17 | + * Represents an exact point in time, independent of timezone. |
| 18 | + * This is a re-export of Temporal.Instant from temporal-polyfill. |
| 19 | + */ |
| 20 | +export type Instant = Temporal.Instant; |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents a duration of time. |
| 24 | + * This is a re-export of Temporal.Duration from temporal-polyfill. |
| 25 | + */ |
| 26 | +export type Duration = Temporal.Duration; |
| 27 | + |
| 28 | +/** |
| 29 | + * Creates a Duration from seconds. |
| 30 | + * @param seconds Number of seconds |
| 31 | + * @returns A Temporal.Duration representing the given seconds |
| 32 | + */ |
| 33 | +export function durationFromSeconds(seconds: number): Duration { |
| 34 | + return Temporal.Duration.from({ seconds }); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Converts a Duration to total seconds. |
| 39 | + * @param duration The duration to convert |
| 40 | + * @returns Total seconds (may be fractional for sub-second durations) |
| 41 | + */ |
| 42 | +export function durationToSeconds(duration: Duration): number { |
| 43 | + return duration.total({ unit: "second" }); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Creates an Instant from a Date object. |
| 48 | + * @param date JavaScript Date object |
| 49 | + * @returns A Temporal.Instant |
| 50 | + */ |
| 51 | +export function instantFromDate(date: Date): Instant { |
| 52 | + return Temporal.Instant.fromEpochMilliseconds(date.getTime()); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Creates an Instant from epoch milliseconds. |
| 57 | + * @param epochMs Unix timestamp in milliseconds |
| 58 | + * @returns A Temporal.Instant |
| 59 | + */ |
| 60 | +export function instantFromEpochMilliseconds(epochMs: number): Instant { |
| 61 | + return Temporal.Instant.fromEpochMilliseconds(epochMs); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Converts an Instant to a Date object. |
| 66 | + * @param instant The instant to convert |
| 67 | + * @returns A JavaScript Date object |
| 68 | + */ |
| 69 | +export function instantToDate(instant: Instant): Date { |
| 70 | + return new Date(instant.epochMilliseconds); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Converts an Instant to epoch milliseconds. |
| 75 | + * @param instant The instant to convert |
| 76 | + * @returns Unix timestamp in milliseconds |
| 77 | + */ |
| 78 | +export function instantToEpochMilliseconds(instant: Instant): number { |
| 79 | + return instant.epochMilliseconds; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Type guard to check if a value is a Temporal.Instant. |
| 84 | + * @param value The value to check |
| 85 | + * @returns True if the value is a Temporal.Instant |
| 86 | + */ |
| 87 | +export function isInstant(value: unknown): value is Instant { |
| 88 | + return ( |
| 89 | + value !== null && |
| 90 | + typeof value === "object" && |
| 91 | + value instanceof Temporal.Instant |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Type guard to check if a value is a Temporal.Duration. |
| 97 | + * @param value The value to check |
| 98 | + * @returns True if the value is a Temporal.Duration |
| 99 | + */ |
| 100 | +export function isDuration(value: unknown): value is Duration { |
| 101 | + return ( |
| 102 | + value !== null && |
| 103 | + typeof value === "object" && |
| 104 | + value instanceof Temporal.Duration |
| 105 | + ); |
| 106 | +} |
0 commit comments