mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
DatabaseDateComponents WIP
This commit is contained in:
1 parent
6c4813990d
commit
252f89fe22
4 files changed
+693
-253
No files matched your search
@@ -0,0 +1,424 @@
|
||||
//
|
||||
// GRDB.swift
|
||||
// https://github.com/groue/GRDB.swift
|
||||
// Copyright (c) 2015 Gwendal Roué
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class DatabaseDateComponentsTests : GRDBTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createDates") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE dates (" +
|
||||
"ID INTEGER PRIMARY KEY, " +
|
||||
"creationDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" +
|
||||
")")
|
||||
}
|
||||
assertNoError {
|
||||
try migrator.migrate(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatHM() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .HM)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "10:11")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.HM)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatHMS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .HMS)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "10:11:12")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.HMS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, dateComponents.second)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatHMSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .HMSS)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "10:11:12.123")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.HMSS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, dateComponents.second)
|
||||
XCTAssertEqual(round(Double(databaseDateComponents.dateComponents.nanosecond) / 1.0e6), round(Double(dateComponents.nanosecond) / 1.0e6))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatYMD() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .YMD)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "1973-09-18")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, dateComponents.year)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, dateComponents.month)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, dateComponents.day)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatYMD_HM() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .YMD_HM)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "1973-09-18 10:11")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HM)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, dateComponents.year)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, dateComponents.month)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, dateComponents.day)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatYMD_HMS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .YMD_HMS)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "1973-09-18 10:11:12")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, dateComponents.year)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, dateComponents.month)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, dateComponents.day)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, dateComponents.second)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFormatYMD_HMSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 9
|
||||
dateComponents.day = 18
|
||||
dateComponents.hour = 10
|
||||
dateComponents.minute = 11
|
||||
dateComponents.second = 12
|
||||
dateComponents.nanosecond = 123_456_789
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDateComponents(dateComponents, format: .YMD_HMSS)])
|
||||
|
||||
let string = db.fetchOne(String.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(string, "1973-09-18 10:11:12.123")
|
||||
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate FROM dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMSS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, dateComponents.year)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, dateComponents.month)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, dateComponents.day)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, dateComponents.hour)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, dateComponents.minute)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, dateComponents.second)
|
||||
XCTAssertEqual(round(Double(databaseDateComponents.dateComponents.nanosecond) / 1.0e6), round(Double(dateComponents.nanosecond) / 1.0e6))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsIsLexicallyComparableToCURRENT_TIMESTAMP() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
do {
|
||||
let date = NSDate().dateByAddingTimeInterval(-1)
|
||||
let dateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date)
|
||||
try db.execute(
|
||||
"INSERT INTO dates (id, creationDate) VALUES (?,?)",
|
||||
arguments: [1, DatabaseDateComponents(dateComponents, format: .YMD_HMS)])
|
||||
}
|
||||
do {
|
||||
try db.execute(
|
||||
"INSERT INTO dates (id) VALUES (?)",
|
||||
arguments: [2])
|
||||
}
|
||||
do {
|
||||
let date = NSDate().dateByAddingTimeInterval(1)
|
||||
let dateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date)
|
||||
try db.execute(
|
||||
"INSERT INTO dates (id, creationDate) VALUES (?,?)",
|
||||
arguments: [3, DatabaseDateComponents(dateComponents, format: .YMD_HMS)])
|
||||
}
|
||||
|
||||
let ids = db.fetchAll(Int.self, "SELECT id FROM dates ORDER BY creationDate").map { $0! }
|
||||
XCTAssertEqual(ids, [1,2,3])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsFromUnparsableString() {
|
||||
let databaseDateComponents = DatabaseDateComponents(databaseValue: .Text("foo"))
|
||||
XCTAssertTrue(databaseDateComponents == nil)
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDD() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDHHMM() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HM)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDHHMMSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02:03"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, 3)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDHHMMSSSSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02:03.00456"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMSS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, 3)
|
||||
XCTAssertTrue(abs(databaseDateComponents.dateComponents.nanosecond - 4_000_000) < 10) // We actually get 4_000_008. Some precision is lost during the NSDateComponents -> NSDate conversion. Not a big deal.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDTHHMM() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HM)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, NSDateComponentUndefined)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDTHHMMSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02:03"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, 3)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.nanosecond, NSDateComponentUndefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseDateComponentsAcceptsFormatYYYYMMDDTHHMMSSSSS() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02:03.00456"])
|
||||
let databaseDateComponents = db.fetchOne(DatabaseDateComponents.self, "SELECT creationDate from dates")!
|
||||
XCTAssertEqual(databaseDateComponents.format, DatabaseDateComponents.Format.YMD_HMSS)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.year, 2015)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.month, 7)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.day, 22)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.hour, 1)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.minute, 2)
|
||||
XCTAssertEqual(databaseDateComponents.dateComponents.second, 3)
|
||||
XCTAssertTrue(abs(databaseDateComponents.dateComponents.nanosecond - 4_000_000) < 10) // We actually get 4_000_008. Some precision is lost during the NSDateComponents -> NSDate conversion. Not a big deal.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
-41
@@ -31,9 +31,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
super.setUp()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createStuffs") { db in
|
||||
migrator.registerMigration("createDates") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE stuffs (" +
|
||||
"CREATE TABLE dates (" +
|
||||
"ID INTEGER PRIMARY KEY, " +
|
||||
"creationDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" +
|
||||
")")
|
||||
@@ -45,7 +45,7 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
|
||||
func testDatabaseDate() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
let dateComponents = NSDateComponents()
|
||||
@@ -59,12 +59,11 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
|
||||
do {
|
||||
let date = calendar.dateFromComponents(dateComponents)!
|
||||
try db.execute("INSERT INTO stuffs (creationDate) VALUES (?)", arguments: [DatabaseDate(date)])
|
||||
try db.execute("INSERT INTO dates (creationDate) VALUES (?)", arguments: [DatabaseDate(date)])
|
||||
}
|
||||
|
||||
do {
|
||||
let row = db.fetchOneRow("SELECT creationDate FROM stuffs")!
|
||||
let date = (row.value(atIndex: 0)! as DatabaseDate).date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate FROM dates")!.date
|
||||
// All components must be preserved, but nanosecond since ISO-8601 stores milliseconds.
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), dateComponents.year)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Month, fromDate: date), dateComponents.month)
|
||||
@@ -72,22 +71,8 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Hour, fromDate: date), dateComponents.hour)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Minute, fromDate: date), dateComponents.minute)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Second, fromDate: date), dateComponents.second)
|
||||
XCTAssertEqual(round(Double(calendar.component(NSCalendarUnit.Nanosecond, fromDate: date)) / 1e6), round(Double(dateComponents.nanosecond) / 1e6))
|
||||
XCTAssertEqual(round(Double(calendar.component(NSCalendarUnit.Nanosecond, fromDate: date)) / 1.0e6), round(Double(dateComponents.nanosecond) / 1.0e6))
|
||||
}
|
||||
|
||||
do {
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate FROM stuffs")!.date
|
||||
// All components must be preserved, but nanosecond since ISO-8601 stores milliseconds.
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), dateComponents.year)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Month, fromDate: date), dateComponents.month)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Day, fromDate: date), dateComponents.day)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Hour, fromDate: date), dateComponents.hour)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Minute, fromDate: date), dateComponents.minute)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Second, fromDate: date), dateComponents.second)
|
||||
XCTAssertEqual(round(Double(calendar.component(NSCalendarUnit.Nanosecond, fromDate: date)) / 1e6), round(Double(dateComponents.nanosecond) / 1e6))
|
||||
}
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,18 +81,18 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (id, creationDate) VALUES (?,?)",
|
||||
"INSERT INTO dates (id, creationDate) VALUES (?,?)",
|
||||
arguments: [1, DatabaseDate(NSDate().dateByAddingTimeInterval(-1))])
|
||||
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (id) VALUES (?)",
|
||||
"INSERT INTO dates (id) VALUES (?)",
|
||||
arguments: [2])
|
||||
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (id, creationDate) VALUES (?,?)",
|
||||
"INSERT INTO dates (id, creationDate) VALUES (?,?)",
|
||||
arguments: [3, DatabaseDate(NSDate().dateByAddingTimeInterval(1))])
|
||||
|
||||
let ids = db.fetchAll(Int.self, "SELECT id FROM stuffs ORDER BY creationDate").map { $0! }
|
||||
let ids = db.fetchAll(Int.self, "SELECT id FROM dates ORDER BY creationDate").map { $0! }
|
||||
XCTAssertEqual(ids, [1,2,3])
|
||||
}
|
||||
}
|
||||
@@ -122,9 +107,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -142,9 +127,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -162,9 +147,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02:03"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -182,9 +167,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22 01:02:03.00456"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -202,9 +187,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -222,9 +207,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02:03"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -242,9 +227,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(
|
||||
"INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
"INSERT INTO dates (creationDate) VALUES (?)",
|
||||
arguments: ["2015-07-22T01:02:03.00456"])
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2015)
|
||||
@@ -263,9 +248,9 @@ class DatabaseDateTests : GRDBTestCase {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// // 00:30:00.0 UT January 1, 2013 according to https://en.wikipedia.org/wiki/Julian_day
|
||||
// try db.execute(
|
||||
// "INSERT INTO stuffs (creationDate) VALUES (?)",
|
||||
// "INSERT INTO dates (creationDate) VALUES (?)",
|
||||
// arguments: [2_456_293.520833])
|
||||
// let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from stuffs")!.date
|
||||
// let date = db.fetchOne(DatabaseDate.self, "SELECT creationDate from dates")!.date
|
||||
// let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
// calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
// XCTAssertEqual(calendar.component(NSCalendarUnit.Year, fromDate: date), 2013)
|
||||
@@ -23,8 +23,6 @@
|
||||
56997A3A1B539038005EBEED /* DatabaseErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796A1B539038005EBEED /* DatabaseErrorTests.swift */; };
|
||||
56997A3B1B539038005EBEED /* DatabaseMigratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796C1B539038005EBEED /* DatabaseMigratorTests.swift */; };
|
||||
56997A3C1B539038005EBEED /* DatabaseMigratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796C1B539038005EBEED /* DatabaseMigratorTests.swift */; };
|
||||
56997A3D1B539038005EBEED /* DateTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796E1B539038005EBEED /* DateTimeTests.swift */; };
|
||||
56997A3E1B539038005EBEED /* DateTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796E1B539038005EBEED /* DateTimeTests.swift */; };
|
||||
56997A3F1B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796F1B539038005EBEED /* DatabaseValueConvertibleTests.swift */; };
|
||||
56997A401B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699796F1B539038005EBEED /* DatabaseValueConvertibleTests.swift */; };
|
||||
56997A411B539038005EBEED /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569979701B539038005EBEED /* RawRepresentableTests.swift */; };
|
||||
@@ -94,6 +92,10 @@
|
||||
56E5D8371B4D43FA00430942 /* DatabaseValueConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE5E1B42E59F00C35172 /* DatabaseValueConvertible.swift */; };
|
||||
56E5D8381B4D43FA00430942 /* Statement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE561B42C34A00C35172 /* Statement.swift */; };
|
||||
56E5D8391B4D43FA00430942 /* UpdateStatement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE5C1B42D49C00C35172 /* UpdateStatement.swift */; };
|
||||
56F0B98F1B6001C600A2F135 /* DatabaseDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */; };
|
||||
56F0B9901B6001C600A2F135 /* DatabaseDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */; };
|
||||
56F0B9911B6001C600A2F135 /* DatabaseDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */; };
|
||||
56F0B9921B6001C600A2F135 /* DatabaseDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
DC3773F919C8CBB3004FCF85 /* GRDB.h in Headers */ = {isa = PBXBuildFile; fileRef = DC3773F819C8CBB3004FCF85 /* GRDB.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
DC70AC991AC2331000371524 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DC37744219C8DC91004FCF85 /* libsqlite3.dylib */; };
|
||||
@@ -128,7 +130,6 @@
|
||||
569979681B539038005EBEED /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = "<group>"; };
|
||||
5699796A1B539038005EBEED /* DatabaseErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorTests.swift; sourceTree = "<group>"; };
|
||||
5699796C1B539038005EBEED /* DatabaseMigratorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseMigratorTests.swift; sourceTree = "<group>"; };
|
||||
5699796E1B539038005EBEED /* DateTimeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateTimeTests.swift; sourceTree = "<group>"; };
|
||||
5699796F1B539038005EBEED /* DatabaseValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleTests.swift; sourceTree = "<group>"; };
|
||||
569979701B539038005EBEED /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = "<group>"; };
|
||||
569979731B539038005EBEED /* FetchedRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedRowTests.swift; sourceTree = "<group>"; };
|
||||
@@ -165,6 +166,8 @@
|
||||
56E5D7CA1B4D3FED00430942 /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
56E5D7D31B4D3FEE00430942 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
56E5D7F91B4D422D00430942 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateTests.swift; sourceTree = "<group>"; };
|
||||
DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDB-Bridging.h"; sourceTree = "<group>"; };
|
||||
DC3773F319C8CBB3004FCF85 /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
DC3773F719C8CBB3004FCF85 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = ../GRDB/Info.plist; sourceTree = "<group>"; };
|
||||
@@ -223,6 +226,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
569979661B539038005EBEED /* Database */,
|
||||
56F0B98C1B6001C600A2F135 /* DatabaseDate */,
|
||||
569979691B539038005EBEED /* DatabaseError */,
|
||||
5699796B1B539038005EBEED /* DatabaseMigrator */,
|
||||
5699796D1B539038005EBEED /* DatabaseValueConvertible */,
|
||||
@@ -262,7 +266,6 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5699796F1B539038005EBEED /* DatabaseValueConvertibleTests.swift */,
|
||||
5699796E1B539038005EBEED /* DateTimeTests.swift */,
|
||||
569979701B539038005EBEED /* RawRepresentableTests.swift */,
|
||||
);
|
||||
path = DatabaseValueConvertible;
|
||||
@@ -338,6 +341,15 @@
|
||||
path = GRDBOSX;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
56F0B98C1B6001C600A2F135 /* DatabaseDate */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56F0B98D1B6001C600A2F135 /* DatabaseDateComponentsTests.swift */,
|
||||
56F0B98E1B6001C600A2F135 /* DatabaseDateTests.swift */,
|
||||
);
|
||||
path = DatabaseDate;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
DC10500F19C904DD00D8CA30 /* GRDB Tests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -627,8 +639,10 @@
|
||||
56997A5A1B539038005EBEED /* RowModelInitializersTests.swift in Sources */,
|
||||
56997A401B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */,
|
||||
56997A4C1B539038005EBEED /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56F0B9921B6001C600A2F135 /* DatabaseDateTests.swift in Sources */,
|
||||
56997A481B539038005EBEED /* MinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
56997A5C1B539038005EBEED /* RowModelEditedTests.swift in Sources */,
|
||||
56F0B9901B6001C600A2F135 /* DatabaseDateComponentsTests.swift in Sources */,
|
||||
56997A4A1B539038005EBEED /* MinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56997A931B539162005EBEED /* DictionaryRowTests.swift in Sources */,
|
||||
56997A3A1B539038005EBEED /* DatabaseErrorTests.swift in Sources */,
|
||||
@@ -639,7 +653,6 @@
|
||||
56997A461B539038005EBEED /* FetchedRowTests.swift in Sources */,
|
||||
56E5D8181B4D435D00430942 /* GRDBTestCase.swift in Sources */,
|
||||
56997A641B539038005EBEED /* UpdateStatementTests.swift in Sources */,
|
||||
56997A3E1B539038005EBEED /* DateTimeTests.swift in Sources */,
|
||||
56997A501B539038005EBEED /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56997A3C1B539038005EBEED /* DatabaseMigratorTests.swift in Sources */,
|
||||
56997A621B539038005EBEED /* SelectStatementTests.swift in Sources */,
|
||||
@@ -661,8 +674,10 @@
|
||||
56997A591B539038005EBEED /* RowModelInitializersTests.swift in Sources */,
|
||||
56997A3F1B539038005EBEED /* DatabaseValueConvertibleTests.swift in Sources */,
|
||||
56997A4B1B539038005EBEED /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56F0B9911B6001C600A2F135 /* DatabaseDateTests.swift in Sources */,
|
||||
56997A471B539038005EBEED /* MinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
56997A5B1B539038005EBEED /* RowModelEditedTests.swift in Sources */,
|
||||
56F0B98F1B6001C600A2F135 /* DatabaseDateComponentsTests.swift in Sources */,
|
||||
56997A491B539038005EBEED /* MinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56997A921B539162005EBEED /* DictionaryRowTests.swift in Sources */,
|
||||
56997A391B539038005EBEED /* DatabaseErrorTests.swift in Sources */,
|
||||
@@ -673,7 +688,6 @@
|
||||
56997A451B539038005EBEED /* FetchedRowTests.swift in Sources */,
|
||||
56E5D8041B4D424400430942 /* GRDBTestCase.swift in Sources */,
|
||||
56997A631B539038005EBEED /* UpdateStatementTests.swift in Sources */,
|
||||
56997A3D1B539038005EBEED /* DateTimeTests.swift in Sources */,
|
||||
56997A4F1B539038005EBEED /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56997A3B1B539038005EBEED /* DatabaseMigratorTests.swift in Sources */,
|
||||
56997A611B539038005EBEED /* SelectStatementTests.swift in Sources */,
|
||||
|
||||
+223
-206
@@ -100,25 +100,32 @@ public struct DatabaseDate : DatabaseValueConvertible {
|
||||
return .Text(DatabaseDate.storageDateFormatter.stringFromDate(date))
|
||||
}
|
||||
|
||||
/// Create an instance initialized to `databaseValue`.
|
||||
/**
|
||||
Create an instance initialized to `databaseValue`.
|
||||
|
||||
Supported formats are:
|
||||
|
||||
- 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
|
||||
*/
|
||||
public init?(databaseValue: DatabaseValue) {
|
||||
// We need a string:
|
||||
guard let string = String(databaseValue: databaseValue) else {
|
||||
// We need date components;
|
||||
guard let databaseDateComponents = DatabaseDateComponents(databaseValue: databaseValue) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// We need date components:
|
||||
guard let dateComponents = dateComponentsFromSQLiteString(string) else {
|
||||
// We need date components with year, month, and day:
|
||||
switch databaseDateComponents.format {
|
||||
case .YMD, .YMD_HM, .YMD_HMS, .YMD_HMSS:
|
||||
self.init(DatabaseDate.UTCCalendar.dateFromComponents(databaseDateComponents.dateComponents))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
// We need at least year, month & day (we may get only hour & minutes):
|
||||
guard dateComponents.year != NSDateComponentUndefined && dateComponents.month != NSDateComponentUndefined && dateComponents.day != NSDateComponentUndefined else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OK gimme the date
|
||||
self.init(DatabaseDate.UTCCalendar.dateFromComponents(dateComponents))
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +141,7 @@ public struct DatabaseDate : DatabaseValueConvertible {
|
||||
/// The DatabaseDate date formatter for stored dates.
|
||||
static let storageDateFormatter: NSDateFormatter = {
|
||||
let formatter = NSDateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||
formatter.dateFormat = DatabaseDateComponents.Format.YMD_HMSS.rawValue
|
||||
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
|
||||
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
return formatter
|
||||
@@ -144,24 +151,36 @@ public struct DatabaseDate : DatabaseValueConvertible {
|
||||
public struct DatabaseDateComponents : DatabaseValueConvertible {
|
||||
|
||||
// MARK: - NSDateComponents conversion
|
||||
//
|
||||
// We consistently use the Swift nil to represent the database NULL: the
|
||||
// date property is a non-optional NSDateComponents, and the
|
||||
// NSDateComponents initializer is failable:
|
||||
|
||||
/// The represented date
|
||||
public enum Format : String {
|
||||
case YMD = "yyyy-MM-dd"
|
||||
case YMD_HM = "yyyy-MM-dd HH:mm"
|
||||
case YMD_HMS = "yyyy-MM-dd HH:mm:ss"
|
||||
case YMD_HMSS = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||
case HM = "HH:mm"
|
||||
case HMS = "HH:mm:ss"
|
||||
case HMSS = "HH:mm:ss.SSS"
|
||||
}
|
||||
|
||||
/// The date components
|
||||
public let dateComponents: NSDateComponents
|
||||
|
||||
/// The database format
|
||||
public let format: Format
|
||||
|
||||
/**
|
||||
Creates a DatabaseDate from an NSDateComponents.
|
||||
Creates a DatabaseDateComponents from an NSDateComponents and a format.
|
||||
|
||||
The result is nil if and only if *dateComponents* is nil.
|
||||
|
||||
- parameter dateComponents: An optional NSDateComponents.
|
||||
- returns: An optional DatabaseDate.
|
||||
- parameter format: A format that specifies which date components were
|
||||
loaded, or will be stored in the database.
|
||||
- returns: An optional DatabaseDateComponents.
|
||||
*/
|
||||
public init?(_ dateComponents: NSDateComponents?) {
|
||||
public init?(_ dateComponents: NSDateComponents?, format: Format) {
|
||||
if let dateComponents = dateComponents {
|
||||
self.format = format
|
||||
self.dateComponents = dateComponents
|
||||
} else {
|
||||
return nil
|
||||
@@ -173,220 +192,218 @@ public struct DatabaseDateComponents : DatabaseValueConvertible {
|
||||
|
||||
/// Returns a value that can be stored in the database.
|
||||
public var databaseValue: DatabaseValue {
|
||||
return .Text(SQLiteStringFromDateComponents(dateComponents))
|
||||
let dateString: String?
|
||||
switch format {
|
||||
case .YMD, .YMD_HM, .YMD_HMS, .YMD_HMSS:
|
||||
let year = (dateComponents.year == NSDateComponentUndefined) ? 0 : dateComponents.year
|
||||
let month = (dateComponents.month == NSDateComponentUndefined) ? 1 : dateComponents.month
|
||||
let day = (dateComponents.day == NSDateComponentUndefined) ? 1 : dateComponents.day
|
||||
dateString = NSString(format: "%04d-%02d-%02d", year, month, day) as String
|
||||
default:
|
||||
dateString = nil
|
||||
}
|
||||
|
||||
let timeString: String?
|
||||
switch format {
|
||||
case .YMD_HM, .HM:
|
||||
let hour = (dateComponents.hour == NSDateComponentUndefined) ? 0 : dateComponents.hour
|
||||
let minute = (dateComponents.minute == NSDateComponentUndefined) ? 0 : dateComponents.minute
|
||||
timeString = NSString(format: "%02d:%02d", hour, minute) as String
|
||||
case .YMD_HMS, .HMS:
|
||||
let hour = (dateComponents.hour == NSDateComponentUndefined) ? 0 : dateComponents.hour
|
||||
let minute = (dateComponents.minute == NSDateComponentUndefined) ? 0 : dateComponents.minute
|
||||
let second = (dateComponents.second == NSDateComponentUndefined) ? 0 : dateComponents.second
|
||||
timeString = NSString(format: "%02d:%02d:%02d", hour, minute, second) as String
|
||||
case .YMD_HMSS, .HMSS:
|
||||
let hour = (dateComponents.hour == NSDateComponentUndefined) ? 0 : dateComponents.hour
|
||||
let minute = (dateComponents.minute == NSDateComponentUndefined) ? 0 : dateComponents.minute
|
||||
let second = (dateComponents.second == NSDateComponentUndefined) ? 0 : dateComponents.second
|
||||
let nanosecond = (dateComponents.nanosecond == NSDateComponentUndefined) ? 0 : dateComponents.nanosecond
|
||||
timeString = NSString(format: "%02d:%02d:%02d.%03d", hour, minute, second, Int(round(Double(nanosecond) / 1_000_000.0))) as String
|
||||
default:
|
||||
timeString = nil
|
||||
}
|
||||
|
||||
return .Text(" ".join([dateString, timeString].filter { $0 != nil }.map { $0! }))
|
||||
}
|
||||
|
||||
/// Create an instance initialized to `databaseValue`.
|
||||
public init?(databaseValue: DatabaseValue) {
|
||||
// We need a string:
|
||||
// https://www.sqlite.org/lang_datefunc.html
|
||||
//
|
||||
// Supported formats are:
|
||||
//
|
||||
// - 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
|
||||
// - HH:MM
|
||||
// - HH:MM:SS
|
||||
// - HH:MM:SS.SSS
|
||||
|
||||
// We need a String
|
||||
guard let string = String(databaseValue: databaseValue) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.init(dateComponentsFromSQLiteString(string))
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Not Public
|
||||
|
||||
static let UTCCalendar: NSCalendar = {
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
calendar.locale = NSLocale(localeIdentifier: "en_US_POSIX")
|
||||
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
return calendar
|
||||
}()
|
||||
|
||||
/// The DatabaseDate date formatter for stored dates.
|
||||
static let storageDateFormatter: NSDateFormatter = {
|
||||
let formatter = NSDateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
|
||||
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
|
||||
return formatter
|
||||
}()
|
||||
}
|
||||
|
||||
private func SQLiteStringFromDateComponents(dateComponents: NSDateComponents) -> String {
|
||||
let yearDefined = dateComponents.year == NSDateComponentUndefined
|
||||
let monthDefined = dateComponents.month == NSDateComponentUndefined
|
||||
let dayDefined = dateComponents.day == NSDateComponentUndefined
|
||||
|
||||
var dateString: String?
|
||||
switch (yearDefined, monthDefined, dayDefined) {
|
||||
case (false, false, false):
|
||||
dateString = nil
|
||||
case (true, true, true):
|
||||
dateString = NSString(format: "%04d-%02d-%02d", dateComponents.year, dateComponents.month, dateComponents.day) as String
|
||||
default:
|
||||
fatalError("Can't convert NSDateComponents into SQLite string, because it does not define all year, month and day components.")
|
||||
}
|
||||
|
||||
let hourDefined = dateComponents.hour == NSDateComponentUndefined
|
||||
let minuteDefined = dateComponents.minute == NSDateComponentUndefined
|
||||
let secondDefined = dateComponents.second == NSDateComponentUndefined
|
||||
let nanosecondDefined = dateComponents.nanosecond == NSDateComponentUndefined
|
||||
|
||||
var timeString: String?
|
||||
switch (hourDefined, minuteDefined, secondDefined, nanosecondDefined) {
|
||||
case (false, false, false, false):
|
||||
timeString = nil
|
||||
case (true, true, false, false):
|
||||
timeString = NSString(format: "%02d:%02d", dateComponents.hour, dateComponents.minute) as String
|
||||
case (true, true, true, false):
|
||||
timeString = NSString(format: "%02d:%02d:%02d", dateComponents.hour, dateComponents.minute, dateComponents.second) as String
|
||||
case (true, true, true, true):
|
||||
timeString = NSString(format: "%02d:%02d:%02d.%03d", dateComponents.hour, dateComponents.minute, dateComponents.second, Int(round(Double(dateComponents.nanosecond) / 1_000_0000.0))) as String
|
||||
default:
|
||||
fatalError("Can't convert NSDateComponents into SQLite string, because it does not define all year, month and day components.")
|
||||
}
|
||||
|
||||
return " ".join([dateString, timeString].filter { $0 != nil }.map { $0! })
|
||||
}
|
||||
|
||||
private func dateComponentsFromSQLiteString(string: String) -> NSDateComponents? {
|
||||
// https://www.sqlite.org/lang_datefunc.html
|
||||
//
|
||||
// Supported formats are:
|
||||
//
|
||||
// - 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
|
||||
// - HH:MM
|
||||
// - HH:MM:SS
|
||||
// - HH:MM:SS.SSS
|
||||
|
||||
|
||||
let dateComponents = NSDateComponents()
|
||||
let scanner = NSScanner(string: string)
|
||||
scanner.charactersToBeSkipped = NSCharacterSet()
|
||||
|
||||
// YYYY or HH
|
||||
var initialNumber: Int = 0
|
||||
if !scanner.scanInteger(&initialNumber) {
|
||||
return nil
|
||||
}
|
||||
switch scanner.scanLocation {
|
||||
case 2:
|
||||
// HH
|
||||
let hour = initialNumber
|
||||
if hour >= 0 && hour <= 23 {
|
||||
dateComponents.hour = hour
|
||||
} else {
|
||||
let dateComponents = NSDateComponents()
|
||||
let scanner = NSScanner(string: string)
|
||||
scanner.charactersToBeSkipped = NSCharacterSet()
|
||||
|
||||
// YYYY or HH
|
||||
let hasDate: Bool
|
||||
var initialNumber: Int = 0
|
||||
if !scanner.scanInteger(&initialNumber) {
|
||||
return nil
|
||||
}
|
||||
switch scanner.scanLocation {
|
||||
case 2:
|
||||
// HH
|
||||
hasDate = false
|
||||
|
||||
let hour = initialNumber
|
||||
if hour >= 0 && hour <= 23 {
|
||||
dateComponents.hour = hour
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
case 4:
|
||||
// YYYY
|
||||
hasDate = true
|
||||
|
||||
let year = initialNumber
|
||||
if year >= 0 && year <= 9999 {
|
||||
dateComponents.year = year
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// -
|
||||
if !scanner.scanString("-", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MM
|
||||
var month: Int = 0
|
||||
if scanner.scanInteger(&month) && month >= 1 && month <= 12 {
|
||||
dateComponents.month = month
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// -
|
||||
if !scanner.scanString("-", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DD
|
||||
var day: Int = 0
|
||||
if scanner.scanInteger(&day) && day >= 1 && day <= 31 {
|
||||
dateComponents.day = day
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// YYYY-MM-DD
|
||||
if scanner.atEnd {
|
||||
self.init(dateComponents, format: .YMD)
|
||||
return
|
||||
}
|
||||
|
||||
// T/space
|
||||
if !(scanner.scanString("T", intoString: nil) || scanner.scanString(" ", intoString: nil)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HH
|
||||
var hour: Int = 0
|
||||
if scanner.scanInteger(&hour) && hour >= 0 && hour <= 23 {
|
||||
dateComponents.hour = hour
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
case 4:
|
||||
// YYYY
|
||||
let year = initialNumber
|
||||
if year >= 0 && year <= 9999 {
|
||||
dateComponents.year = year
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// -
|
||||
if !scanner.scanString("-", intoString: nil) {
|
||||
// :
|
||||
if !scanner.scanString(":", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MM
|
||||
var month: Int = 0
|
||||
if scanner.scanInteger(&month) && month >= 1 && month <= 12 {
|
||||
dateComponents.month = month
|
||||
var minute: Int = 0
|
||||
if scanner.scanInteger(&minute) && minute >= 0 && minute <= 59 {
|
||||
dateComponents.minute = minute
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// -
|
||||
if !scanner.scanString("-", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DD
|
||||
var day: Int = 0
|
||||
if scanner.scanInteger(&day) && day >= 1 && day <= 31 {
|
||||
dateComponents.day = day
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// YYYY-MM-DD
|
||||
// [YYYY-MM-DD] HH:MM
|
||||
if scanner.atEnd {
|
||||
return dateComponents
|
||||
if hasDate {
|
||||
self.init(dateComponents, format: .YMD_HM)
|
||||
} else {
|
||||
self.init(dateComponents, format: .HM)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// T/space
|
||||
if !(scanner.scanString("T", intoString: nil) || scanner.scanString(" ", intoString: nil)) {
|
||||
// :
|
||||
if !scanner.scanString(":", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HH
|
||||
var hour: Int = 0
|
||||
if scanner.scanInteger(&hour) && hour >= 0 && hour <= 23 {
|
||||
dateComponents.hour = hour
|
||||
// SS
|
||||
var second: Int = 0
|
||||
if scanner.scanInteger(&second) && second >= 0 && second <= 59 {
|
||||
dateComponents.second = second
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
// :
|
||||
if !scanner.scanString(":", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MM
|
||||
var minute: Int = 0
|
||||
if scanner.scanInteger(&minute) && minute >= 0 && minute <= 59 {
|
||||
dateComponents.minute = minute
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// YYYY-MM-DD HH:MM
|
||||
if scanner.atEnd {
|
||||
return dateComponents
|
||||
}
|
||||
|
||||
// :
|
||||
if !scanner.scanString(":", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SS
|
||||
var second: Int = 0
|
||||
if scanner.scanInteger(&second) && second >= 0 && second <= 59 {
|
||||
dateComponents.second = second
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// YYYY-MM-DD HH:MM:SS
|
||||
if scanner.atEnd {
|
||||
return dateComponents
|
||||
}
|
||||
|
||||
// .
|
||||
if !scanner.scanString(".", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SSS
|
||||
var millisecondDigits: NSString? = nil
|
||||
if scanner.scanCharactersFromSet(NSCharacterSet.decimalDigitCharacterSet(), intoString: &millisecondDigits), var millisecondDigits = millisecondDigits {
|
||||
if millisecondDigits.length > 3 {
|
||||
millisecondDigits = millisecondDigits.substringToIndex(3)
|
||||
// [YYYY-MM-DD] HH:MM:SS
|
||||
if scanner.atEnd {
|
||||
if hasDate {
|
||||
self.init(dateComponents, format: .YMD_HMS)
|
||||
} else {
|
||||
self.init(dateComponents, format: .HMS)
|
||||
}
|
||||
return
|
||||
}
|
||||
dateComponents.nanosecond = millisecondDigits.integerValue * 1_000_000
|
||||
} else {
|
||||
|
||||
// .
|
||||
if !scanner.scanString(".", intoString: nil) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SSS
|
||||
var millisecondDigits: NSString? = nil
|
||||
if scanner.scanCharactersFromSet(NSCharacterSet.decimalDigitCharacterSet(), intoString: &millisecondDigits), var millisecondDigits = millisecondDigits {
|
||||
if millisecondDigits.length > 3 {
|
||||
millisecondDigits = millisecondDigits.substringToIndex(3)
|
||||
}
|
||||
dateComponents.nanosecond = millisecondDigits.integerValue * 1_000_000
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// [YYYY-MM-DD] HH:MM:SS.SSS
|
||||
if scanner.atEnd {
|
||||
if hasDate {
|
||||
self.init(dateComponents, format: .YMD_HMSS)
|
||||
} else {
|
||||
self.init(dateComponents, format: .HMSS)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Unknown format
|
||||
return nil
|
||||
}
|
||||
|
||||
return dateComponents
|
||||
}
|
||||
Reference in new issue
Block a user