refactor: add date utility for consistent UTC handling

This commit is contained in:
Sam Chau
2026-01-17 14:31:48 +10:30
parent 3d1e55e46c
commit 7c1952d264
7 changed files with 103 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount, onDestroy } from 'svelte';
import { Clock, Timer, CheckCircle, PauseCircle } from 'lucide-svelte';
import { parseUTC } from '$shared/dates';
export let enabled: boolean;
export let schedule: number; // minutes
@@ -20,8 +21,7 @@
});
// Calculate times
// Database stores timestamps without timezone - append Z to parse as UTC
$: lastRunTime = lastRunAt ? new Date(lastRunAt.endsWith('Z') ? lastRunAt : lastRunAt + 'Z').getTime() : null;
$: lastRunTime = parseUTC(lastRunAt)?.getTime() ?? null;
$: scheduleMs = schedule * 60 * 1000;
$: nextRunTime = lastRunTime ? lastRunTime + scheduleMs : null;
$: timeUntilNext = nextRunTime ? nextRunTime - now : null;
@@ -49,14 +49,10 @@
return `${seconds}s`;
}
// Parse timestamp - append Z if needed to treat as UTC
function parseTimestamp(str: string): Date {
return new Date(str.endsWith('Z') ? str : str + 'Z');
}
// Format absolute time
function formatTime(isoString: string): string {
const date = parseTimestamp(isoString);
const date = parseUTC(isoString);
if (!date) return '-';
return date.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
@@ -65,7 +61,8 @@
}
function formatDate(isoString: string): string {
const date = parseTimestamp(isoString);
const date = parseUTC(isoString);
if (!date) return '-';
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);

View File

@@ -13,6 +13,7 @@
import SearchAction from '$ui/actions/SearchAction.svelte';
import { createSearchStore } from '$stores/search';
import { alertStore } from '$alerts/store';
import { parseUTC } from '$shared/dates';
import type { PageData } from './$types';
import type { Column } from '$ui/table/types';
import type { DatabaseInstance } from '$db/queries/databaseInstances.ts';
@@ -66,8 +67,14 @@
// Format last synced date
function formatLastSynced(date: string | null): string {
if (!date) return 'Never';
return new Date(date).toLocaleDateString();
const d = parseUTC(date);
if (!d) return 'Never';
return d.toLocaleString(undefined, {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit'
});
}
// Handle row click - navigate to database details

View File

@@ -5,12 +5,13 @@
import { Tag, Clock, Zap, Shield, Calendar } from 'lucide-svelte';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { parseUTC } from '$shared/dates';
export let profiles: DelayProfileTableRow[];
function formatDate(dateString: string): string {
// SQLite stores timestamps without timezone info, treat as UTC
const date = new Date(dateString + 'Z');
const date = parseUTC(dateString);
if (!date) return '-';
return date.toLocaleString(undefined, {
year: 'numeric',
month: 'short',

View File

@@ -6,12 +6,13 @@
import { marked } from 'marked';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { parseUTC } from '$shared/dates';
export let expressions: RegularExpressionTableRow[];
function formatDate(dateString: string): string {
// SQLite stores timestamps without timezone info, treat as UTC
const date = new Date(dateString + 'Z');
const date = parseUTC(dateString);
if (!date) return '-';
return date.toLocaleString(undefined, {
year: 'numeric',
month: 'short',

View File

@@ -1,14 +1,14 @@
<script lang="ts">
import { Bell } from 'lucide-svelte';
import { parseUTC } from '$shared/dates';
import type { NotificationHistoryRecord } from '$db/queries/notificationHistory.ts';
export let history: NotificationHistoryRecord[];
export let services: Array<{ id: string; name: string }>;
function formatDateTime(date: string): string {
// SQLite stores as UTC without timezone indicator, append Z to parse correctly
const utcDate = date.endsWith('Z') ? date : date.replace(' ', 'T') + 'Z';
return new Date(utcDate).toLocaleString();
const d = parseUTC(date);
return d ? d.toLocaleString() : '-';
}
function getServiceName(serviceId: string): string {