// The capture time the upload dialog sends with a photo (owner review 2, R3), as local wall time without an offset // ("2025-05-01T06:30:00", the way the desktop clients send it): the Exif DateTimeOriginal of a JPEG or of a TIFF-based file // (TIFF, DNG and most RAW formats), else the file's modification time. The server keeps the time a photo records itself // and uses this one only for a photo that records none (a PNG, an export stripped of its Exif data), so such photos keep // their order instead of taking the moment they arrived. The reader is minimal: it follows one IFD pointer, checks every // offset against the bytes it read and gives up (null) on anything it does not expect. /** Bytes read from the start of a file: every camera writes the Exif block within them. */ const READ = 256 * 1024; /** The capture time of a picked File; null when neither the photo nor the file gives a usable one. */ export async function captureTime(file) { try { const taken = exifCaptureTime(new Uint8Array(await file.slice(0, READ).arrayBuffer())); if (taken) return taken; } catch { // Unreadable here: the file's own time stands in, and the server reads the photo again anyway. } const modified = file.lastModified; return Number.isFinite(modified) && modified > 0 ? localIso(new Date(modified)) : null; } /** The Exif DateTimeOriginal of the JPEG or TIFF whose first bytes are given, as "yyyy-MM-ddTHH:mm:ss"; null when there is none. */ export function exifCaptureTime(bytes) { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); const size = view.byteLength; let tiff = -1; if (size >= 4 && view.getUint16(0) === 0xffd8) { // JPEG: the segments before the image data, up to the APP1 that starts with "Exif\0\0". for (let at = 2; at + 4 <= size;) { const marker = view.getUint16(at); if ((marker & 0xff00) !== 0xff00 || marker === 0xffda) break; const length = view.getUint16(at + 2); if (marker === 0xffe1 && length >= 16 && at + 10 <= size && view.getUint32(at + 4) === 0x45786966 && view.getUint16(at + 8) === 0) { tiff = at + 10; break; } if (length < 2) break; at += 2 + length; } } else { tiff = 0; } if (tiff < 0 || tiff + 8 > size) return null; const order = view.getUint16(tiff); if (order !== 0x4949 && order !== 0x4d4d) return null; const little = order === 0x4949; const u16 = (at) => view.getUint16(at, little); const u32 = (at) => view.getUint32(at, little); if (u16(tiff + 2) !== 42) return null; // The 12-byte entry of a tag in the IFD at an offset from the TIFF header; -1 when it is not within reach. const entry = (ifd, tag) => { const at = tiff + ifd; if (ifd < 8 || at + 2 > size) return -1; const count = u16(at); for (let i = 0; i < count; i++) { const e = at + 2 + i * 12; if (e + 12 > size) return -1; if (u16(e) === tag) return e; } return -1; }; const exif = entry(u32(tiff + 4), 0x8769); if (exif < 0) return null; const original = entry(u32(exif + 8), 0x9003); // ASCII "YYYY:MM:DD HH:MM:SS" and its terminator, stored after the IFD. if (original < 0 || u16(original + 2) !== 2 || u32(original + 4) < 19) return null; const text = tiff + u32(original + 8); if (text + 19 > size) return null; let value = ""; for (let i = 0; i < 19; i++) value += String.fromCharCode(view.getUint8(text + i)); const m = /^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/.exec(value); if (!m) return null; const [year, month, day, hour, minute, second] = m.slice(1).map(Number); // A real date and time only ("0000:00:00 00:00:00" is what cameras write without a clock): checked in UTC, where every // wall time exists. const check = new Date(Date.UTC(year, month - 1, day, hour, minute, second)); if (year < 1900 || check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day || check.getUTCHours() !== hour || check.getUTCMinutes() !== minute || check.getUTCSeconds() !== second) return null; return `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}`; } /** Local wall time with milliseconds and no offset; null outside the years a server date takes. */ export function localIso(date) { const year = date.getFullYear(); if (!(year >= 1 && year <= 9999)) return null; const p = (n, width = 2) => String(n).padStart(width, "0"); return `${p(year, 4)}-${p(date.getMonth() + 1)}-${p(date.getDate())}T${p(date.getHours())}:${p(date.getMinutes())}:${p(date.getSeconds())}` + `.${p(date.getMilliseconds(), 3)}`; }