From d08acf72984669cd9adfd70ae0e620a9439ec327 Mon Sep 17 00:00:00 2001 From: kubo_takaichi Date: Wed, 27 May 2015 12:25:46 +0900 Subject: [PATCH] Add a dateTime formatter candidate --- .../src/main/resources/swift/Models.mustache | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift/Models.mustache b/modules/swagger-codegen/src/main/resources/swift/Models.mustache index 587b710326b..31c055b54dc 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Models.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Models.mustache @@ -97,17 +97,24 @@ class Decoders { static private func initialize() { dispatch_once(&once) { - let dateTimeFormatter = NSDateFormatter() - dateTimeFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" - let dateFormatter = NSDateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd" + let formatters = [ + "yyyy-MM-dd", + "yyyy-MM-dd'T'HH:mm:ssZZZZZ", + "yyyy-MM-dd'T'HH:mm:ss'Z'" + ].map { (format: String) -> NSDateFormatter in + let formatter = NSDateFormatter() + formatter.dateFormat = format + return formatter + } // Decoder for NSDate Decoders.addDecoder(clazz: NSDate.self) { (source: AnyObject) -> NSDate in let sourceString = source as! String - if count(sourceString) == 10 { - return dateFormatter.dateFromString(sourceString)! + for formatter in formatters { + if let date = formatter.dateFromString(sourceString) { + return date + } } - return dateTimeFormatter.dateFromString(sourceString)! + fatalError("formatter failed to parse \(sourceString)") } {{#models}}{{#model}} // Decoder for {{{classname}}}