How Much Difference Can Capitalization Make?
October 03, 2012In our iPad app we have some code that parses a special datetime format generated in our web service layer, and then determines what day of the week the date is. Pretty innocuous stuff. Worked like a charm in iOS 5.
When iOS 6 came out, we found the day of the week suddenly appeared to be off by a few days. Instead of thursday we got tuesday!
WTF? How does Apple break NSDateFormatter?
The string was converted to a date similar to this example:
NSString* s = @"201212011023";
NSDateFormatter* f = [[NSDateFormatter alloc]init];
f.dateFormat = @"YYYYMMddhhmm";
NSDate* d = [f dateFromString:s];
In iOS 5.X the date was correctly generated. The next step formatted this NSDate object with the day of the week, month and day. All was fine.
Now enter iOS 6.0: the day of the week suddenly switched to being off by 2-4 days, yet the day and month was still correct. What kind of odd bug could make this happen?
The reason the day of the week was wrong but the date was correct was that the year generated was 2011 instead of 2012. So the day of the week matched last year. How could this happen?
Turns out the problem is rather interesting, as in the old Irish curse "may you live in interesting times".
The correct date format is actually "yyyyMMddhhmm". The capital-Y year format is explained in the Apple documentation (which I assume is built on top of some standard unix functionality) as:
Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.*
Clearly Apple changed the behavior in iOS 6 as either a better implementation, a bug fix, a change in interpretation or something not entirely clear. I wonder how many people actually use such non-standard years?
Fortunately it is only a single label in a summary page which does not affect any functionality (but might confuse a user). It will be immediately (as in App Store immediately, a somewhat more geological timeframe) updated.
In this case the problem was a typo. Ah, the joys of untyped constants.