【问题标题】:UILocalNotification & NSTimeZone, wrong timezone showing upUILocalNotification 和 NSTimeZone,出现错误的时区
【发布时间】:2012-02-16 17:46:35
【问题描述】:

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

}

谢谢!

【问题讨论】:

    标签: objective-c nsdate uilocalnotification nstimezone


    【解决方案1】:

    注意到末尾的 +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]);
    

    【讨论】:

    • 哦,我不知道 +0000,谢谢!但是,我在 dateComps 初始化之后添加了 dateComps.timeZone = [NSTimeZone timeZoneWithName:@"EST"]; ,我仍然得到 fireDate : 2012-02-16 18:07:00 + 0000
    • 抱歉,EST 不是有效名称。尝试“美国/蒙特利尔”(在答案中编辑)。您可以使用 [NSTimeZone knownTimeZoneNames] 获取有效名称的数组。
    • 有用!我复制/粘贴了使用 [NSTimeZone knownTimeZoneNames] 找到的那个,无处不在......但奇怪的是我仍然有 +0000。我在设置每个变量之前移动了 setTimeZones 并且仍然得到它!
    • 我使用了一个小技巧来改变当前的 GMT 位置,这很有效,但它仍然是一个“hack”,谢谢!
    • 您实际上不需要在除日历之外的任何地方设置时区。日期将使用您提供给日历的日期。但是,您需要使用 NSDateFormatter 来显示所需时区的日期。见编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多