سؤال

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!

هل كانت مفيدة؟

المحلول

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]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top