diff --git a/server/src/nest/journey/journey-entries-from-photos.helpers.ts b/server/src/nest/journey/journey-entries-from-photos.helpers.ts index c638a3f41..6e0360047 100644 --- a/server/src/nest/journey/journey-entries-from-photos.helpers.ts +++ b/server/src/nest/journey/journey-entries-from-photos.helpers.ts @@ -1,5 +1,6 @@ import { haversineKm } from '../common/geo'; import { localParts, resolveTimeZone } from '../common/timezoneService'; +import { getIntlLanguage, SUPPORTED_LANGUAGE_CODES } from '@trek/shared'; /** * The pure half of making journal entries from gallery photos (#1920): when @@ -56,7 +57,25 @@ export interface ClusterSummary { const BORROWED_ZONE_WINDOW_MS = 72 * 60 * 60 * 1000; /** An offset written after a clock time: `+02:00`, `-0530`. `Z` is not one (see {@link carriedOffsetMinutes}). */ -const CARRIED_OFFSET = /T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?([+-])(\d{2}):?(\d{2})$/; +const CARRIED_OFFSET = /[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?([+-])(\d{2}):?(\d{2})$/; + +/** A clock time with neither `Z` nor an offset after it: `2026-05-14T09:00:00`, `2026-05-14 09:00`. */ +const ZONELESS_STAMP = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)$/; + +/** + * The instant a stored capture time names, in ms since the epoch, or NaN. + * + * A stamp without `Z` and without an offset is read as UTC, as every writer + * of `taken_at` stores the instant in UTC. `Date.parse` would read it on the + * server's own clock, so the same photo would land on another day depending + * on the TZ the instance runs in. + */ +export function captureInstant(takenAt: string): number { + // A space between day and time, as SQL writes it, is the `T` Date.parse reliably reads. + const text = takenAt.trim().replace(/^(\d{4}-\d{2}-\d{2}) /, '$1T'); + const zoneless = ZONELESS_STAMP.exec(text); + return Date.parse(zoneless ? `${zoneless[1]}T${zoneless[2]}Z` : text); +} /** * The offset a stored capture time carries, in minutes east of UTC, or null. @@ -105,7 +124,7 @@ function atOffset(instant: number, minutes: number): { date: string; time: strin */ export function localCaptures(photos: readonly FromPhotosPhoto[], fallbackZone: string | null): LocalCapture[] { const parsed = photos - .map((photo) => ({ photo, instant: Date.parse(photo.takenAt) })) + .map((photo) => ({ photo, instant: captureInstant(photo.takenAt) })) .filter((p) => Number.isFinite(p.instant)); // The zones the photos' own positions name, for those without one to borrow. const geoZones = parsed @@ -297,14 +316,11 @@ export function fallbackTitle( } /** - * The two TREK language codes that are not the BCP-47 tag of their language: - * `br` is Brazilian Portuguese (bare `br` is Breton) and `gr` is Greek. An MCP - * client may send either; the app sends the BCP-47 locale. + * The BCP-47 tag Intl reads for a language a caller named. A TREK language + * code goes through the shared map (`br` is Brazilian Portuguese, not Breton, + * and `gr` is Greek); the app sends the BCP-47 locale, which passes as it is. */ -const TREK_LANGUAGE_TAGS: Readonly> = { br: 'pt-BR', gr: 'el' }; - -/** The BCP-47 tag Intl reads for a language a caller named. */ export function intlLanguage(lang: string | undefined): string | undefined { if (!lang) return undefined; - return TREK_LANGUAGE_TAGS[lang] ?? lang; + return SUPPORTED_LANGUAGE_CODES.includes(lang) ? getIntlLanguage(lang) : lang; } diff --git a/server/tests/unit/nest/journey/journey-entries-from-photos.helpers.test.ts b/server/tests/unit/nest/journey/journey-entries-from-photos.helpers.test.ts index dd2fec43a..dfba98564 100644 --- a/server/tests/unit/nest/journey/journey-entries-from-photos.helpers.test.ts +++ b/server/tests/unit/nest/journey/journey-entries-from-photos.helpers.test.ts @@ -4,6 +4,7 @@ * a group becomes and the title it gets without a place name. */ import { + captureInstant, carriedOffsetMinutes, centroid, clusterCaptures, @@ -48,6 +49,27 @@ describe('carriedOffsetMinutes', () => { expect(carriedOffsetMinutes('2026-05-14')).toBeNull(); expect(carriedOffsetMinutes('2026-05-14T09:10:00+15:00')).toBeNull(); expect(carriedOffsetMinutes('2026-05-14T09:10:00+02:75')).toBeNull(); + // SQL's spelling, a space between day and time. + expect(carriedOffsetMinutes('2026-05-14 09:10:00+02:00')).toBe(120); + }); +}); + +describe('captureInstant', () => { + it("JRNPHOTOS-HELP-020: a capture time without a zone is UTC, whatever the server's own clock", () => { + const utc = Date.UTC(2026, 4, 14, 23, 30); + expect(captureInstant('2026-05-14T23:30:00Z')).toBe(utc); + expect(captureInstant('2026-05-14T23:30:00')).toBe(utc); + expect(captureInstant('2026-05-14 23:30')).toBe(utc); + expect(captureInstant(' 2026-05-14T23:30:00.000 ')).toBe(utc); + expect(captureInstant('2026-05-15 01:30:00+02:00')).toBe(utc); + expect(captureInstant('not a date')).toBeNaN(); + }); + + it('JRNPHOTOS-HELP-021: a zoneless capture time lands on the same local day as its UTC spelling', () => { + const [zoneless] = localCaptures([photo('2026-05-14T22:30:00', TOKYO)], null); + const [utc] = localCaptures([photo('2026-05-14T22:30:00Z', TOKYO)], null); + expect(zoneless).toMatchObject({ date: '2026-05-15', time: '07:30', zone: 'Asia/Tokyo' }); + expect(zoneless?.instant).toBe(utc?.instant); }); });