Skip to content

Commit 4590dab

Browse files
feat: update timestamp formatting to respect user's local timezone
1 parent 579def5 commit 4590dab

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/lib/components/GtfsRtLogViewer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
}
9696
9797
function formatTimestamp(ts: string) {
98-
return new Date(ts).toLocaleString();
98+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
99+
return new Date(ts).toLocaleString('en-US', { timeZone });
99100
}
100101
101102
const selectedData = $derived.by(() => {

src/lib/components/KeyLogViewer.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@
237237
function formatTimestamp(ts: string): string {
238238
try {
239239
const date = new Date(ts);
240-
return date.toLocaleString();
240+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
241+
return date.toLocaleString('en-US', { timeZone });
241242
} catch {
242243
return ts;
243244
}

src/lib/utils/date.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export function formatTimestamp(
4242
hour12: false
4343
};
4444

45-
const local = date.toLocaleString(undefined, localParams);
45+
const timeZone: string | undefined =
46+
typeof Intl !== 'undefined' ? Intl.DateTimeFormat().resolvedOptions().timeZone : undefined;
47+
const local = date.toLocaleString('en-US', { ...localParams, timeZone });
4648
const utc = date
4749
.toISOString()
4850
.replace('T', ' ')
@@ -53,10 +55,11 @@ export function formatTimestamp(
5355
const hour = date.getHours();
5456
if (hour < 4) {
5557
const prevDate = new Date(num - 24 * 60 * 60 * 1000);
56-
const prevDateStr = prevDate.toLocaleDateString(undefined, {
58+
const prevDateStr = prevDate.toLocaleDateString('en-US', {
5759
year: 'numeric',
5860
month: 'short',
59-
day: 'numeric'
61+
day: 'numeric',
62+
timeZone
6063
});
6164
const serviceHour = hour + 24;
6265
const minutes = date.getMinutes().toString().padStart(2, '0');

0 commit comments

Comments
 (0)