tags:

views:

124

answers:

2

In Cocoa, is there a notification i can register for, that informs me, when a new day begins - at 00h:00min:01s in the morning?

+5  A: 

There's no notification that I know of. You can get a timer to fire whenever a new day begins like so:

[[NSTimer alloc] initWithFireDate:midnight
                         interval:60 * 60 * 24 //one day, in seconds
                           target:someObj
                         selector:@selector(someSelector)
                         userInfo:nil
                          repeats:YES];

The trick is getting an NSDate set to midnight. Check the Date and Time Programming Guide for how to do that with date components and the like.

EDIT: see this question for how to get the midnight NSDate.

Tom Dalling
+6  A: 

If it is for iPhone development, you can also listen for a UIApplicationSignificantTimeChangeNotification. It gets posted on more occasions than the arrival of midnight, but when you receive one, you can simply check if you are on or near midnight.

For Mac OS X, you would have to do what Tom Dalling suggests but you should also keep track of changes to the system clock yourself (in order to update your timer) as well as changes to the current time zone.

Jason Coco
+1 I've never heard of that notification before. Thanks!
Dave DeLong
Thank you both for the answers. It seems, i can only accept one of them and not yet vote them up. Unfortunately it’s for Mac OS X - UIApplicationSignificantTimeChangeNotification would have been exactly what i was looking for..