NSDate slides back or forward per hour when in airplane mode or after daylight saving time

I have an application that plays an audible alarm at a scheduled time. This setting is like a repeating alarm that will be triggered every day at the same time. For example: Wake up at 6 in the morning every day.

I first noticed a problem after daylight saving time. I would have an alarm that I set up in December (for example) to leave at 6 in the morning every morning. After the march, the alarm began to go away 1 hour earlier (or it could be later, I don’t remember). But when I tried to debug the problem, it would never happen, and by the time I sat down to really delve into it, I could not reproduce the problem.

Recently, when I went to bed, I put my phone on an airplane. This seems to solve the problem much more often. But there are times when everything works fine. But if I check the alarm time after I go to airplane mode, they will appear an hour later than I created. NSDate will show in my alarm table at 7am instead of 6am. But sometimes it appears like 6am. So I turn on the alarm. But the next morning the alarm went off at 7 in the morning.

I think this is due to the DST and the phone, not knowing if it is in the DST for the NSDate Object. However, the time shown on my phone was never wrong. But my worries go away at the wrong time.

Here is an example of my code: Notes: timerRec is a structured record from my database alarmTime is an NSDate object, the NSLog value extracted from the database is there for debugging, to see if I can make it print incorrectly But when I'm connected to the debugger, it never lost.

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 seems somewhat strange in the way I use NSDate and how the system clock works behind the scenes in airplane mode.

Thank,

Kevin

+3
source share
1 answer

You are probably using a format that does not take the time zone into account when saving the date.

NSLogateRec NSDate , NSDateFormatter? , NSDate + DST NSDate , NSDateFromatterMediumStyle, , ( datestyle, , , TZ DST!)

+1

All Articles