Add date time with timezone offset option

This commit is contained in:
klymp
2026-07-26 16:54:12 +02:00
parent aa6183a95d
commit 1533d3de6d
11 changed files with 173 additions and 33 deletions
+1
View File
@@ -18,6 +18,7 @@
}, },
"exif": { "exif": {
"dateTimeOriginal": false, "dateTimeOriginal": false,
"timeZone": false,
"fileName": false, "fileName": false,
"dimensions": false, "dimensions": false,
"fileSize": false, "fileSize": false,
+5
View File
@@ -525,8 +525,13 @@ html.ipp-sidebar-open .ipp-sidebar {
.ipp-sidebar-date { .ipp-sidebar-date {
padding: 12px 0; padding: 12px 0;
}
.ipp-sidebar-date-primary {
font-weight: 500; font-weight: 500;
} }
.ipp-sidebar-row-body .ipp-sidebar-date-secondary {
margin-bottom: 0;
}
.ipp-sidebar-coords { .ipp-sidebar-coords {
font-size: 13px; font-size: 13px;
+1
View File
@@ -20,6 +20,7 @@ export const ICON_CLOSE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fil
// Sidebar field rows (file / camera / lens / map) // Sidebar field rows (file / camera / lens / map)
export const ICON_IMAGE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M8.5,13.5L11,16.5L14.5,12L19,18H5M21,19V5C21,3.89 20.1,3 19,3H5A2,2 0 0,0 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19Z"/></svg>' export const ICON_IMAGE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M8.5,13.5L11,16.5L14.5,12L19,18H5M21,19V5C21,3.89 20.1,3 19,3H5A2,2 0 0,0 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19Z"/></svg>'
export const ICON_CALENDAR = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M19,19H5V8H19M16,1V3H8V1H6V3H5C3.89,3 3,3.89 3,5V19C3,20.1 3.9,21 5,21H19C20.1,21 21,20.1 21,19V5C21,3.89 20.1,3 19,3H18V1M19,6H5V5H19V6Z"/></svg>'
export const ICON_CAMERA = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M4,4H7L9,2H15L17,4H20A2,2 0 0,1 22,6V18A2,2 0 0,1 20,20H4A2,2 0 0,1 2,18V6A2,2 0 0,1 4,4M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9Z"/></svg>' export const ICON_CAMERA = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M4,4H7L9,2H15L17,4H20A2,2 0 0,1 22,6V18A2,2 0 0,1 20,20H4A2,2 0 0,1 2,18V6A2,2 0 0,1 4,4M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9Z"/></svg>'
export const ICON_IRIS = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M13.73,15L9.83,21.76C10.53,21.91 11.25,22 12,22C14.4,22 16.6,21.15 18.32,19.75L14.66,13.4M2.46,15C3.38,17.92 5.61,20.26 8.45,21.34L12.12,15M8.54,12L4.64,5.25C3,7 2,9.39 2,12C2,12.68 2.07,13.35 2.2,14H9.69M21.8,10H14.31L14.6,10.5L19.36,18.75C21,16.97 22,14.6 22,12C22,11.31 21.93,10.64 21.8,10M21.54,9C20.62,6.07 18.39,3.74 15.55,2.66L11.88,9M9.4,10.5L14.17,2.24C13.47,2.09 12.75,2 12,2C9.6,2 7.4,2.84 5.68,4.25L9.34,10.6L9.4,10.5Z"/></svg>' export const ICON_IRIS = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M13.73,15L9.83,21.76C10.53,21.91 11.25,22 12,22C14.4,22 16.6,21.15 18.32,19.75L14.66,13.4M2.46,15C3.38,17.92 5.61,20.26 8.45,21.34L12.12,15M8.54,12L4.64,5.25C3,7 2,9.39 2,12C2,12.68 2.07,13.35 2.2,14H9.69M21.8,10H14.31L14.6,10.5L19.36,18.75C21,16.97 22,14.6 22,12C22,11.31 21.93,10.64 21.8,10M21.54,9C20.62,6.07 18.39,3.74 15.55,2.66L11.88,9M9.4,10.5L14.17,2.24C13.47,2.09 12.75,2 12,2C9.6,2 7.4,2.84 5.68,4.25L9.34,10.6L9.4,10.5Z"/></svg>'
export const ICON_MAP = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M12,11.5A2.5,2.5 0 0,1 9.5,9A2.5,2.5 0 0,1 12,6.5A2.5,2.5 0 0,1 14.5,9A2.5,2.5 0 0,1 12,11.5M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12,2Z"/></svg>' export const ICON_MAP = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M12,11.5A2.5,2.5 0 0,1 9.5,9A2.5,2.5 0 0,1 12,6.5A2.5,2.5 0 0,1 14.5,9A2.5,2.5 0 0,1 12,11.5M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12,2Z"/></svg>'
+95 -18
View File
@@ -5,7 +5,7 @@
// by the toolbar info button or the `i` key, persisted via localStorage. // by the toolbar info button or the `i` key, persisted via localStorage.
import { state, SIDEBAR_STORAGE_KEY } from './state.js' import { state, SIDEBAR_STORAGE_KEY } from './state.js'
import { ICON_INFO, ICON_CLOSE, ICON_IMAGE, ICON_CAMERA, ICON_IRIS, ICON_MAP } from './icons.js' import { ICON_INFO, ICON_CLOSE, ICON_IMAGE, ICON_CALENDAR, ICON_CAMERA, ICON_IRIS, ICON_MAP } from './icons.js'
import type { GalleryItem, GalleryExif } from '../shared/types.js' import type { GalleryItem, GalleryExif } from '../shared/types.js'
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -200,7 +200,7 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
const rows: HTMLElement[] = [heading] const rows: HTMLElement[] = [heading]
if (exif.dateTimeOriginal) { if (exif.dateTimeOriginal) {
rows.push(renderDateRow(exif.dateTimeOriginal)) rows.push(renderDateRow(exif.dateTimeOriginal, exif.timeZone))
} }
const fileRow = renderFileRow(exif) const fileRow = renderFileRow(exif)
if (fileRow) rows.push(fileRow) if (fileRow) rows.push(fileRow)
@@ -212,10 +212,21 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
return rows return rows
} }
function renderDateRow (iso: string): HTMLElement { function renderDateRow (iso: string, timeZone?: string): HTMLElement {
const row = document.createElement('div') const row = makeRow(ICON_CALENDAR)
row.className = 'ipp-sidebar-row ipp-sidebar-date' row.classList.add('ipp-sidebar-date')
row.textContent = formatDate(iso) const body = row.querySelector('.ipp-sidebar-row-body') as HTMLElement
const formatted = formatDate(iso, timeZone)
const date = document.createElement('p')
date.className = 'ipp-sidebar-date-primary'
date.textContent = formatted.date
body.appendChild(date)
if (formatted.time) {
const time = document.createElement('p')
time.className = 'ipp-sidebar-date-secondary'
time.textContent = formatted.time
body.appendChild(time)
}
return row return row
} }
@@ -367,20 +378,86 @@ function hasAnyLocationField (exif: GalleryExif): boolean {
(exif.latitude != null && exif.longitude != null)) (exif.latitude != null && exif.longitude != null))
} }
function formatDate (iso: string): string { const DATE_FORMAT_OPTIONS: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric'
}
const TIME_FORMAT_OPTIONS: Intl.DateTimeFormatOptions = {
weekday: 'short',
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
}
export interface FormattedDate {
date: string
time: string
}
export function formatDate (iso: string, timeZone?: string): FormattedDate {
const d = new Date(iso) const d = new Date(iso)
if (isNaN(d.getTime())) return iso if (isNaN(d.getTime())) return { date: iso, time: '' }
try {
return new Intl.DateTimeFormat(undefined, { if (timeZone) {
year: 'numeric', try {
month: 'long', return {
day: 'numeric', date: new Intl.DateTimeFormat(undefined, { ...DATE_FORMAT_OPTIONS, timeZone }).format(d),
hour: 'numeric', time: new Intl.DateTimeFormat(undefined, {
minute: '2-digit' ...TIME_FORMAT_OPTIONS,
}).format(d) timeZone,
} catch (e) { timeZoneName: 'longOffset'
return iso }).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)
}
}
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.
}
}
} }
try {
return {
date: new Intl.DateTimeFormat(undefined, DATE_FORMAT_OPTIONS).format(d),
time: new Intl.DateTimeFormat(undefined, TIME_FORMAT_OPTIONS).format(d)
}
} catch (e) {
return { date: iso, time: '' }
}
}
function parseFixedOffset (timeZone: string): number | null {
const match = /^(?:UTC|GMT)([+-])(\d{1,2})(?::?(\d{2}))?$/i.exec(timeZone.trim())
if (!match) return null
const hours = Number(match[2])
const minutes = Number(match[3] || 0)
if (hours > 14 || minutes > 59 || (hours === 14 && minutes !== 0)) return null
const total = hours * 60 + minutes
return match[1] === '-' ? -total : total
}
function formatFixedOffset (offsetMinutes: number): string {
const sign = offsetMinutes < 0 ? '-' : '+'
const absolute = Math.abs(offsetMinutes)
const hours = Math.floor(absolute / 60).toString().padStart(2, '0')
const minutes = (absolute % 60).toString().padStart(2, '0')
return `GMT${sign}${hours}:${minutes}`
} }
function formatBytes (bytes: number): string { function formatBytes (bytes: number): string {
+11 -1
View File
@@ -42,7 +42,7 @@ function copy (key: keyof ExifInfo & keyof GalleryExif): FieldRule {
`enabled: true` users). `enabled: true` users).
*/ */
const EXIF_FIELDS = [ const EXIF_FIELDS = [
'dateTimeOriginal', 'make', 'model', 'lensModel', 'make', 'model', 'lensModel',
'exposureTime', 'iso', 'fNumber', 'focalLength' 'exposureTime', 'iso', 'fNumber', 'focalLength'
] as const ] as const
@@ -50,6 +50,16 @@ const LOCATION_FIELDS = ['city', 'state', 'country'] as const
const EXIF_RULES: FieldRule[] = [ const EXIF_RULES: FieldRule[] = [
...EXIF_FIELDS.map(copy), ...EXIF_FIELDS.map(copy),
{
flag: 'dateTimeOriginal',
emit: (out, info) => {
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
}
}
},
{ {
flag: 'fileName', flag: 'fileName',
emit: (out, _info, asset) => { emit: (out, _info, asset) => {
+1
View File
@@ -16,6 +16,7 @@ export type GroupByDateMode = 'month' | 'day'
export interface GalleryExif { export interface GalleryExif {
dateTimeOriginal?: string // ISO date string; client formats with Intl.DateTimeFormat dateTimeOriginal?: string // ISO date string; client formats with Intl.DateTimeFormat
timeZone?: string
fileName?: string fileName?: string
width?: number width?: number
height?: number height?: number
+1
View File
@@ -20,6 +20,7 @@ export interface ExifInfo {
// Additional EXIF fields surfaced by Immich for the metadata sidebar // Additional EXIF fields surfaced by Immich for the metadata sidebar
// (gated server-side by ipp.showMetadata.exif.* and .location.* config). // (gated server-side by ipp.showMetadata.exif.* and .location.* config).
dateTimeOriginal?: string | null; dateTimeOriginal?: string | null;
timeZone?: string | null;
fileSizeInByte?: number | null; fileSizeInByte?: number | null;
make?: string | null; make?: string | null;
model?: string | null; model?: string | null;
+15
View File
@@ -22,6 +22,8 @@ const asset = (): Asset => ({
originalMimeType: 'image/heic', originalMimeType: 'image/heic',
exifInfo: { exifInfo: {
description: 'A nice photo', description: 'A nice photo',
dateTimeOriginal: '2025-10-18T06:28:54.000Z',
timeZone: 'Asia/Singapore',
make: 'Canon', make: 'Canon',
model: 'EOS R5', model: 'EOS R5',
city: 'Paris', city: 'Paris',
@@ -55,6 +57,19 @@ describe('buildAssetMetadata', () => {
expect(meta.exif?.city).toBeUndefined() expect(meta.exif?.city).toBeUndefined()
}) })
it('returns the photo timezone only when both date and timezone are enabled', () => {
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')
setConfig({ ipp: { showMetadata: { exif: { dateTimeOriginal: true } } } })
expect(buildAssetMetadata(asset(), share()).exif?.timeZone).toBeUndefined()
setConfig({ ipp: { showMetadata: { exif: { timeZone: true } } } })
expect(buildAssetMetadata(asset(), share()).exif).toBeUndefined()
})
it('returns description when a description surface is enabled', () => { it('returns description when a description surface is enabled', () => {
setConfig({ ipp: { showMetadata: { description: { sidebar: true } } } }) setConfig({ ipp: { showMetadata: { description: { sidebar: true } } } })
const meta = buildAssetMetadata(asset(), share()) const meta = buildAssetMetadata(asset(), share())
+1
View File
@@ -186,6 +186,7 @@ describe('metadataEnabled shim', () => {
const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown> const exif = ((config.ipp as Record<string, unknown>).showMetadata as Record<string, unknown>).exif as Record<string, unknown>
expect(exif.enabled).toBeUndefined() expect(exif.enabled).toBeUndefined()
expect(exif.dateTimeOriginal).toBe(true) expect(exif.dateTimeOriginal).toBe(true)
expect(exif.timeZone).toBeUndefined()
expect(exif.fileName).toBe(true) expect(exif.fileName).toBe(true)
expect(exif.dimensions).toBe(true) expect(exif.dimensions).toBe(true)
expect(exif.fileSize).toBe(true) expect(exif.fileSize).toBe(true)
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { formatDate } from '../src/client/sidebar'
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')
expect(formatted.date).toBe('Oct 18, 2025')
expect(formatted.time).toContain('Sat')
expect(formatted.time).toContain('2:28:54 PM')
expect(formatted.time).toContain('GMT+08:00')
})
it('supports legacy fixed-offset timezones from Immich', () => {
const formatted = formatDate(capturedAt, 'UTC+8')
expect(formatted.date).toBe('Oct 18, 2025')
expect(formatted.time).toContain('2:28:54 PM')
expect(formatted.time).toContain('GMT+08:00')
})
it('retains the original value when the timestamp is invalid', () => {
expect(formatDate('not-a-date', 'Asia/Singapore')).toEqual({ date: 'not-a-date', time: '' })
})
})
+17 -14
View File
@@ -13,6 +13,7 @@ Show the description in the sidebar only (not as a lightbox caption), expose cam
"description": { "caption": false, "sidebar": true }, "description": { "caption": false, "sidebar": true },
"exif": { "exif": {
"dateTimeOriginal": true, "dateTimeOriginal": true,
"timeZone": true,
"make": true, "make": true,
"model": true, "model": true,
"lensModel": true, "lensModel": true,
@@ -39,7 +40,7 @@ Every per-field flag defaults to `false`. A field is sent to the client only whe
The share owner's **"Show metadata"** toggle in Immich takes precedence over everything here: when it's off, IPP suppresses all description / EXIF / location output and hides the info sidebar (and its toolbar toggle) entirely, regardless of these settings. Note that with "Show metadata" off Immich also strips file creation dates from non-album shares, so [date grouping](/config/gallery#groupbydate) won't work for those shares. The share owner's **"Show metadata"** toggle in Immich takes precedence over everything here: when it's off, IPP suppresses all description / EXIF / location output and hides the info sidebar (and its toolbar toggle) entirely, regardless of these settings. Note that with "Show metadata" off Immich also strips file creation dates from non-album shares, so [date grouping](/config/gallery#groupbydate) won't work for those shares.
The info sidebar (and its toolbar toggle button) only appear when there is at least one section the operator has opted into - i.e. `description.sidebar` is true, or `exif` / `location` has at least one flag set to `true`. With all three off, the sidebar UI is suppressed. The info sidebar (and its toolbar toggle button) only appear when there is at least one section the operator has opted into - i.e. `description.sidebar` is true, or `exif` / `location` has at least one displayable field set to `true`. Dependent flags such as `exif.timeZone` do not activate the sidebar on their own. With all three groups off, the sidebar UI is suppressed.
## Description ## Description
@@ -56,19 +57,20 @@ Set both to show in both places; set neither and the description is not included
Under `ipp.showMetadata.exif`. Every flag defaults to `false`. Under `ipp.showMetadata.exif`. Every flag defaults to `false`.
| Option | Type | Description | | Option | Type | Description |
|--------------------|--------|-----------------------------------------------| |--------------------|--------|------------------------------------------------------------------------------------------------------------------|
| `dateTimeOriginal` | `bool` | Show the date the photo was taken (per EXIF). | | `dateTimeOriginal` | `bool` | Show the date the photo was taken (per EXIF). |
| `fileName` | `bool` | Show the original filename. | | `timeZone` | `bool` | Show and use the photo's timezone when formatting the date. Only applies when `dateTimeOriginal` is also enabled. |
| `dimensions` | `bool` | Show width x height and megapixel count. | | `fileName` | `bool` | Show the original filename. |
| `fileSize` | `bool` | Show the file size. | | `dimensions` | `bool` | Show width x height and megapixel count. |
| `make` | `bool` | Camera manufacturer (e.g. "Canon"). | | `fileSize` | `bool` | Show the file size. |
| `model` | `bool` | Camera model (e.g. "EOS R5"). | | `make` | `bool` | Camera manufacturer (e.g. "Canon"). |
| `lensModel` | `bool` | Lens model. | | `model` | `bool` | Camera model (e.g. "EOS R5"). |
| `exposureTime` | `bool` | Shutter speed (e.g. "1/200"). | | `lensModel` | `bool` | Lens model. |
| `iso` | `bool` | ISO sensitivity. | | `exposureTime` | `bool` | Shutter speed (e.g. "1/200"). |
| `fNumber` | `bool` | Aperture f-number. | | `iso` | `bool` | ISO sensitivity. |
| `focalLength` | `bool` | Focal length in millimetres. | | `fNumber` | `bool` | Aperture f-number. |
| `focalLength` | `bool` | Focal length in millimetres. |
## Location group ## Location group
@@ -93,6 +95,7 @@ Show the description in the sidebar only (not as a lightbox caption), expose cam
"description": { "caption": false, "sidebar": true }, "description": { "caption": false, "sidebar": true },
"exif": { "exif": {
"dateTimeOriginal": true, "dateTimeOriginal": true,
"timeZone": true,
"make": true, "make": true,
"model": true, "model": true,
"lensModel": true, "lensModel": true,