Uilocalnotificación y nstimezone, zona horaria incorrecta que aparece
-
26-10-2019 - |
Pregunta
Estoy intentando aquí establecer una alarma. Estoy ubicado en Montreal, así que en la zona horaria de EST. En el código que estoy usando, obtengo la fecha actual e intento hacer que suene unos minutos después. El código funciona perfectamente bien, y la alarma suena como se esperaba.
Aquí está el problema: son las 12.41 de la mañana en este momento. La alarma sonará a las 12.43. Sin embargo, en mi NSLOG, se imprime el tiempo: Firedate: 2012-02-16 17:43:00 +0000
No es un problema importante ya que funciona, pero ¿alguna idea de por qué se muestra en ese momento y todavía funciona? ¿Alguna idea sobre cómo solucionar eso? ¡Gracias!
Básicamente pongo la zona horaria en todas partes, aquí está el código que estoy usando:
-(nulo) SchedulenotificationWithInterval: (int) minutos antes {
// 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];
}
¡Gracias!
Solución
¿Observe el +0000 al final allí? Esa es la zona horaria. Te está diciendo que sonó a las 17:43 GMT. Primero, para establecer su zona horaria, debe usar "América/Montreal" (no EST) o usar timeZoneWithAbbreviation:
con est. Datecomponent está configurado en cualquier momento que establezca y en la zona horaria de su calendario. Es por eso que la fecha es correcta, simplemente no se muestra en la zona horaria correcta. Para cambiar que necesitas usar un NSDateFormatter
. ¡Vea el ejemplo a continuación de cómo!
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]);