开发者

NSDate slips back or forward an hour when in airplane mode or after DST transition

I have an application the plays an alarm sound at a scheduled time. This is setup as a recurring alarm that will go off every day at the same time. For example: Wake up at 6am every day.

I first noticed a problem after the DST transition. I would have an alarm that I created in December (for example) to go off at 6am every morning. After march the alarm started going off 1 hour earlier (or could have been later, can't remember). But开发者_开发百科 when I tried to debug the problem it would never happen, and by the time I sat down to really dig into it, I couldn't reproduce the problem.

Recently, when I go to bed, I've been putting my phone in airplane mode. This seems to reproduce the problem much more frequently. But there are times when everything works fine. But if I check the alarm times after I go to airplane mode, they will appear to be an hour later than I created. The NSDate will show up in my alarm table at 7am instead of 6am. But sometimes it shows up as 6am. So I turn the alarm on. But the next morning the alarm goes off at 7am.

I think it has to do with DST and the phone not knowing if it's in DST for the NSDate object. However, the actually time shown on my phone has never been wrong. But my alarms go off at the wrong time.

Here is a sample of my code: Notes: timerRec is a structured record from my database alarmTime is a NSDate object pulled from the database the NSLog value is there for debugging to see if I can get it to print out wrong But when I'm connected to the debugger it's never failed.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *tempAlarm = [dateFormatter stringFromDate:timerRec.alarmTime];
NSLog(@"scheduleAlarm for firedate: %@", tempAlarm);

[CCUtility scheduleAlarmForDate: timerRec.alarmTime alarmSound:
    [CCUtility convertSoundCodeToFileName: [timerRec.soundCode intValue]]
alarmMessage: message repeatInterval: repeatInterval];

Any help on this would be greatly appreciated. I can post additional code, but the main problem appears to be something weird about how I'm using NSDate and how the system clock works behind the scenes in airplane mode.

Thanks,

Kevin


You probably use a format that does not takes the timezone into account when storing your date.

Why don't you NSLog your timerRec NSDate directly instead of using an NSDateFormatter? In addition, logging the NSDate directly will display the timezone+DST information about the NSDate in the console, whereas the NSDateFromatterMediumStyle you use don't (using this datestyle is obviously really not the best style to use for debugging such issues regading TZ & DST!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜