2
votes

I am trying to get my app to fire a notification based off a scheduled event. The date given is from a schedule that will take place in EST. The time given is Fri Feb 21 2014 12:00:00 -0500 (Noon EST).

When the button to schedule a notification is pressed, it runs this code:

NSString *message = [@"15 minutes until " stringByAppendingString:self.thetitle];
                NSLog(@"original%@", self.thedate);
        NSDate *newDate = [self.thedate dateByAddingTimeInterval:-60*15];
        NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];

        // [formatter3 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        [formatter3 setTimeStyle:NSDateFormatterShortStyle];
        [formatter3 setDateStyle:NSDateFormatterShortStyle];
        NSString *detailstext = [formatter3 stringFromDate:newDate];
        NSDate *othernewdate = [formatter3 dateFromString:detailstext];
        NSLog(@"other%@", othernewdate);
        UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.timeZone = [NSTimeZone systemTimeZone];
        notification.fireDate = othernewdate;
        notification.alertBody = message;
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.hasAction = YES;
        notification.alertAction = NSLocalizedString(@"View", @"View notification button");

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

Since the time being set on the notification fireDate is the othernewdate, I ran an NSLog in the console to see what it showed. It came back:

2014-02-21 16:45:00 +0000

This seems to be perfect, because that time in EST would be 11:45 AM, or 15 minutes before noon, which is what I wanted.

The issue is this: I am testing this in CST. I run the app, tell it to schedule a notification for this event, and then change my phone clock to be EST and it ends up firing the notification at 10:45.

There will be people using this app from different time zones, and then traveling to EST for when the conference that this app is for starts. I don't want them to schedule lots of notifications while in a different time zone and have it fire at the wrong time. Any suggestions?

1
Use a NSDateFormatter when converting between strings and dates. Also set the timezone in the formatter. NSLog of a NSDate is probably not providing the string you want/expect, use a NSDateFormatter in NSLog. NSDate is based on GMT internally, the timezone just effects string to date, date to string and date component operations. - zaph
@Zaph Ok, I have tried doing this adding in a line ~[formatter3 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];~ but it stays the same. - user717452

1 Answers

0
votes

If you want the notification to fire at an absolute date/time, independent of the time zone, then you must set notification.timezone = nil.

From the documentation:

"The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock."