Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i get a notification, when a new day begins?

Tags:

cocoa

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?

like image 789
Helene Bilbo Avatar asked May 30 '10 13:05

Helene Bilbo


2 Answers

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.

like image 147
Tom Dalling Avatar answered Nov 16 '22 03:11

Tom Dalling


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.

like image 38
Jason Coco Avatar answered Nov 16 '22 04:11

Jason Coco