Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for datetime string formats #42

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Sources/SQLiteNIO/SQLiteDataConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ extension Date: SQLiteDataConvertible {
value = v
case .integer(let v):
value = Double(v)
case .text(let v):
guard let d = dateTimeFormatter.date(from: v) ?? dateFormatter.date(from: v) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of the "try one and then the other" approach this requires, but I can't think of a good alternative either.

return nil
}
self = d
return
default:
return nil
}
Expand All @@ -136,3 +142,26 @@ extension Date: SQLiteDataConvertible {
return .float(timeIntervalSince1970)
}
}

/// Matches dates from the `datetime()` function
let dateTimeFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate,
.withSpaceBetweenDateAndTime,
.withTime,
.withColonSeparatorInTime
]
return formatter
}()

/// Matches dates from the `date()` function
let dateFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate
]
return formatter
}()
20 changes: 20 additions & 0 deletions Tests/SQLiteNIOTests/SQLiteNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ final class SQLiteNIOTests: XCTestCase {
print(rows)
}

func testDateFormat() throws {
let conn = try SQLiteConnection.open(storage: .memory, threadPool: self.threadPool, on: self.eventLoop).wait()
defer { try! conn.close().wait() }

XCTAssertEqual(Date(sqliteData: .text("2023-03-10"))?.timeIntervalSince1970, 1678406400)

let rows = try conn.query("SELECT CURRENT_DATE").wait()
XCTAssertNotNil(Date(sqliteData: rows[0].column("CURRENT_DATE")!))
}

func testDateTimeFormat() throws {
let conn = try SQLiteConnection.open(storage: .memory, threadPool: self.threadPool, on: self.eventLoop).wait()
defer { try! conn.close().wait() }

XCTAssertEqual(Date(sqliteData: .text("2023-03-10 23:54:27"))?.timeIntervalSince1970, 1678492467)

let rows = try conn.query("SELECT CURRENT_TIMESTAMP").wait()
XCTAssertNotNil(Date(sqliteData: rows[0].column("CURRENT_TIMESTAMP")!))
}

func testTimestampStorage() throws {
let conn = try SQLiteConnection.open(storage: .memory, threadPool: self.threadPool, on: self.eventLoop).wait()
defer { try! conn.close().wait() }
Expand Down