Skip to content

Commit 2901a70

Browse files
authored
Fixes to date synchronization. (#387)
* Fixes to date synchronization. * add test for bad representation * back out of pending changes stuff * clean up * wip
1 parent dfd2359 commit 2901a70

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

Sources/SQLiteData/Internal/ISO8601.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import Foundation
33
extension Date {
44
@usableFromInline
55
var iso8601String: String {
6+
let nextUpDate = Date(timeIntervalSinceReferenceDate: timeIntervalSinceReferenceDate.nextUp)
67
if #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) {
7-
return formatted(.iso8601.currentTimestamp(includingFractionalSeconds: true))
8+
return
9+
nextUpDate
10+
.formatted(
11+
.iso8601.currentTimestamp(includingFractionalSeconds: true)
12+
)
813
} else {
9-
return DateFormatter.iso8601(includingFractionalSeconds: true).string(from: self)
14+
return DateFormatter.iso8601(includingFractionalSeconds: true)
15+
.string(from: nextUpDate)
1016
}
1117
}
1218

Tests/SQLiteDataTests/CloudKitTests/PreviewTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
}
4444
}
4545

46-
@Test
4746
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
48-
func delete() async throws {
47+
@Test func delete() async throws {
4948
@FetchAll(RemindersList.all, database: userDatabase.database) var remindersLists
5049

5150
try await userDatabase.userWrite { db in
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import DependenciesTestSupport
2+
import Foundation
3+
import SQLiteData
4+
import SQLiteDataTestSupport
5+
import Testing
6+
7+
@Suite(.dependency(\.defaultDatabase, try .database()))
8+
struct DateTests {
9+
@Dependency(\.defaultDatabase) var database
10+
11+
@Test func roundtrip() throws {
12+
let date = Date(timeIntervalSinceReferenceDate: 793109282.061)
13+
let insertedRecord = try database.write { db in
14+
try Record.insert { Record.Draft(date: date) }
15+
.returning(\.self)
16+
.fetchOne(db)!
17+
}
18+
let updatedRecord = try database.write { db in
19+
try Record
20+
.update(insertedRecord)
21+
.returning(\.self)
22+
.fetchOne(db)!
23+
}
24+
#expect(insertedRecord.date == date)
25+
#expect(insertedRecord.date == updatedRecord.date)
26+
}
27+
}
28+
29+
@Table
30+
private struct Record: Equatable {
31+
let id: Int
32+
var date: Date
33+
}
34+
35+
extension DatabaseWriter where Self == DatabaseQueue {
36+
fileprivate static func database() throws -> DatabaseQueue {
37+
let database = try DatabaseQueue()
38+
try database.write { db in
39+
try #sql(
40+
"""
41+
CREATE TABLE "records" (
42+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
43+
"date" TEXT NOT NULL
44+
) STRICT
45+
"""
46+
)
47+
.execute(db)
48+
}
49+
return database
50+
}
51+
}

0 commit comments

Comments
 (0)