#273 refinements: match Immich date display, fix timeline UTC parsing, rename timezone flag

This commit is contained in:
Alan Grainger
2026-07-27 12:14:19 +12:00
parent 4ba5c2eeee
commit f8c83bc1f4
13 changed files with 174 additions and 59 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
},
"exif": {
"dateTimeOriginal": false,
"timeZone": false,
"timezone": false,
"fileName": false,
"dimensions": false,
"fileSize": false,
-3
View File
@@ -523,9 +523,6 @@ html.ipp-sidebar-open .ipp-sidebar {
opacity: 0.8;
}
.ipp-sidebar-date {
padding: 12px 0;
}
.ipp-sidebar-date-primary {
font-weight: 500;
}
+5 -6
View File
@@ -100,12 +100,11 @@ function dateLabel (key: string, mode: GroupByDateMode): string {
if (!y || !m) return key
// Intl.DateTimeFormat picks up the browser's locale; UTC timeZone keeps the
// displayed date consistent with the bucket key (which is already local).
return new Intl.DateTimeFormat(undefined, {
year: 'numeric',
month: 'long',
...(mode === 'day' ? { day: 'numeric' } : {}),
timeZone: 'UTC'
}).format(new Date(Date.UTC(y, m - 1, mode === 'day' ? (d || 1) : 1)))
// Day headers use Immich's timeline format ("Sat, 18 Oct 2025").
return new Intl.DateTimeFormat(undefined, mode === 'day'
? { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' }
: { month: 'long', year: 'numeric', timeZone: 'UTC' }
).format(new Date(Date.UTC(y, m - 1, mode === 'day' ? (d || 1) : 1)))
}
/**
+74 -33
View File
@@ -143,7 +143,7 @@ function renderContents (root: HTMLElement, item: GalleryItem | undefined) {
const exif = item.exif
if (exif && hasAnyExifField(exif)) {
root.appendChild(renderSection('details', ...renderDetailRows(exif)))
root.appendChild(renderSection('details', ...renderDetailRows(exif, item)))
}
if (exif && hasAnyLocationField(exif)) {
@@ -192,7 +192,7 @@ function renderDescription (description: string): HTMLElement {
return el
}
function renderDetailRows (exif: GalleryExif): HTMLElement[] {
function renderDetailRows (exif: GalleryExif, item: GalleryItem): HTMLElement[] {
const heading = document.createElement('h3')
heading.className = 'ipp-sidebar-heading'
heading.textContent = 'Details'
@@ -200,7 +200,7 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
const rows: HTMLElement[] = [heading]
if (exif.dateTimeOriginal) {
rows.push(renderDateRow(exif.dateTimeOriginal, exif.timeZone))
rows.push(renderDateRow(exif.dateTimeOriginal, exif.timeZone, item.localDateTime))
}
const fileRow = renderFileRow(exif)
if (fileRow) rows.push(fileRow)
@@ -212,11 +212,20 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
return rows
}
function renderDateRow (iso: string, timeZone?: string): HTMLElement {
function renderDateRow (iso: string, timeZone?: string, localDateTime?: string): HTMLElement {
const row = makeRow(ICON_CALENDAR)
row.classList.add('ipp-sidebar-date')
const body = row.querySelector('.ipp-sidebar-row-body') as HTMLElement
const formatted = formatDate(iso, timeZone)
// Mirror Immich's DetailPanelDate: with a photo timezone, show the capture
// instant in that zone (with its UTC offset); without one, show the
// photographer's wall-clock (localDateTime) rather than converting the
// instant into the viewer's timezone - keeping the sidebar consistent with
// the date-group headers, which bucket by the same wall-clock.
const formatted = timeZone
? formatDate(iso, timeZone)
: localDateTime
? formatWallClock(localDateTime)
: formatDate(iso)
const date = document.createElement('p')
date.className = 'ipp-sidebar-date-primary'
date.textContent = formatted.date
@@ -396,52 +405,84 @@ export interface FormattedDate {
time: string
}
export function formatDate (iso: string, timeZone?: string): FormattedDate {
/**
* Format a capture timestamp for the sidebar date row, in the photo's own
* timezone when one is provided (and enabled server-side).
*
* `locales` follows the browser locale when undefined; tests pass 'en-US'
* so assertions don't depend on the machine running them.
*/
export function formatDate (iso: string, timeZone?: string, locales?: string): FormattedDate {
const d = new Date(iso)
if (isNaN(d.getTime())) return { date: iso, time: '' }
if (timeZone) {
try {
if (canFormat({ timeZone })) {
// A zone Intl understands (IANA name or "+08:00" style). Show the UTC
// offset too, on browsers that support rendering it.
const timeOptions = canFormat({ timeZone, timeZoneName: 'longOffset' })
? { ...TIME_FORMAT_OPTIONS, timeZone, timeZoneName: 'longOffset' as const }
: { ...TIME_FORMAT_OPTIONS, timeZone }
return {
date: new Intl.DateTimeFormat(undefined, { ...DATE_FORMAT_OPTIONS, timeZone }).format(d),
time: new Intl.DateTimeFormat(undefined, {
...TIME_FORMAT_OPTIONS,
timeZone,
timeZoneName: 'longOffset'
}).format(d)
}
} catch (e) {
const offsetMinutes = parseFixedOffset(timeZone)
if (offsetMinutes != null) {
const localWallClock = new Date(d.getTime() + offsetMinutes * 60_000)
return {
date: new Intl.DateTimeFormat(undefined, { ...DATE_FORMAT_OPTIONS, timeZone: 'UTC' }).format(localWallClock),
time: new Intl.DateTimeFormat(undefined, { ...TIME_FORMAT_OPTIONS, timeZone: 'UTC' }).format(localWallClock) +
' ' + formatFixedOffset(offsetMinutes)
}
date: new Intl.DateTimeFormat(locales, { ...DATE_FORMAT_OPTIONS, timeZone }).format(d),
time: new Intl.DateTimeFormat(locales, timeOptions).format(d)
}
}
try {
return {
date: new Intl.DateTimeFormat(undefined, { ...DATE_FORMAT_OPTIONS, timeZone }).format(d),
time: new Intl.DateTimeFormat(undefined, { ...TIME_FORMAT_OPTIONS, timeZone }).format(d)
}
} catch (e) {
// Invalid/unknown timezone: retain the previous browser-local fallback.
// Fixed-offset values from Immich ("UTC+8") that Intl rejects: shift the
// instant manually and append the offset label ourselves. Anything else
// unrecognised falls through to browser-local formatting.
const offsetMinutes = parseFixedOffset(timeZone)
if (offsetMinutes != null) {
const localWallClock = new Date(d.getTime() + offsetMinutes * 60_000)
return {
date: new Intl.DateTimeFormat(locales, { ...DATE_FORMAT_OPTIONS, timeZone: 'UTC' }).format(localWallClock),
time: new Intl.DateTimeFormat(locales, { ...TIME_FORMAT_OPTIONS, timeZone: 'UTC' }).format(localWallClock) +
' ' + formatFixedOffset(offsetMinutes)
}
}
}
try {
return {
date: new Intl.DateTimeFormat(undefined, DATE_FORMAT_OPTIONS).format(d),
time: new Intl.DateTimeFormat(undefined, TIME_FORMAT_OPTIONS).format(d)
date: new Intl.DateTimeFormat(locales, DATE_FORMAT_OPTIONS).format(d),
time: new Intl.DateTimeFormat(locales, TIME_FORMAT_OPTIONS).format(d)
}
} catch (e) {
} catch {
return { date: iso, time: '' }
}
}
/**
* Format a timezone-agnostic wall-clock timestamp (Immich's `localDateTime`,
* whose `Z` suffix is nominal) exactly as written, with no offset label -
* every viewer sees what the camera clock showed.
*/
export function formatWallClock (iso: string, locales?: string): FormattedDate {
const d = new Date(iso)
if (isNaN(d.getTime())) return { date: iso, time: '' }
try {
return {
date: new Intl.DateTimeFormat(locales, { ...DATE_FORMAT_OPTIONS, timeZone: 'UTC' }).format(d),
time: new Intl.DateTimeFormat(locales, { ...TIME_FORMAT_OPTIONS, timeZone: 'UTC' }).format(d)
}
} catch {
return { date: iso, time: '' }
}
}
/**
* True when this browser's Intl accepts the given DateTimeFormat options -
* unknown timezones and unsupported `timeZoneName` values throw.
*/
function canFormat (options: Intl.DateTimeFormatOptions): boolean {
try {
return !!new Intl.DateTimeFormat(undefined, options)
} catch {
return false
}
}
function parseFixedOffset (timeZone: string): number | null {
const match = /^(?:UTC|GMT)([+-])(\d{1,2})(?::?(\d{2}))?$/i.exec(timeZone.trim())
if (!match) return null
+1 -1
View File
@@ -81,7 +81,7 @@ const EXIF_RULES: FieldRule[] = [
if (info.dateTimeOriginal) {
out.dateTimeOriginal = info.dateTimeOriginal
// Never expose the timezone independently of the opted-in date.
if (fieldFlag('exif', 'timeZone') && info.timeZone) out.timeZone = info.timeZone
if (fieldFlag('exif', 'timezone') && info.timeZone) out.timeZone = info.timeZone
}
}
},
+7 -2
View File
@@ -381,9 +381,14 @@ function ratioToDimensions (ratio: number): { width: number, height: number } {
* time portion reads as the photographer's local wall-clock (the `Z` suffix is
* nominal, as with Immich's own `localDateTime`). Undefined if no timestamp.
*/
function localDateTimeFromOffset (fileCreatedAt?: string, offsetHours?: number): string | undefined {
export function localDateTimeFromOffset (fileCreatedAt?: string, offsetHours?: number): string | undefined {
if (!fileCreatedAt) return undefined
const ms = Date.parse(fileCreatedAt)
// The timeline bucket API serialises fileCreatedAt as a zone-less UTC
// string ('2024-12-11T07:41:54'). Date.parse treats a zone-less date-time
// as SERVER-LOCAL time, which would skew every date by the server's UTC
// offset (e.g. 12h early on a UTC+12 host) - pin it to UTC explicitly.
const utc = /(?:[Zz]|[+-]\d{2}:?\d{2})$/.test(fileCreatedAt) ? fileCreatedAt : fileCreatedAt + 'Z'
const ms = Date.parse(utc)
if (isNaN(ms)) return undefined
return new Date(ms + (offsetHours || 0) * 3600_000).toISOString()
}
+2 -2
View File
@@ -97,7 +97,7 @@ describe('buildAssetMetadata', () => {
})
it('returns the photo timezone only when both date and timezone are enabled', () => {
setConfig({ ipp: { showMetadata: { exif: { dateTimeOriginal: true, timeZone: true } } } })
setConfig({ ipp: { showMetadata: { exif: { dateTimeOriginal: true, timezone: true } } } })
const meta = buildAssetMetadata(asset(), share())
expect(meta.exif?.dateTimeOriginal).toBe('2025-10-18T06:28:54.000Z')
expect(meta.exif?.timeZone).toBe('Asia/Singapore')
@@ -105,7 +105,7 @@ describe('buildAssetMetadata', () => {
setConfig({ ipp: { showMetadata: { exif: { dateTimeOriginal: true } } } })
expect(buildAssetMetadata(asset(), share()).exif?.timeZone).toBeUndefined()
setConfig({ ipp: { showMetadata: { exif: { timeZone: true } } } })
setConfig({ ipp: { showMetadata: { exif: { timezone: true } } } })
expect(buildAssetMetadata(asset(), share()).exif).toBeUndefined()
})
+46
View File
@@ -0,0 +1,46 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { localDateTimeFromOffset } from '../src/immich'
/*
Regression test for the timeline date-grouping skew: Immich's timeline
bucket API returns fileCreatedAt as a zone-less UTC string
('2024-12-11T07:41:54'), which Date.parse treats as server-local time.
On a non-UTC host every reconstructed localDateTime shifted by the
host's UTC offset, putting assets under the wrong date-group header
(e.g. a "10 Dec" header over photos whose sidebar said 11 Dec).
TZ is pinned to a far-from-UTC zone so the suite fails if the parse
ever becomes host-local again, regardless of the machine running it.
*/
const originalTz = process.env.TZ
beforeAll(() => {
process.env.TZ = 'Pacific/Auckland' // UTC+12/+13
})
afterAll(() => {
if (originalTz === undefined) delete process.env.TZ
else process.env.TZ = originalTz
})
describe('localDateTimeFromOffset', () => {
it('treats zone-less timeline timestamps as UTC, not server-local', () => {
expect(localDateTimeFromOffset('2024-12-11T07:41:54', 0)).toBe('2024-12-11T07:41:54.000Z')
})
it('applies the photographer offset to the UTC instant', () => {
expect(localDateTimeFromOffset('2024-12-10T15:30:00', 8)).toBe('2024-12-10T23:30:00.000Z')
expect(localDateTimeFromOffset('2024-12-11T03:30:00', -8)).toBe('2024-12-10T19:30:00.000Z')
})
it('leaves explicitly zoned timestamps alone', () => {
expect(localDateTimeFromOffset('2024-12-11T07:41:54Z', 0)).toBe('2024-12-11T07:41:54.000Z')
expect(localDateTimeFromOffset('2024-12-11T07:41:54+00:00', 0)).toBe('2024-12-11T07:41:54.000Z')
})
it('returns undefined for missing or invalid input', () => {
expect(localDateTimeFromOffset(undefined, 0)).toBeUndefined()
expect(localDateTimeFromOffset('not-a-date', 0)).toBeUndefined()
})
})
+1 -1
View File
@@ -186,7 +186,7 @@ describe('metadataEnabled shim', () => {
const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown>
expect(exif.enabled).toBeUndefined()
expect(exif.dateTimeOriginal).toBe(true)
expect(exif.timeZone).toBeUndefined()
expect(exif.timezone).toBeUndefined()
expect(exif.fileName).toBe(true)
expect(exif.dimensions).toBe(true)
expect(exif.fileSize).toBe(true)
+28 -4
View File
@@ -1,11 +1,14 @@
import { describe, expect, it } from 'vitest'
import { formatDate } from '../src/client/sidebar'
import { formatDate, formatWallClock } from '../src/client/sidebar'
// The locale is pinned to en-US throughout: formatDate follows the system
// locale by default, which would make these assertions machine-dependent.
describe('sidebar date formatting', () => {
const capturedAt = '2025-10-18T06:28:54.000Z'
it('formats the capture instant in the photo IANA timezone', () => {
const formatted = formatDate(capturedAt, 'Asia/Singapore')
const formatted = formatDate(capturedAt, 'Asia/Singapore', 'en-US')
expect(formatted.date).toBe('Oct 18, 2025')
expect(formatted.time).toContain('Sat')
expect(formatted.time).toContain('2:28:54 PM')
@@ -13,13 +16,34 @@ describe('sidebar date formatting', () => {
})
it('supports legacy fixed-offset timezones from Immich', () => {
const formatted = formatDate(capturedAt, 'UTC+8')
const formatted = formatDate(capturedAt, 'UTC+8', 'en-US')
expect(formatted.date).toBe('Oct 18, 2025')
expect(formatted.time).toContain('2:28:54 PM')
expect(formatted.time).toContain('GMT+08:00')
})
it('falls back to local formatting for an unknown timezone', () => {
const formatted = formatDate(capturedAt, 'Not/AZone', 'en-US')
expect(formatted.date).toMatch(/Oct 1[78], 2025/)
expect(formatted.time).toContain(':28:54')
})
it('retains the original value when the timestamp is invalid', () => {
expect(formatDate('not-a-date', 'Asia/Singapore')).toEqual({ date: 'not-a-date', time: '' })
expect(formatDate('not-a-date', 'Asia/Singapore', 'en-US')).toEqual({ date: 'not-a-date', time: '' })
})
})
describe('sidebar wall-clock formatting', () => {
// localDateTime carries the photographer's wall-clock with a nominal Z;
// it must render exactly as written, with no offset label, for any viewer.
it('renders the wall-clock as written with no offset label', () => {
const formatted = formatWallClock('2024-12-10T23:30:00.000Z', 'en-US')
expect(formatted.date).toBe('Dec 10, 2024')
expect(formatted.time).toContain('11:30:00 PM')
expect(formatted.time).not.toContain('GMT')
})
it('retains the original value when the timestamp is invalid', () => {
expect(formatWallClock('not-a-date', 'en-US')).toEqual({ date: 'not-a-date', time: '' })
})
})