From d1d8c2a1518e738b1ca29d4c0961a4a575ff86da Mon Sep 17 00:00:00 2001 From: TimFelixBeyer Date: Wed, 22 May 2024 12:16:13 +0200 Subject: [PATCH] Rudimentary handling of timezones --- FaceExplorer/Model/ModelData.swift | 32 +++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/FaceExplorer/Model/ModelData.swift b/FaceExplorer/Model/ModelData.swift index 9103a82..e4c8154 100755 --- a/FaceExplorer/Model/ModelData.swift +++ b/FaceExplorer/Model/ModelData.swift @@ -94,18 +94,18 @@ func getFaces(path: String) throws -> [Face] { // print("id: \(row[0]!)") // } // WARNING: For readability we add a trailing s to all database names in their swift object counterpart. - // let additionalAssetAttributes = Table("ZADDITIONALASSETATTRIBUTES") let assets = Table("ZASSET") let detectedFaces = Table("ZDETECTEDFACE") // Generic Queries let pk = Expression("Z_PK") let uuid = Expression("ZUUID") // detectedFace-ß queries - var asset: Expression + let asset = Expression("ZASSET") + var assetForFace: Expression if #available(macOS 14.0, *) { - asset = Expression("ZASSETFORFACE") + assetForFace = Expression("ZASSETFORFACE") } else { - asset = Expression("ZASSET") + assetForFace = Expression("ZASSET") } let centerX = Expression("ZCENTERX") let centerY = Expression("ZCENTERY") @@ -119,6 +119,10 @@ func getFaces(path: String) throws -> [Face] { let mergeTargetPerson = Expression("ZMERGETARGETPERSON") let fullName = Expression("ZFULLNAME") + // additionalAssetAttribute queries + let additionalAttributes = Table("ZADDITIONALASSETATTRIBUTES") + let timezoneOffset = Expression("ZINFERREDTIMEZONEOFFSET") + // Prefetch some data from the db for performance let assetsDict = Dictionary(uniqueKeysWithValues: try db.prepare(assets.select(pk, dateCreated, dateCreatedi, uuid)).map { @@ -130,15 +134,29 @@ func getFaces(path: String) throws -> [Face] { ($0[pk], $0) } ) + let timezoneOffsetDict = Dictionary(uniqueKeysWithValues: + try db.prepare(additionalAttributes.select(asset, timezoneOffset)).map { + ($0[asset], $0) + } + ) var count = 0 for face in try db.prepare(detectedFaces.filter(quality > -1)) { - let fullPic = assetsDict[face[asset]!]! + if face[Expression("ZASSETVISIBLE")] == 0 { + continue + } + let fullPic = assetsDict[face[assetForFace]!]! let name = getName(face: face, personsDict: personsDict) // Sometimes the capture date is in Int and sometimes in the Double format. // We need to be able to parse both. let interval = (fullPic[dateCreated] ?? Double(fullPic[dateCreatedi] ?? 0)) - let captureDate = Date(timeIntervalSince1970: 978310800 + interval) + // TODO: Look at timezones again, CEST has issues. + var timezoneOffsetValue = 0.0 + if let timezone = timezoneOffsetDict[face[assetForFace]!], + let offset = timezone[timezoneOffset] { + timezoneOffsetValue = Double(offset) + } + let captureDate = Date(timeIntervalSince1970: 978303600 + interval + timezoneOffsetValue) var attributeList: [String: (Int, String)] = [:] for attribute in faceAttributes { @@ -147,7 +165,7 @@ func getFaces(path: String) throws -> [Face] { } faces.append(Face(id: face[pk], uuid: face[uuid], - photoPk: face[asset], + photoPk: face[assetForFace], photoUUID: fullPic[uuid], centerX: face[centerX], centerY: face[centerY],