Skip to content

Instantly share code, notes, and snippets.

@teofiloisrael
Last active December 18, 2015 22:39
Show Gist options
  • Save teofiloisrael/5855913 to your computer and use it in GitHub Desktop.
Save teofiloisrael/5855913 to your computer and use it in GitHub Desktop.
Timer que se dispara cada media noche
//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