diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 93d7978d4..4ad284876 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -2,6 +2,8 @@ import Foundation /// DatabaseDateComponents reads and stores DateComponents in the database. public struct DatabaseDateComponents : DatabaseValueConvertible { + + public static var useScanfStrategy = true /// The available formats for reading and storing date components. public enum Format : String { @@ -130,7 +132,11 @@ public struct DatabaseDateComponents : DatabaseValueConvertible { guard let string = String.fromDatabaseValue(dbValue) else { return nil } - + + if useScanfStrategy { + return SQLiteDateParser.components(from: string) + } + var dateComponents = DateComponents() let scanner = Scanner(string: string) scanner.charactersToBeSkipped = CharacterSet() diff --git a/GRDB/Core/Support/Foundation/SQLiteDateParser.swift b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift new file mode 100644 index 000000000..4e7d2c895 --- /dev/null +++ b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift @@ -0,0 +1,90 @@ +import Foundation + +// inspired by: http://jordansmith.io/performant-date-parsing/ + +class SQLiteDateParser { + + private static var mutex: pthread_mutex_t = { + var mutex = pthread_mutex_t() + pthread_mutex_init(&mutex, nil) + return mutex + }() + + private static let year = UnsafeMutablePointer.allocate(capacity: 1) + private static let month = UnsafeMutablePointer.allocate(capacity: 1) + private static let day = UnsafeMutablePointer.allocate(capacity: 1) + private static let hour = UnsafeMutablePointer.allocate(capacity: 1) + private static let minute = UnsafeMutablePointer.allocate(capacity: 1) + private static let second = UnsafeMutablePointer.allocate(capacity: 1) + + public static func components(from dateString: String) -> DatabaseDateComponents? { + switch dateString.count { + case 23, 19, 16, 10: + return datetimeComponents(from: dateString) + case 12, 8, 5: + return timeComponents(from: dateString) + default: + return nil + } + } + + // - YYYY-MM-DD + // - YYYY-MM-DD HH:MM + // - YYYY-MM-DD HH:MM:SS + // - YYYY-MM-DD HH:MM:SS.SSS + // - YYYY-MM-DDTHH:MM + // - YYYY-MM-DDTHH:MM:SS + // - YYYY-MM-DDTHH:MM:SS.SSS + private static func datetimeComponents(from dateString: String) -> DatabaseDateComponents? { + pthread_mutex_lock(&mutex) + + let parseCount = withVaList([year, month, day, hour, minute, second]) { pointer in + vsscanf(dateString, "%d-%d-%d%*c%d:%d:%f", pointer) + } + + var components = DateComponents() + + components.year = Int(year.pointee) + components.month = Int(month.pointee) + components.day = Int(day.pointee) + + if parseCount >= 5 { + components.hour = Int(hour.pointee) + components.minute = Int(minute.pointee) + } + + if parseCount >= 6 { + components.second = Int(second.pointee) + components.nanosecond = Int((second.pointee - Float(components.second!)) * 1_000_000_000) + } + + pthread_mutex_unlock(&mutex) + + return DatabaseDateComponents(components, format: .YMD_HMSS) + } + + // - HH:MM + // - HH:MM:SS + // - HH:MM:SS.SSS + private static func timeComponents(from timeString: String) -> DatabaseDateComponents? { + pthread_mutex_lock(&mutex) + + let parseCount = withVaList([hour, minute, second]) { pointer in + vsscanf(timeString, "%d:%d:%f", pointer) + } + + var components = DateComponents() + + components.hour = Int(hour.pointee) + components.minute = Int(minute.pointee) + + if parseCount >= 3 { + components.second = Int(second.pointee) + components.nanosecond = Int((second.pointee - Float(components.second!)) * 1_000_000_000) + } + + pthread_mutex_unlock(&mutex) + + return DatabaseDateComponents(components, format: .HMSS) + } +}