diff --git a/GRDB/Foundation/DatabaseDateComponents.swift b/GRDB/Foundation/DatabaseDateComponents.swift index dbb7e0535..28daf7e7d 100644 --- a/GRDB/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Foundation/DatabaseDateComponents.swift @@ -32,6 +32,15 @@ public struct DatabaseDateComponents : DatabaseValueConvertible { /// The format "HH:mm:ss.SSS". case HMSS = "HH:mm:ss.SSS" + + var hasYMDComponents: Bool { + switch self { + case .YMD, .YMD_HM, .YMD_HMS, .YMD_HMSS: + return true + case .HM, .HMS, .HMSS: + return false + } + } } // MARK: - NSDateComponents conversion diff --git a/GRDB/Foundation/NSDate.swift b/GRDB/Foundation/NSDate.swift index 72907985f..1f3a652d5 100644 --- a/GRDB/Foundation/NSDate.swift +++ b/GRDB/Foundation/NSDate.swift @@ -11,60 +11,59 @@ extension NSDate : DatabaseValueConvertible { /// Returns an NSDate initialized from *databaseValue*, if possible. public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? { - if let julianDayNumber = Double.fromDatabaseValue(databaseValue) { - // Julian day number - // Conversion uses the same algorithm as SQLite: https://www.sqlite.org/src/artifact/8ec787fed4929d8c - let JD = Int64(julianDayNumber * 86400000) - let Z = Int(((JD + 43200000)/86400000)) - var A = Int(((Double(Z) - 1867216.25)/36524.25)) - A = Z + 1 + A - (A/4) - let B = A + 1524 - let C = Int(((Double(B) - 122.1)/365.25)) - let D = (36525*(C&32767))/100 - let E = Int((Double(B-D)/30.6001)) - let X1 = Int((30.6001*Double(E))) - let day = B - D - X1 - let month = E<14 ? E-1 : E-13 - let year = month>2 ? C - 4716 : C - 4715 - var s = Int(((JD + 43200000) % 86400000)) - var second = Double(s)/1000.0 - s = Int(second) - second -= Double(s) - let hour = s/3600 - s -= hour*3600 - let minute = s/60 - second += Double(s - minute*60) - - let dateComponents = NSDateComponents() - dateComponents.year = year - dateComponents.month = month - dateComponents.day = day - dateComponents.hour = hour - dateComponents.minute = minute - dateComponents.second = Int(second) - dateComponents.nanosecond = Int((second - Double(Int(second))) * 1.0e9) - - let date = UTCCalendar.dateFromComponents(dateComponents)! - return self.init(timeInterval: 0, sinceDate: date) - } - if let databaseDateComponents = DatabaseDateComponents.fromDatabaseValue(databaseValue) { - // Date components - - switch databaseDateComponents.format { - case .YMD, .YMD_HM, .YMD_HMS, .YMD_HMSS: - // Date is fully defined - let date = UTCCalendar.dateFromComponents(databaseDateComponents.dateComponents)! - return self.init(timeInterval: 0, sinceDate: date) - default: - // SQLite assumes 2000-01-01 when YMD are not provided, but this is - // dangerous. - return nil - } + return fromDatabaseDateComponents(databaseDateComponents) + } + if let julianDayNumber = Double.fromDatabaseValue(databaseValue) { + return fromJulienDayNumber(julianDayNumber) } - return nil } + + private static func fromJulienDayNumber(julianDayNumber: Double) -> Self? { + // Conversion uses the same algorithm as SQLite: https://www.sqlite.org/src/artifact/8ec787fed4929d8c + let JD = Int64(julianDayNumber * 86400000) + let Z = Int(((JD + 43200000)/86400000)) + var A = Int(((Double(Z) - 1867216.25)/36524.25)) + A = Z + 1 + A - (A/4) + let B = A + 1524 + let C = Int(((Double(B) - 122.1)/365.25)) + let D = (36525*(C&32767))/100 + let E = Int((Double(B-D)/30.6001)) + let X1 = Int((30.6001*Double(E))) + let day = B - D - X1 + let month = E<14 ? E-1 : E-13 + let year = month>2 ? C - 4716 : C - 4715 + var s = Int(((JD + 43200000) % 86400000)) + var second = Double(s)/1000.0 + s = Int(second) + second -= Double(s) + let hour = s/3600 + s -= hour*3600 + let minute = s/60 + second += Double(s - minute*60) + + let dateComponents = NSDateComponents() + dateComponents.year = year + dateComponents.month = month + dateComponents.day = day + dateComponents.hour = hour + dateComponents.minute = minute + dateComponents.second = Int(second) + dateComponents.nanosecond = Int((second - Double(Int(second))) * 1.0e9) + + let date = UTCCalendar.dateFromComponents(dateComponents)! + return self.init(timeInterval: 0, sinceDate: date) + } + + private static func fromDatabaseDateComponents(databaseDateComponents: DatabaseDateComponents) -> Self? { + guard databaseDateComponents.format.hasYMDComponents else { + // Refuse to turn hours without any date information into NSDate: + return nil + } + let date = UTCCalendar.dateFromComponents(databaseDateComponents.dateComponents)! + return self.init(timeInterval: 0, sinceDate: date) + } } /// The DatabaseDate date formatter for stored dates.