I'm trying to put together a utility class that converts to/from ISO8601 representations. So far, I've got the following:
import Foundation
class ISODateConversion {
static let WithMilliseconds = NSDateFormatter()
static let SansMilliseconds = NSDateFormatter()
class func dateFromString(input:String) -> NSDate {
return WithMilliseconds.dateFromString(input) ?? SansMilliseconds.dateFromString(input)
}
class func stringFromDate(date:NSDate) ->String {
return SansMilliseconds.stringFromDate(date)
}
}
But I need to more than just instantiate those two formatters. I want them to be initialized with the following expressions:
WithMilliseconds.timeZone = NSTimeZone(abbreviation: "GMT")
WithMilliseconds.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
SansMilliseconds.timeZone = NSTimeZone(abbreviation: "GMT")
SansMilliseconds.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
NSDateFormatter
doesn't have an in init() that allows me set those at creation time. But I only need them to be created once and then just reused. What's the right pattern/way to do this.
(I also think that I should change this to be extensions to String and NSDate, but I still have the issue of how I have my module do a one time setup of these reusable formatters).