From 9581bf08b767ed260936ec87e45ac773164938d3 Mon Sep 17 00:00:00 2001 From: Alexander Ross Date: Wed, 20 Nov 2019 23:15:25 +0100 Subject: [PATCH] Add Date.iso8601 helper --- Sources/EasyMode/Date.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sources/EasyMode/Date.swift b/Sources/EasyMode/Date.swift index 8dedf5b..3cce0a8 100644 --- a/Sources/EasyMode/Date.swift +++ b/Sources/EasyMode/Date.swift @@ -12,10 +12,21 @@ extension Date { public var isoDate: String { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" - formatter.timeZone = TimeZone(abbreviation: "CET")! return formatter.string(from: self) } + public var iso8601Date: String { isoDate } + + public var iso8601: String { + if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) { + return ISO8601DateFormatter.string(from: self, timeZone: TimeZone.current, formatOptions: .withInternetDateTime) + } else { + var buffer = [CChar](repeating: 0, count: 25) + var time = time_t(self.timeIntervalSince1970) + strftime_l(&buffer, buffer.count, "%FT%T%z", localtime(&time), nil) + return String(cString: buffer) + } + } public static func from(year: Int, month: Int, day: Int) -> Date { let gregorianCalendar = Calendar(identifier: .gregorian)