From b7e477559c230f10bbe12f11ba4c6c56000b88db Mon Sep 17 00:00:00 2001 From: pruthvikar Date: Fri, 19 May 2017 21:46:54 +0200 Subject: [PATCH 1/3] Update BinaryUtils.swift replaced internal reference date interval calculation with Foundation --- Sources/PostgreSQL/Bind/BinaryUtils.swift | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Sources/PostgreSQL/Bind/BinaryUtils.swift b/Sources/PostgreSQL/Bind/BinaryUtils.swift index 76501e3..b20994d 100644 --- a/Sources/PostgreSQL/Bind/BinaryUtils.swift +++ b/Sources/PostgreSQL/Bind/BinaryUtils.swift @@ -216,15 +216,6 @@ 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)! - }() - } - static func parseTimetamp(value: UnsafeMutablePointer, isInteger: Bool) -> Date { let interval: TimeInterval if isInteger { @@ -234,7 +225,7 @@ struct BinaryUtils { let seconds = parseFloat64(value :value) interval = TimeInterval(seconds) } - return Date(timeInterval: interval, since: TimestampConstants.referenceDate) + return Date(timeIntervalSinceReferenceDate: interval) } // MARK: - Interval From 08aa39465be2c40aa7a7f0513f15958cd69010c5 Mon Sep 17 00:00:00 2001 From: pruthvikar Date: Fri, 19 May 2017 21:48:39 +0200 Subject: [PATCH 2/3] Update Bind.swift replaced removed function with timeIntervalSinceReferenceDate --- Sources/PostgreSQL/Bind/Bind.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PostgreSQL/Bind/Bind.swift b/Sources/PostgreSQL/Bind/Bind.swift index ebd87ef..bf7eaf9 100644 --- a/Sources/PostgreSQL/Bind/Bind.swift +++ b/Sources/PostgreSQL/Bind/Bind.swift @@ -145,7 +145,7 @@ 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.timeIntervalSinceReferenceDate if configuration.hasIntegerDatetimes { let microseconds = Int64(interval * 1_000_000) From f3eee229addfe8152b7ea78bccc4cec87d5a0746 Mon Sep 17 00:00:00 2001 From: pruthvikar Reddy Date: Sat, 20 May 2017 01:28:34 +0200 Subject: [PATCH 3/3] Changed reference date to correct year (2000 as opposed to 2001) --- Sources/PostgreSQL/Bind/BinaryUtils.swift | 9 ++++++++- Sources/PostgreSQL/Bind/Bind.swift | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/PostgreSQL/Bind/BinaryUtils.swift b/Sources/PostgreSQL/Bind/BinaryUtils.swift index b20994d..675907d 100644 --- a/Sources/PostgreSQL/Bind/BinaryUtils.swift +++ b/Sources/PostgreSQL/Bind/BinaryUtils.swift @@ -215,6 +215,13 @@ struct BinaryUtils { } // MARK: - Date / Time + + struct TimestampConstants { + // 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 { let interval: TimeInterval @@ -225,7 +232,7 @@ struct BinaryUtils { let seconds = parseFloat64(value :value) interval = TimeInterval(seconds) } - return Date(timeIntervalSinceReferenceDate: interval) + return Date(timeInterval: interval, since: TimestampConstants.referenceDate) } // MARK: - Interval diff --git a/Sources/PostgreSQL/Bind/Bind.swift b/Sources/PostgreSQL/Bind/Bind.swift index bf7eaf9..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.timeIntervalSinceReferenceDate - + let interval = date.timeIntervalSince(BinaryUtils.TimestampConstants.referenceDate) + if configuration.hasIntegerDatetimes { let microseconds = Int64(interval * 1_000_000) var value = microseconds.bigEndian