2

我无法理解时区,似乎无法解决它。

用户应输入警报响起的时间。因此,他们在澳大利亚悉尼时区选择周日早上 6:00(使用 pickerView)。

然后,当他们的设备将时区更改为美国洛杉矶时,警报仍应在洛杉矶时间早上 6:00 响起(现在是悉尼时间凌晨 1:00 或其他时间)。

设置通知:

UILocalNotification *localNotification;
[localNotification setTimeZone:[NSTimeZone localTimeZone]];

读取通知以显示在 TableView 中:

NSDate *fd = <the firedate of the scheduled notification>
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
//[df_local setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[df_local setTimeZone:[NSTimeZone localTimeZone]];
[df_local setDateFormat:@"hh:mm"];
NSString* ts_local_string = [df_local stringFromDate:fd];

使用该代码,我将悉尼的时间设置为上午 6:00,但在洛杉矶查看时,时间现在是下午 1:00。

在设置 UILocalNotification 时我应该将时区设置为什么,以及在阅读通知时我应该将时区设置为什么,以便在悉尼和洛杉矶查看时,时间都是当地时间上午 6:00。

谢谢

4

1 回答 1

0

我想我已经设法弄清楚了。

UILocalNotification *localNotification;
[localNotification setTimeZone:[NSTimeZone localTimeZone]];
    [localNotification setFireDate:fireDate];

这将使 fireDate 中的任何日期成为通知将在用户当前时区中关闭的时间/日期。所以它会在悉尼早上 6:00 起飞,然后在洛杉矶早上 6:00 再次起飞,等等。

所以这部分按预期工作,我犯的错误是显示时间。

我需要将 NSDateFormatter 的时区设置为 UILocalNotification 的时区,而不是我的问题中的代码:

UILocalNotification *myLN = <The UILocalNotification we've retrieved from somewhere>;
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:myLN.timeZone];
[df_local setDateFormat:@"hh:mm"];
NSString* ts_local_string = [df_local stringFromDate:fd];
于 2013-12-09T05:39:13.727 回答