Checking for a Day Change Timezone Adjusted

I just took a stab at creating some code (pasted below) that would check for a day change. However, now I'm reading that there's a special function for day changes.

NSCalendarDayChangedNotification

Apparently it works in iOS8 and later. But people keep using the phrase "you can listen to it"... as if using that function is like tuning into a radio station. I looked it up and the last WWDC provided a presentation with some relevant code:

Reacting to the Change of Day • You may want to run some code when the day changes NSCalendarDayChangedNotification • Example

noteCenter = [NSNotificationCenter defaultCenter];
observer = [noteCenter addObserverForName:NSCalendarDayChangedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
// your code here
}];

My Question

The PDF I found doesn't say much else though. So my question is how does this function work? The PDF makes it seem like it truly is just this simple, paste that code in and I'm done. Is that true? Will it adjust for timezones? Do I have to enable this notification center that it is referencing?

My code (not sure if it would work):

    //Find out if it's a new day
    let lastDateUsed_NSDefault = NSUserDefaults.standardUserDefaults()
    if let endOf_LastDateUsed = lastDateUsed_NSDefault.objectForKey("lastDateUsed") as! NSDate! {
        print("Success! NSUserDefault key: 'lastDateUsed' returned \(endOf_LastDateUsed)")
        //Check if the day has changed

        let calendar = NSCalendar.currentCalendar()
        let startOfToday = calendar.startOfDayForDate(now)

        if endOf_LastDateUsed.compare(startOfToday) == NSComparisonResult.OrderedAscending {
            //endOf_LastDateUsed is greater than startOfToday.
            print("Do nothing! Last date used must have been today!")
        } else if endOf_LastDateUsed.compare(startOfToday) == NSComparisonResult.OrderedAscending {
            //Day has changed. Run alert.


            //Save "endOfToday" date value to NSDefault
            let endOfToday = startOfToday.dateByAddingTimeInterval(24 * 60 * 60)
            NSUserDefaults.standardUserDefaults().setObject(endOfToday, forKey:"lastDateUsed")
            print("Time stored in NSDefault: \(endOfToday)")
        }

    }
    else {
        print("Failure! NSUserDefault key: 'lasteDateUsed' returned nothing. No record.")
        //First time user - Set end of today
        let calendar = NSCalendar.currentCalendar()
        let startOfToday = calendar.startOfDayForDate(now)
        print("startOfToday: \(startOfToday)")
        let endOfToday = startOfToday.dateByAddingTimeInterval(24 * 60 * 60)
        //Store
        NSUserDefaults.standardUserDefaults().setObject(endOfToday, forKey:"lastDateUsed")

        print("Time stored in NSDefault: \(endOfToday)")
    }
Jon Skeet
people
quotationmark

The documentation (which was slightly harder to locate than I'd have hoped, admittedly) is reasonably clear:

Posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone.

So yes, it should handle daylight saving changes appropriately, given that it knows about the time zone. I don't know about enabling the notification centre, but that should be easy enough to test by adjusting the system clock or time zone on a device and seeing what happens.

(What isn't clear from the docs is whether this event is fired if the date is changed via manual intervention, e.g. through changing the system time or time zone in a way that directly changes the date. It would be worth you experimenting with that.)

people

See more on this question at Stackoverflow