UILocalNotification & NSTimeZone,显示错误的时区

发布于 2025-01-06 02:59:22 字数 2079 浏览 1 评论 0原文

我正在尝试在这里设置闹钟。我位于蒙特利尔,所以处于东部时区。 在我使用的代码中,我获取当前日期,并尝试让它在几分钟后响起。 代码运行得很好,警报按预期响起。

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

这不是一个主要问题,因为它有效,但任何关于它为何显示的想法那个时候还有效吗?关于如何解决这个问题有什么想法吗?谢谢!

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

-(void)scheduleNotificationWithInterval:(int)分钟之前{

// 当前日期
NSDate *now = [NSDate 日期];
// 指定我们要使用的单位
无符号单位 = NSTimeZoneCalendarUnit | NSYearCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

NSCalendar *日历 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone* 区域 = [NSTimeZone timeZoneWithName:@"EST"];
[日历 setTimeZone:zone];
NSDateComponents *components = [日历组件:单位 fromDate:now];
[组件 setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];

NSInteger 年份 = [组件年份];
NSInteger 月份 = [组件月份];
NSInteger day = [组件日];
NSInteger hour = [组件小时];
NSInteger 分钟 = [组件分钟];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:year];
[dateComps setMonth:month];
[dateComps setDay:day];
[dateComps setHour:小时];
[dateComps setMinute:分钟+2]; // 暂时的
NSDate *itemDate = [日历 dateFromComponents:dateComps];

NSLog(@"fireDate : %@", itemDate);

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    返回;
localNotif.fireDate = itemDate;
//localNotif.timeZone = 区域;
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"EST"];

分钟前 = 15; // 暂时的
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i 分钟。", nil),
                        @“Blabla”,分钟之前];
localNotif.alertAction = NSLocalizedString(@"See Foo", nil);

localNotif.soundName = UILocalNotificationDefaultSoundName;

NSDictionary *infoDict = [NSDictionary DictionaryWithObject:@"LastCall" forKey:@"lastcall"];
localNotif.userInfo = infoDict;

[[UIApplication共享应用]scheduleLocalNotification:localNotif]; 

}

谢谢!

I am trying here to set an alarm. I am located in Montréal, so in the EST Timezone.
In the code I am using, I get the current date, and try to make it ring some minutes later.
The code works perfectly fine, and the alarm rings as expected.

Here is the issue : It's 12.41 am right now. the alarm will ring at 12.43.
However, in my NSLog, the time is printed : fireDate : 2012-02-16 17:43:00 +0000

It's not a major problem since it works, but any idea on why it is showing at that time and still works? Any idea on how to fix that? Thanks!

I basically put the timezone everywhere, here is the code I am using :

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

}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-01-13 02:59:22

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

Notice the +0000 at the end there? That is the time zone. It's telling you that it rang at 17:43 GMT. First, to set your time zone, you need to use "America/Montreal" (not EST) or use timeZoneWithAbbreviation: with EST. The dateComponent is configured at whatever time you set and at the timezone of your calendar. That's why the date is correct, just not displayed at the right time zone. To change that you need to use an NSDateFormatter. See the example below for how!

 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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文