Merge pull request #273 from kylmp/exif-timezone
# Conflicts: # app/tests/assetMetadata.test.ts
This commit is contained in:
@@ -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)
|
||||
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_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>'
|
||||
|
||||
+95
-18
@@ -5,7 +5,7 @@
|
||||
// by the toolbar info button or the `i` key, persisted via localStorage.
|
||||
|
||||
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'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -200,7 +200,7 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
|
||||
const rows: HTMLElement[] = [heading]
|
||||
|
||||
if (exif.dateTimeOriginal) {
|
||||
rows.push(renderDateRow(exif.dateTimeOriginal))
|
||||
rows.push(renderDateRow(exif.dateTimeOriginal, exif.timeZone))
|
||||
}
|
||||
const fileRow = renderFileRow(exif)
|
||||
if (fileRow) rows.push(fileRow)
|
||||
@@ -212,10 +212,21 @@ function renderDetailRows (exif: GalleryExif): HTMLElement[] {
|
||||
return rows
|
||||
}
|
||||
|
||||
function renderDateRow (iso: string): HTMLElement {
|
||||
const row = document.createElement('div')
|
||||
row.className = 'ipp-sidebar-row ipp-sidebar-date'
|
||||
row.textContent = formatDate(iso)
|
||||
function renderDateRow (iso: string, timeZone?: 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)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -367,20 +378,86 @@ function hasAnyLocationField (exif: GalleryExif): boolean {
|
||||
(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)
|
||||
if (isNaN(d.getTime())) return iso
|
||||
try {
|
||||
return new Intl.DateTimeFormat(undefined, {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: 'numeric',
|
||||
minute: '2-digit'
|
||||
}).format(d)
|
||||
} catch (e) {
|
||||
return iso
|
||||
if (isNaN(d.getTime())) return { date: iso, time: '' }
|
||||
|
||||
if (timeZone) {
|
||||
try {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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}`
|
||||
}
|
||||
|
||||
export function formatBytes (bytes: number): string {
|
||||
|
||||
Reference in New Issue
Block a user