2012-02-16 57 views
1

我想在这里设置闹钟。我位于蒙特利尔,所以在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]; 

}

谢谢!

回答

2

注意+0000在那里?那是时区。它告诉你,格林威治标准时间17:43它响了。首先,要设置您的时区,您需要使用“美国/蒙特利尔”(而不是EST)或使用timeZoneWithAbbreviation:与EST。 dateComponent在您设置的任何时间和日历的时区进行配置。这就是为什么日期是正确的,只是没有显示在正确的时区。要改变你需要使用NSDateFormatter。请参阅下面的示例以了解如何!

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]); 
+0

哦,我不知道+0000,谢谢!但是,我添加了_dateComps.timeZone = [NSTimeZone timeZoneWithName:@“EST”]; _在dateComps init之后,我仍然获得** fireDate:2012-02-16 18:07:00 + 0000 ** – 2012-02-16 18:06:45

+0

对不起,EST不是有效的名称。尝试“美国/蒙特利尔”(编辑回答)。您可以使用[NSTimeZone knownTimeZoneNames]获取有效名称的数组。 – MGA 2012-02-16 18:14:45

+0

有用!我复制/粘贴使用[NSTimeZone knownTimeZoneNames]找到的那个,到处都是......但我奇怪地仍然有+0000。我在设置每个变量之前移动了setTimeZones,并且仍然可以得到它! – 2012-02-16 18:24:40