1

我想在这里设置警报。我位于蒙特利尔,所以在 EST 时区。在我使用的代码中,我得到了当前日期,并尝试让它在几分钟后响起。该代码运行良好,警报按预期响起。

问题是:现在是上午 12.41。闹钟将在 12.43 响起。但是,在我的 NSLog 中,打印了时间:fireDate:2012-02-16 17:43:00 +0000

这不是一个主要问题,因为它可以工作,但你知道为什么它当时显示并且仍然有效吗?关于如何解决这个问题的任何想法?谢谢!

我基本上把时区放在任何地方,这是我正在使用的代码:

-(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]; 

}

谢谢!

4

1 回答 1

2

注意到最后的 +0000 了吗?那是时区。它告诉你它在格林威治标准时间 17:43 响起。首先,要设置您的时区,您需要使用“America/Montreal”(不是 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]);
于 2012-02-16T17:55:59.370 回答