我想在这里设置闹钟。我位于蒙特利尔,所以在EST时区。 在我使用的代码中,我得到当前日期,并在几分钟后尝试使其响起。代码工作得很好,并且闹铃按预期振铃。UILocalNotification&NSTimeZone,显示错误的时区
这是问题:现在是12点41分。闹铃将在12.43点响起。 然而,在我的NSLog,时间印:fireDate:2012-02-16十七时43分00秒+0000
这不是一个大问题,因为它的工作原理,但为什么它在显示任何想法那个时间,仍然有效?任何想法如何解决这个问题?谢谢!
我基本上把时区随处可见,这里是我使用的代码:
- (空)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];
}
谢谢!
哦,我不知道+0000,谢谢!但是,我添加了_dateComps.timeZone = [NSTimeZone timeZoneWithName:@“EST”]; _在dateComps init之后,我仍然获得** fireDate:2012-02-16 18:07:00 + 0000 ** – 2012-02-16 18:06:45
对不起,EST不是有效的名称。尝试“美国/蒙特利尔”(编辑回答)。您可以使用[NSTimeZone knownTimeZoneNames]获取有效名称的数组。 – MGA 2012-02-16 18:14:45
有用!我复制/粘贴使用[NSTimeZone knownTimeZoneNames]找到的那个,到处都是......但我奇怪地仍然有+0000。我在设置每个变量之前移动了setTimeZones,并且仍然可以得到它! – 2012-02-16 18:24:40