Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified your code to not recreate NSDateFormatter each iteration #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions NSDateISO8601.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@

import Foundation

public extension NSDate {
public class func ISOStringFromDate(date: NSDate) -> String {
private struct ISONSDateFormatters {

static let ISOStringFromDateFormatter: NSDateFormatter = {
var dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"

return dateFormatter.stringFromDate(date).stringByAppendingString("Z")
}
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
return dateFormatter
}()

public class func dateFromISOString(string: String) -> NSDate {
static let dateFromISOStringFormatter: NSDateFormatter = {
var dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

return dateFormatter.dateFromString(string)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()

}

extension NSDate {

class func ISOStringFromDate(date: NSDate) -> String {
return ISONSDateFormatters.ISOStringFromDateFormatter.stringFromDate(date).stringByAppendingString("Z")
}

class func dateFromISOString(string: String) -> NSDate? {
return ISONSDateFormatters.dateFromISOStringFormatter.dateFromString(string)
}
}