Last active
January 16, 2016 16:21
-
-
Save groue/ab172d2ee3344a0bfed1 to your computer and use it in GitHub Desktop.
A type that reads and stores NSDate as timestamp in an SQLite database http://github.com/groue/GRDB.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
struct DatabaseTimestamp: DatabaseValueConvertible { | |
// NSDate conversion | |
// | |
// Value types should consistently use the Swift nil to represent the | |
// database NULL: the date property is a non-optional NSDate. | |
let date: NSDate | |
// As a convenience, the NSDate initializer accepts an optional NSDate, and | |
// is failable: the result is nil if and only if *date* is nil. | |
init?(_ date: NSDate?) { | |
guard let date = date else { | |
return nil | |
} | |
self.date = date | |
} | |
// DatabaseValueConvertible adoption | |
/// Returns a value that can be stored in the database. | |
var databaseValue: DatabaseValue { | |
// Double itself adopts DatabaseValueConvertible: | |
return date.timeIntervalSince1970.databaseValue | |
} | |
/// Returns a value initialized from *databaseValue*, if possible. | |
static func fromDatabaseValue(databaseValue: DatabaseValue) -> DatabaseTimestamp? { | |
// Double itself adopts DatabaseValueConvertible: | |
guard let timeInterval = Double.fromDatabaseValue(databaseValue) else { | |
// No Double, no NSDate! | |
return nil | |
} | |
return DatabaseTimestamp(NSDate(timeIntervalSince1970: timeInterval)) | |
} | |
} | |
var configuration = Configuration() | |
configuration.trace = { print($0) } | |
let dbQueue = DatabaseQueue(configuration: configuration) // Memory database | |
var migrator = DatabaseMigrator() | |
migrator.registerMigration("createEvents") { db in | |
try db.execute("CREATE TABLE events (" + | |
"date DATETIME " + | |
")") | |
} | |
try! migrator.migrate(dbQueue) | |
try! dbQueue.inDatabase { db in | |
try db.execute("INSERT INTO events (date) VALUES (?)", arguments: [DatabaseTimestamp(NSDate())]) | |
let row = Row.fetchOne(db, "SELECT * FROM events")! | |
let timestamp: Double = row.value(named: "date") | |
let date = (row.value(named: "date") as DatabaseTimestamp).date | |
print("timestamp: \(timestamp)") | |
print("date: \(date)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment