Date Formats and Do Not Disturb

Perhaps fitting that my return to this blog is dealing with the same thing as my last post, date formatters. Hopefully if you are an iOS developer you have searched your code for YYYY, ISO week date year, and replaced it with yyyy. However if you are showing the date to the user then you probably shouldn't be using your own format string anyway.

The details are hazy, but I believe around iPhone OS 3.0, my cousin Matt did some digging into the system. At the time he wanted to build an app that would've required or worked better if it could run once a day via something like the cron. He discovered that either part of Springboard, or at a similar level, was a scheduler much like cron. Of course this was all unavailable to developers publicly, but you could submit jobs, to this scheduler and at the appointed time it would kick off the requested application. As I recall he also noticed that something like a daily alarm would only have one entry in the plist, and when the alarm was woken up to go off, it would put itself in the schedule to go off tomorrow.

Now one might wonder how this affects something like do not disturb which should in theory turn on at a certain time of the day, and turn off at another. Only having one process, this scheduler, running all the time saves battery instead of having the alarm, do not disturb, calendar, etc. all checking to see if it's time to do something. Indeed I believe the scheduler itself isn't running all the time since it knew when it next needed to do something it could also lay dormant until the appointed time. For good or bad this scheduler seems to take a full date and time (and probably time zone information as well), and so if you schedule that do not disturb needs to run between Jan 1 and Jan 6 and you date formatter string has YYYY instead of yyyy, then it is going into the scheduler for 2012-01-02 08:00:00 for example. The reason why it will start working again January 7th is that the ISO week date year will be 2013.

The take away is dates and times are hard. They seem straight forward, but can quickly turn into a mess. You should double check your formatter strings to make sure they mean what you want. You should search your code for YYYY and change it to yyyy if that is what you really want, and you should really consider how you use NSDateFormatter in case you would be better served using styles instead of a custom date format string.