I'm parsing a fixed date format. Here I'm parsing a date that does not exist, so I expect to get nil returned from the date formatter, and that's what I get:
let currentCal = DateFormatter()
currentCal.locale = Locale(identifier: "en_US_POSIX")
currentCal.timeZone = TimeZone(identifier: "America/Chicago")!
currentCal.dateFormat = "yyyy-MM-dd HH:mm:ss"
currentCal.date(from: "2010-03-14 02:15:00") // => nil
(In 2010, Daylight Saving Time began in Chicago on March 14, so clocks rolled over from 1:59:59 AM to 3:00:00 AM. 2:15 AM didn't happen that day.)
However, if I set the calendar explicitly, I get a date:
let specifiedCal = DateFormatter()
specifiedCal.calendar = Calendar(identifier: .gregorian)
specifiedCal.locale = Locale(identifier: "en_US_POSIX")
specifiedCal.timeZone = TimeZone(identifier: "America/Chicago")!
specifiedCal.dateFormat = "yyyy-MM-dd HH:mm:ss"
specifiedCal.date(from: "2010-03-14 02:15:00") // => a Date
I've since learned that the locale includes calendar info, so there's no need for me to set it. That said, I can't find any indication in the documentation or the NSDateFormatter header file that explains what's going on. Intuitively it seems like it shouldn't make a difference, so why the change in behavior?