From 4f60cc5c98392943f8114a94f2fc3431664c68a4 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 21 Oct 2026 15:18:00 +0200 Subject: [PATCH] fix(integrations): cut a journey day in the zone of its own first track, never in the UTC Dawarich writes times in --- .../integrations/dawarich-tracks.service.ts | 23 ++++++++--- .../unit/nest/dawarich-tracks.service.test.ts | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/server/src/nest/integrations/dawarich-tracks.service.ts b/server/src/nest/integrations/dawarich-tracks.service.ts index dc67decb2..199ec1465 100644 --- a/server/src/nest/integrations/dawarich-tracks.service.ts +++ b/server/src/nest/integrations/dawarich-tracks.service.ts @@ -1,7 +1,7 @@ import { Trips } from '../../db/entities/Trips.entity'; import { TripsRepository } from '../../db/repositories/Trips.repository'; import { DomainError } from '../common/domain-error'; -import { DawarichClient, DawarichError, type DawarichCreds } from './dawarich.client'; +import { DawarichClient, DawarichError, type DawarichCreds, type DawarichTrackFeature } from './dawarich.client'; import { RECORDING_GAP, bucketPointsByDay, bucketTracksByDay, countPoints, offsetMinutesOf } from './dawarich.helpers'; import { DawarichService } from './dawarich.service'; import { InjectRepository } from '@mikro-orm/nestjs'; @@ -207,7 +207,7 @@ export class DawarichTracksService { if (capabilities?.tracks !== false) { try { const { features, truncated } = await this.client.listTracks(creds, from, to); - const sourceOffset = sourceOffsetOf(features[0]?.properties?.start_at); + const sourceOffset = sourceOffsetOn(features, date); const offset = zoneOf(zone, sourceOffset); const day = bucketTracksByDay(features, { maxPointsPerDay: DAWARICH_TRACK_POINTS_PER_DAY, @@ -255,10 +255,21 @@ export class DawarichTracksService { } } -/** The zone a Dawarich time names; null when it names none. */ -function sourceOffsetOf(value: unknown): number | null { - if (typeof value !== 'string' || !/(Z|[+-]\d{2}:?\d{2})$/.test(value)) return null; - return offsetMinutesOf(value); +/** + * The zone Dawarich recorded the date in, read off the first track that starts + * on it: the window opens on the day before, and a traveller who crossed zones + * overnight was elsewhere then. A `Z` time names no zone, Dawarich just wrote + * UTC, so it reveals nothing about where the day was. + */ +function sourceOffsetOn(features: DawarichTrackFeature[], date: string): number | null { + for (const feature of features) { + const startAt = feature.properties?.start_at; + if (typeof startAt !== 'string' || !/[+-]\d{2}:?\d{2}$/.test(startAt)) continue; + const offset = offsetMinutesOf(startAt); + const localDate = new Date(Date.parse(startAt) + offset * 60_000).toISOString().slice(0, 10); + if (localDate === date) return offset; + } + return null; } function zoneOf( diff --git a/server/tests/unit/nest/dawarich-tracks.service.test.ts b/server/tests/unit/nest/dawarich-tracks.service.test.ts index e6f111393..afc69530d 100644 --- a/server/tests/unit/nest/dawarich-tracks.service.test.ts +++ b/server/tests/unit/nest/dawarich-tracks.service.test.ts @@ -864,4 +864,45 @@ describe('DawarichTracksService.fetchDay', () => { expect(h.listPoints).toHaveBeenCalledTimes(2); }); + + it('DAWARICH-TRACKS-062: a track written in UTC names no zone, so asked for the source zone it still cuts in the one chosen', async () => { + const h = harness({ capabilities: CAPABILITIES }); + h.listTracks.mockResolvedValue({ + features: [feature({ start_at: '2026-05-01T08:00:00Z', end_at: '2026-05-01T10:00:00Z' })], + truncated: false, + }); + + const day = await h.service.fetchDay(USER, '2026-05-01', { offsetMinutes: 120, useSourceOffset: true }); + + expect(day).toMatchObject({ + sourceKind: 'tracks', + utcOffsetMinutes: 120, + offsetSource: 'chosen', + sourceOffsetMinutes: null, + window: { from: '2026-04-30T22:00:00.000Z', to: '2026-05-01T22:00:00.000Z' }, + }); + expect(day.segments).toHaveLength(1); + }); + + it('DAWARICH-TRACKS-063: the source zone is the one of the first track on the date, not of the evening before in another zone', async () => { + const h = harness({ capabilities: CAPABILITIES }); + h.listTracks.mockResolvedValue({ + features: [ + // The evening before in Berlin, then the morning after landing in Tokyo. + feature({ start_at: '2026-04-30T20:00:00+02:00', end_at: '2026-04-30T21:00:00+02:00' }), + feature({ start_at: '2026-05-01T09:00:00+09:00', end_at: '2026-05-01T10:00:00+09:00' }), + ], + truncated: false, + }); + + const day = await h.service.fetchDay(USER, '2026-05-01', { offsetMinutes: 0, useSourceOffset: true }); + + expect(day).toMatchObject({ + utcOffsetMinutes: 540, + offsetSource: 'source', + sourceOffsetMinutes: 540, + window: { from: '2026-04-30T15:00:00.000Z', to: '2026-05-01T15:00:00.000Z' }, + }); + expect(day.segments).toHaveLength(1); + }); });