Created
March 29, 2022 07:32
-
-
Save omochi/94bf1009bf62440056db0dd0c109c6c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension YearMonthDay: PostgresDataConvertible { | |
public static var postgresDataType: PostgresDataType { | |
.date | |
} | |
public init?(postgresData: PostgresData) { | |
guard let date = postgresData.date else { | |
return nil | |
} | |
self.init(date: date, calendar: .gregorian, timeZone: .gmt) | |
} | |
public var postgresData: PostgresData? { | |
guard let date = self.date(calendar: .gregorian, timeZone: .gmt) else { | |
return nil | |
} | |
let seconds = Int(date.timeIntervalSince(Self.postgresOriginDate)) / 86400 | |
let value = ByteBuffer(integer: Int32(seconds)) | |
return PostgresData( | |
type: .date, | |
typeModifier: nil, | |
formatCode: .binary, | |
value: value | |
) | |
} | |
private static let postgresOriginDate: Date = { | |
ISO8601DateFormatter().date(from: "2000-01-01T00:00:00Z")! | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment