diff --git a/Sources/PostgreSQL/Bind/BinaryUtils.swift b/Sources/PostgreSQL/Bind/BinaryUtils.swift index 76501e3..675907d 100644 --- a/Sources/PostgreSQL/Bind/BinaryUtils.swift +++ b/Sources/PostgreSQL/Bind/BinaryUtils.swift @@ -215,14 +215,12 @@ struct BinaryUtils { } // MARK: - Date / Time - + struct TimestampConstants { - static let referenceDate: Date = { - let components = DateComponents(year: 2000, month: 1, day: 1) - var calendar = Calendar(identifier: .gregorian) - calendar.timeZone = TimeZone(abbreviation: "UTC")! - return calendar.date(from: components)! - }() + // Foundation referenceDate is 00:00:00 UTC on 1 January 2001, + // the reference date we want is 00:00:00 UTC on 1 January 2000 + static let offsetTimeIntervalSinceFoundationReferenceDate: TimeInterval = -31_622_400 + static let referenceDate = Date(timeIntervalSinceReferenceDate: offsetTimeIntervalSinceFoundationReferenceDate) } static func parseTimetamp(value: UnsafeMutablePointer, isInteger: Bool) -> Date { diff --git a/Sources/PostgreSQL/Bind/Bind.swift b/Sources/PostgreSQL/Bind/Bind.swift index ebd87ef..4c417b8 100644 --- a/Sources/PostgreSQL/Bind/Bind.swift +++ b/Sources/PostgreSQL/Bind/Bind.swift @@ -145,8 +145,8 @@ public final class Bind { Creates an input binding from a Date. */ public convenience init(date: Date, configuration: Configuration) { - let interval = date.timeIntervalSince(BinaryUtils.TimestampConstants.referenceDate) - + let interval = date.timeIntervalSince(BinaryUtils.TimestampConstants.referenceDate) + if configuration.hasIntegerDatetimes { let microseconds = Int64(interval * 1_000_000) var value = microseconds.bigEndian