I have a little project that is a timer that counts down 'til midnight, and I was wondering If I should leave it as it is, calculating the time until midnight each second (inside NSTimer method that gets called every 1 second):
NSDate *now = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[self gregorian] components:
(NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit) fromDate:now];
[now release];
NSUInteger hour = 23 - [dateComponents hour];
NSUInteger min = 59 - [dateComponents minute];
NSUInteger sec = 59 - [dateComponents second];
NSString *time = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d",
hour, min, sec];
[[self lblCountDown] setText:
[time stringByReplacingOccurrencesOfString:@"1" withString:@" 1"]];
[time release];
or should I just calculate that the first time, and then every other time just subtract one second each time since its synced to 1 second per call? I'm not worried that this will take longer than 1 second, but if the result will be the same the other way there's no reason not to optimize.. so anyway, should I do it that way? And why?
Thanks.