UILocalNotification & NSTimeZone, mauvais fuseau horaire montrant
-
26-10-2019 - |
Question
Je tente ici de définir une alarme. Je suis situé à Montréal, donc dans l'EST Fuseau horaire. Dans le code, je me sers, je reçois la date actuelle et essayer de le faire sonner quelques minutes plus tard. Le code fonctionne parfaitement bien, et l'alarme sonne comme prévu.
Voici la question: Il est 00h41 en ce moment. l'alarme sonnera à 12,43. Cependant, dans mon NSLog, le temps est imprimé: fireDate: 2012-02-16 17:43:00 +0000
Il est pas un problème majeur car il fonctionne, mais aucune idée pourquoi il montre à ce moment-là et fonctionne encore? Toute idée sur la façon de résoudre ce problème? Merci!
Je mets essentiellement le fuseau horaire partout, voici le code que je utilise:
- (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];
}
Merci!
La solution
Notez que le +0000 à la fin il? C'est le fuseau horaire. Il vous dit qu'il a sonné à 17h43 GMT. Tout d'abord, pour définir votre fuseau horaire, vous devez utiliser « Amérique / Montréal » (non EST) ou l'utilisation timeZoneWithAbbreviation:
avec EST. Le dateComponent est configuré à l'heure que vous définissez et au fuseau horaire de votre calendrier. Voilà pourquoi la date est correcte, mais pas affiché dans la zone de moment. Pour changer cela, vous devez utiliser un NSDateFormatter
. Voir l'exemple ci-dessous comment!
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]);