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

Add support for persisting dates with microsecond precision #29

Merged
merged 1 commit into from Mar 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/PostgreSQL/Data/PostgreSQLData+Date.swift
Expand Up @@ -24,8 +24,8 @@ extension Date: PostgreSQLDataConvertible {
switch data.type {
case .timestamp, .time:
let microseconds = try value.makeFixedWidthInteger(Int64.self)
let seconds = microseconds / _microsecondsPerSecond
return Date(timeInterval: Double(seconds), since: _psqlDateStart)
let seconds = Double(microseconds) / Double(_microsecondsPerSecond)
return Date(timeInterval: seconds, since: _psqlDateStart)
case .date:
let days = try value.makeFixedWidthInteger(Int32.self)
let seconds = days * _secondsInDay
Expand All @@ -37,7 +37,7 @@ extension Date: PostgreSQLDataConvertible {

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

Expand Down