Last active
December 18, 2015 22:39
-
-
Save teofiloisrael/5855913 to your computer and use it in GitHub Desktop.
Timer que se dispara cada media noche
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Update each midnight | |
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; | |
[cal setTimeZone:[NSTimeZone systemTimeZone]]; | |
NSDateComponents * comp = [cal components:( NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; | |
[comp setMinute:0]; | |
[comp setHour:0]; | |
NSDate *startOfToday = [cal dateFromComponents:comp]; | |
NSDate *midnight = [startOfToday dateByAddingTimeInterval:60 * 60 * 24]; | |
//////////////////////////////////////////////////////////////////////// | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; | |
//Optionally for time zone converstions | |
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; | |
NSString *stringFromDate = [formatter stringFromDate:midnight]; | |
[formatter release]; | |
NSLog(@"El timer se disparará el %@", stringFromDate); | |
//////////////////////////////////////////////////////////////////////// | |
self.midnightTimer = [[NSTimer alloc] initWithFireDate:midnight | |
interval:60 * 60 * 24 | |
target:self | |
selector:@selector(timerFired:) | |
userInfo:nil | |
repeats:YES]; | |
NSRunLoop *runloop = [NSRunLoop currentRunLoop]; | |
[runloop addTimer:self.midnightTimer forMode:NSDefaultRunLoopMode]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment