From a2c92087067c1d4f40ae64ab8abaa3f0180bd726 Mon Sep 17 00:00:00 2001 From: gras Date: Sun, 11 Feb 2024 21:41:42 +0100 Subject: [PATCH] Fixed serious error with offset calculation. Fixed bad test-data. --- .../model/fileaccess/MetadataLoader.ts | 24 +++++++++++++----- ...ey_opera_house_no_tsoffset_but_gps_utc.jpg | Bin 22653 -> 22641 bytes ...y_opera_house_no_tsoffset_but_gps_utc.json | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/backend/model/fileaccess/MetadataLoader.ts b/src/backend/model/fileaccess/MetadataLoader.ts index bb5e07b3..03e09543 100644 --- a/src/backend/model/fileaccess/MetadataLoader.ts +++ b/src/backend/model/fileaccess/MetadataLoader.ts @@ -197,23 +197,35 @@ export class MetadataLoader { //function to convert timestamp into milliseconds taking offset into account const timestampToMS = (timestamp: string, offset: string) => { - "replace first two : with - in timestamp string and add offset if exists (else +00:00 - UTC), parse this into MS" - return Date.parse(timestamp.replace(':', '-').replace(':', '-') + (offset ? offset : '+00:00')); + if (!timestamp) { + return undefined; + } + //replace : with - in the yyyy-mm-dd part of the timestamp. + let formattedTimestamp = timestamp.substring(0,9).replaceAll(':', '-') + timestamp.substring(9,timestamp.length); + if (formattedTimestamp.indexOf("Z") > 0) { //replace Z (and what comes after the Z) with offset + formattedTimestamp.substring(0, formattedTimestamp.indexOf("Z")) + (offset ? offset : '+00:00'); + } else if (formattedTimestamp.indexOf("+") > 0) { //don't do anything + } else { //add offset + formattedTimestamp = formattedTimestamp + (offset ? offset : '+00:00'); + } + //parse into MS and return + return Date.parse(formattedTimestamp); } //function to calculate offset from exif.exif.gpsTimeStamp or exif.gps.GPSDateStamp + exif.gps.GPSTimestamp const getTimeOffsetByGPSStamp = (timestamp: string, gpsTimeStamp: string, gps: any) => { - let UTCTimestamp = gpsTimeStamp; //use the exif.exif.gpsTimestamp if available + let UTCTimestamp = gpsTimeStamp; if (!UTCTimestamp && gps && gps.GPSDateStamp && gps.GPSTimeStamp) { //else use exif.gps.GPS*Stamp if available //GPS timestamp is always UTC (+00:00) - UTCTimestamp = gps.GPSDateStamp.replaceAll(':', '-') + gps.GPSTimeStamp.join(':') + '+00:00'; + UTCTimestamp = gps.GPSDateStamp.replaceAll(':', '-') + gps.GPSTimeStamp.join(':'); } - if (UTCTimestamp) { + if (UTCTimestamp && timestamp) { //offset in minutes is the difference between gps timestamp and given timestamp - const offsetMinutes = (Date.parse(UTCTimestamp) - Date.parse(timestamp.replace(':', '-').replace(':', '-'))) / 1000 / 60; + //to calculate this correctly, we have to work with the same offset + const offsetMinutes = (timestampToMS(timestamp, '+00:00')- timestampToMS(UTCTimestamp, '+00:00')) / 1000 / 60; if (-720 <= offsetMinutes && offsetMinutes <= 840) { //valid offset is within -12 and +14 hrs (https://en.wikipedia.org/wiki/List_of_UTC_offsets) return (offsetMinutes < 0 ? "-" : "+") + //leading +/- diff --git a/test/backend/assets/timestamps/sydney_opera_house_no_tsoffset_but_gps_utc.jpg b/test/backend/assets/timestamps/sydney_opera_house_no_tsoffset_but_gps_utc.jpg index f1c9a652d337a90e9c251477c8cd2121fa807884..184331398738dbff246dd6f1d9e316c383ba9acc 100644 GIT binary patch delta 44 zcmV+{0Mq~dumSO~0kE3_0Va|Q9J9Cr`vjA12M@Ez2Oa?hF*#H*Fth6j$pW*w3&Tu? C0ua3b delta 55 zcmeykf${GK#tpNX7>y