Skip to content

Commit 42fbe05

Browse files
authored
Merge pull request vapor#29 from MrMage/fix-date-format
Add support for persisting dates with microsecond precision
2 parents efba3a4 + c36db27 commit 42fbe05

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+Date.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ extension Date: PostgreSQLDataConvertible {
2424
switch data.type {
2525
case .timestamp, .time:
2626
let microseconds = try value.makeFixedWidthInteger(Int64.self)
27-
let seconds = microseconds / _microsecondsPerSecond
28-
return Date(timeInterval: Double(seconds), since: _psqlDateStart)
27+
let seconds = Double(microseconds) / Double(_microsecondsPerSecond)
28+
return Date(timeInterval: seconds, since: _psqlDateStart)
2929
case .date:
3030
let days = try value.makeFixedWidthInteger(Int32.self)
3131
let seconds = days * _secondsInDay
@@ -37,7 +37,7 @@ extension Date: PostgreSQLDataConvertible {
3737

3838
/// See `PostgreSQLDataCustomConvertible.convertToPostgreSQLData()`
3939
public func convertToPostgreSQLData() throws -> PostgreSQLData {
40-
return PostgreSQLData(type: .timestamp, format: .text, data: Data(description.utf8))
40+
return PostgreSQLData(type: .timestamp, format: .binary, data: Int64(self.timeIntervalSince(_psqlDateStart) * Double(_microsecondsPerSecond)).data)
4141
}
4242
}
4343

0 commit comments

Comments
 (0)