1

I am trying here to set an alarm. I am located in Montréal, so in the EST Timezone. In the code I am using, I get the current date, and try to make it ring some minutes later. The code works perfectly fine, and the alarm rings as expected.

Here is the issue : It's 12.41 am right now. the alarm will ring at 12.43. However, in my NSLog, the time is printed : fireDate : 2012-02-16 17:43:00 +0000

It's not a major problem since it works, but any idea on why it is showing at that time and still works? Any idea on how to fix that? Thanks!

I basically put the timezone everywhere, here is the code I am using :

-(void)scheduleNotificationWithInterval:(int)minutesBefore {

// Current date
NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSTimeZoneCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone* zone = [NSTimeZone timeZoneWithName:@"EST"];
[calendar setTimeZone:zone];
NSDateComponents *components = [calendar components:units fromDate:now];
[components setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];

NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:year];
[dateComps setMonth:month];
[dateComps setDay:day];
[dateComps setHour:hour];
[dateComps setMinute:minute+2]; // Temporary
NSDate *itemDate = [calendar dateFromComponents:dateComps];

NSLog(@"fireDate : %@", itemDate);

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = itemDate;
//localNotif.timeZone = zone;
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"EST"];

minutesBefore = 15; // Temporary
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
                        @"Blabla", minutesBefore];
localNotif.alertAction = NSLocalizedString(@"See Foo", nil);

localNotif.soundName = UILocalNotificationDefaultSoundName;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"LastCall" forKey:@"lastcall"];
localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

}

Thanks!

1 Answers1

2

Notice the +0000 at the end there? That is the time zone. It's telling you that it rang at 17:43 GMT. First, to set your time zone, you need to use "America/Montreal" (not EST) or use timeZoneWithAbbreviation: with EST. The dateComponent is configured at whatever time you set and at the timezone of your calendar. That's why the date is correct, just not displayed at the right time zone. To change that you need to use an NSDateFormatter. See the example below for how!

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone* zone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
[calendar setTimeZone:zone];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:16];
[dateComps setYear:2001];
[dateComps setMinute:30];
NSDate *itemDate = [calendar dateFromComponents:dateComps];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];

NSLog(@"fireDate : %@", [formatter stringFromDate:itemDate]);
MGA
  • 3,711
  • 1
  • 21
  • 22
  • Oh I didn't know for the +0000, thanks! However, I added _dateComps.timeZone = [NSTimeZone timeZoneWithName:@"EST"];_ right after the dateComps init and I still get **fireDate : 2012-02-16 18:07:00 +0000** – Séraphin Hochart Feb 16 '12 at 18:06
  • Sorry, EST is not a valid name. Try "America/Montreal" (edited in answer). You can get an array of the valid names using [NSTimeZone knownTimeZoneNames]. – MGA Feb 16 '12 at 18:14
  • Useful! I copy/pasted the one found using [NSTimeZone knownTimeZoneNames], everywhere... But I weirdly still have +0000. I moved the setTimeZones before setting every variable and still get that! – Séraphin Hochart Feb 16 '12 at 18:24
  • I use a little hack to change the current GMT position with an interval, which works, but it's still a "hack", thanks! – Séraphin Hochart Feb 16 '12 at 18:39
  • You don't actually need to set the timezone anywhere but the calendar. The date will use the one you give to your calendar. You need to use an NSDateFormatter however to display the date in the timezone you want. See edit – MGA Feb 16 '12 at 19:02