The curious case of the phantom datesPosted: | Author: Jörn | Filed under: iOS, Swift | Tags: DateFormatter | No Comments »
Recently I had to fix a bug where a DateFormatter would return nil when trying to convert a String to a Date. The date string was in a perfectly valid format and the formatter would return nil while on two dates when all the other dates could be transformed without problem.
Here is a screenshot of a playground that I used to examine the problem:
As you can see the formatter does not have a problem with 2022-03-27T01:30:00 but for some reason it cannot transform 2022-03-27T02:00:00 to a valid date
I observed that this invalid dates only occur on March 27th between 02:00a.m. and 02:59a.m.
And then it dawned on me. Daylight Saving Time! Of course. In my country we change our clocks on March 27th directly from 01:59 to 03:00. So the formatter is correctly returning nil when trying to format a date that lies in the hour that does not exist during that night.
So a DateFormatter is quite clever and knows about the oddities of DST. Turns out, that DateFormatter is even more clever when you want it to be: By setting its isLenient property to true the formatter uses heuristics to guess what date you wanted when you provides the date string for a date that does not exist.
Here is what happens when you let the DateFormatter guess what you really wanted:
As you can see now the formatter correctly transforms the 02:00a.m. date to 03:00.a.m. during the night of the switch to Daylight Saving Time.
This might not always be what you want, but in my case it was the (easy) fix to the bug.
Leave a Reply