@@ -67,24 +67,24 @@ private let codeTemplateForCreatableWithJSON = [
67
67
68
68
private let codeTemplateForDateParsing = [
69
69
" extension Date { " ,
70
- " // Date parsing is serialized on a dedicated queue because DateFormatter is not thread-safe . " ,
71
- " private static let parsingQueue = DispatchQueue(label: \" JSONDateParsing \" ) " ,
70
+ " // Date formatters are cached because they are expensive to create. All cache access is performed on a serial queue . " ,
71
+ " private static let cacheQueue = DispatchQueue(label: \" DateFormatterCacheQueue \" ) " ,
72
72
" private static var formatterCache = [String: DateFormatter]() " ,
73
73
" private static func dateFormatter(with format: String) -> DateFormatter { " ,
74
74
" if let formatter = formatterCache[format] { return formatter } " ,
75
75
" let formatter = DateFormatter() " ,
76
76
" formatter.dateFormat = format " ,
77
+ " formatter.locale = Locale(identifier: \" en_US_POSIX \" ) " ,
78
+ " formatter.calendar = Calendar(identifier: .gregorian) " ,
79
+ " formatter.timeZone = TimeZone(secondsFromGMT: 0)! // UTC is assumed, but won't interfere with a format-specified time zone. " ,
77
80
" formatterCache[format] = formatter " ,
78
81
" return formatter " ,
79
82
" } " ,
80
83
" " ,
81
84
" static func parse(string: String, format: String) -> Date? { " ,
82
- " var date: Date? " ,
83
- " parsingQueue.sync { " ,
84
- " let formatter = dateFormatter(with: format) " ,
85
- " date = formatter.date(from: string) " ,
86
- " } " ,
87
- " return date " ,
85
+ " var formatter: DateFormatter! " ,
86
+ " cacheQueue.sync { formatter = dateFormatter(with: format) } " ,
87
+ " return formatter.date(from: string) " ,
88
88
" } " ,
89
89
" " ,
90
90
" init?(json: [String: Any], key: String, format: String) { " ,
0 commit comments