Skip to content

Commit

Permalink
Add Date.iso8601 helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ross committed Nov 20, 2019
1 parent 04d43b4 commit 9581bf0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Sources/EasyMode/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9581bf0

Please sign in to comment.