views:

195

answers:

3

I have created an application.in the app i start a countdown timer and save current date as double value in database also i save end date according to countdown timer .after closing and return to app i get time form database find the difference between current date and start date and set the countdown according to that.but a problem occurs when i change the time 8 hours ahead then the count down timer behaves differently.how do i find that user has changed time or time zone? by the if i change time zone suppose new delhi to usa it works fine but in the same time zone if i increase/decrease date or time it does not behave as expected.how do i manage this?

Also i had seen a strange problem when increase/decrease date or time the app closes as soon as it launched.i am unable to figure it out

+1  A: 

For your timer to be exact, you should just use the end date. I'm not sure why you want to keep the start date.

Here's an example with a continuous countdown:

// I'm assuming you want to update the countdown every seconds
// So you should set a timer like this somewhere
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick) userInfo:nil repeats:YES];

- (void)updateCountdown {
 NSTimeInterval timeInterval = [endDate timeIntervalSinceNow];

 unsigned int d, h, m, s;
 NSTimeInterval r;

 d = timeInterval / (24 * 3600);
 r = timeInterval - d * 24 * 3600;

 h = r / 3600;
 r = r - h * 3600;

 m = r / 60;
 r = r - m * 60;

 s = r;

 NSString *countdown = [NSString stringWithFormat:@"%.2d:%.2d:%.2d:%.2d", d, h, m, s];
 // Now you could use countdown to update the text of a UILabel
}

- (void)timerTick {
 if ([endDate compare:[NSDate date]] == NSOrderedDescending) {
  //endDate is later in time than "now", ie not yet reached
  [self updateCountdown];
 } else {
  //stop the countdown
  [timer invalidate];
 }
}

endDate should be a NSDate containing the date when the countdown should reach 0.

Because the countdown is adjusted every seconds, you won't have issues even if the system time or timezone changes.

However make sure you save and restore endDate correctly upon app restart/close (i.e. use a fixed reference always! I would use the timeIntervalSinceReferenceDate and dateWithTimeIntervalSinceReferenceDate: from the NSDate class).

Jean Regisser
A: 

If you're just interested in having a timer firing once at endDate use something like this:

NSTimer* timer = [[NSTimer alloc] initWithFireDate:endDate
                                          interval:0.0f
                                            target:self
                                          selector:@selector(endDateReached:)
                                          userInfo:nil
                                           repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];
Jean Regisser
A: 

I'm not sure how you can get around the user changing the time while your application isn't running. But I don't think you need to store the startDate or the duration of the timer. What you could do is store the endDate in the database. I would try and store it in a timezone agnostic manner, like GMT.

Then the next time your application is started, take the dates stored in your database and convert them back to the local time. Also get the current time from the NSDate class when your application is started. The current time can then be subtracted from the converted endDate to calculate the timer duration.

This should allow the user to change their time zone and still fire events at the correct time. You'd also have less to store in your database.

Randaltor
i am doing exactly what you saying.i am storing end date in db and when relaunching i'm getting local time.but when getting current time from nsdate class if i change the time 2 hours back the current nsdate will return wrong time.so that difference between end date and current date is not as expected.that is the proble.how do i overcome this problem
Rahul Vyas