Skip to content

Instantly share code, notes, and snippets.

@omochi
Created March 29, 2022 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/94bf1009bf62440056db0dd0c109c6c9 to your computer and use it in GitHub Desktop.
Save omochi/94bf1009bf62440056db0dd0c109c6c9 to your computer and use it in GitHub Desktop.
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