From 7a1aa88edb1ad82408cdc281b9eff550b6bbabb1 Mon Sep 17 00:00:00 2001 From: Matt Greenfield Date: Mon, 16 Apr 2018 19:18:49 +0700 Subject: [PATCH] fractions of seconds are not the same thing as integer counts of nanoseconds --- GRDB/Core/Support/Foundation/SQLiteDateParser.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/GRDB/Core/Support/Foundation/SQLiteDateParser.swift b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift index eae297b3e..75b164fcd 100644 --- a/GRDB/Core/Support/Foundation/SQLiteDateParser.swift +++ b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift @@ -62,7 +62,7 @@ class SQLiteDateParser { guard parseCount >= 7 else { return DatabaseDateComponents(components, format: .YMD_HMS) } - components.nanosecond = Int(nanosecond.pointee) + components.nanosecond = nanosecondsInt(for: nanosecond.pointee) return DatabaseDateComponents(components, format: .YMD_HMSS) } @@ -90,8 +90,17 @@ class SQLiteDateParser { guard parseCount >= 4 else { return DatabaseDateComponents(components, format: .HMS) } - components.nanosecond = Int(nanosecond.pointee) + components.nanosecond = nanosecondsInt(for: nanosecond.pointee) return DatabaseDateComponents(components, format: .HMSS) } + + private static func nanosecondsInt(for secondFractional: Int32) -> Int { + guard secondFractional > 0 else { return 0 } + + let magnitude = Int32(log10(Double(secondFractional))) + let multiplier = Int32(pow(10.0, Double(8 - magnitude))) + + return Int(secondFractional * multiplier) + } }