mirror of
https://github.com/Dictionarry-Hub/profilarr.git
synced 2026-01-22 19:01:02 +01:00
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
/**
|
|
* Date utilities for handling SQLite timestamps
|
|
*
|
|
* SQLite stores timestamps from CURRENT_TIMESTAMP as UTC in the format
|
|
* "YYYY-MM-DD HH:MM:SS" (no timezone indicator). JavaScript's Date constructor
|
|
* interprets strings without timezone info as local time, causing incorrect
|
|
* display.
|
|
*
|
|
* These utilities normalize SQLite timestamps to proper ISO 8601 format
|
|
* so JavaScript correctly interprets them as UTC.
|
|
*/
|
|
|
|
/**
|
|
* Normalizes a SQLite timestamp to ISO 8601 format with UTC indicator.
|
|
* Handles both SQLite format ("YYYY-MM-DD HH:MM:SS") and ISO format.
|
|
*
|
|
* @param timestamp - SQLite or ISO timestamp string
|
|
* @returns ISO 8601 formatted string with Z suffix, or null if input is null/undefined
|
|
*
|
|
* @example
|
|
* toUTC("2026-01-17 03:21:52") // "2026-01-17T03:21:52Z"
|
|
* toUTC("2026-01-17T03:21:52") // "2026-01-17T03:21:52Z"
|
|
* toUTC("2026-01-17T03:21:52Z") // "2026-01-17T03:21:52Z" (unchanged)
|
|
* toUTC(null) // null
|
|
*/
|
|
export function toUTC(timestamp: string | null | undefined): string | null {
|
|
if (!timestamp) return null;
|
|
|
|
// Already has Z suffix - return as-is
|
|
if (timestamp.endsWith('Z')) return timestamp;
|
|
|
|
// Replace space with T (SQLite format) and add Z
|
|
return timestamp.replace(' ', 'T') + 'Z';
|
|
}
|
|
|
|
/**
|
|
* Parses a SQLite timestamp into a JavaScript Date object.
|
|
* Normalizes the timestamp to UTC before parsing.
|
|
*
|
|
* @param timestamp - SQLite or ISO timestamp string
|
|
* @returns Date object, or null if input is null/undefined
|
|
*
|
|
* @example
|
|
* parseUTC("2026-01-17 03:21:52") // Date object in UTC
|
|
* parseUTC(null) // null
|
|
*/
|
|
export function parseUTC(timestamp: string | null | undefined): Date | null {
|
|
const normalized = toUTC(timestamp);
|
|
if (!normalized) return null;
|
|
return new Date(normalized);
|
|
}
|