ios - 了解 UILocalNotification 时区

标签 ios datetime uilocalnotification

来自文档:

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

假设我在格林威治标准时间明天中午(绝对时间)安排本地通知。我安排在西雅图时间下午 1 点(太平洋时间,GMT-8),然后立即前往芝加哥,晚上 11 点(中部时间,GMT-6)抵达。我的设备从太平洋时间调整为中部时间。请帮助我了解在以下三种情况下我的通知何时发生:

  1. UILocalNotification timeZone 设置为 nil。
  2. UILocalNotification timeZone 设置为 GMT。
  3. UILocalNotification 时区设置为太平洋时间。

最佳答案

我刚刚在 iOS 6.1.3 上运行了一些测试。这是我得到的:

我在西雅图,下午 1:00(太平洋夏令时间,GMT-7)。我创建了一个 NSDate:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
// 2013-08-31 @ 12:00:00 (noon)
dateComponents.year = 2013;
dateComponents.month = 8;
dateComponents.day = 31;
dateComponents.hour = 12;
dateComponents.minute = 0;
dateComponents.second = 0;

NSDate *fireDate = [gregorianCalendar dateFromComponents:dateComponents];

现在我有

fireDate = 2013-08-31 19:00:00 +0000 (2013-08-31 12:00:00 -0700)

然后我创建并安排了通知:

notification1 = [[UILocalNotification alloc] init];
notification1.fireDate = fireDate;
// notification1.timeZone is nil by default

NSLog(@"%@", notification1);

notification2 = [[UILocalNotification alloc] init];
notification2.fireDate = fireDate;
notification2.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

NSLog(@"%@", notification2);

notification3 = [[UILocalNotification alloc] init];
notification3.fireDate = fireDate;
notification3.timeZone = [NSTimeZone defaultTimeZone];

NSLog(@"%@", notification3);

刚刚在西雅图(太平洋夏令时间,GMT-7)创建的通知日志:

notification1:
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time,
time zone = (null),
next fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time

notification2:
fire date = Saturday, August 31, 2013, 7:00:00 PM GMT,
time zone = GMT (GMT) offset 0,
next fire date = Saturday, August 31, 2013, 7:00:00 PM Pacific Daylight Time

notification3:
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time,
time zone = US/Pacific (PDT) offset -25200 (Daylight),
next fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time

我将手机的时区更改为芝加哥,现在是下午 3:00(中部夏令时,GMT-5)。

芝加哥的通知日志(中部夏令时,GMT-5)

notification1:
fire date = Saturday, August 31, 2013, 2:00:00 PM Central Daylight Time,
time zone = (null),
next fire date = Saturday, August 31, 2013, 2:00:00 PM Central Daylight Time

notification2:
fire date = Saturday, August 31, 2013, 7:00:00 PM GMT,
time zone = GMT (GMT) offset 0,
next fire date = Saturday, August 31, 2013, 7:00:00 PM Central Daylight Time

notification3:
fire date = Saturday, August 31, 2013, 12:00:00 PM Pacific Daylight Time,
time zone = US/Pacific (PDT) offset -25200 (Daylight),
next fire date = Saturday, August 31, 2013, 12:00:00 PM Central Daylight Time

结论:

  1. 当 UILocalNotification timeZone 为 nil 时,触发日期是固定的。这意味着通知将在 GMT-7 中午 12:00、GMT-5 中午 2:00 或 GMT 7:00 触发。
  2. 当 UILocalNotification timeZone 设置为 GMT 时,触发日期按 GMT 时间计算,如果用户转到另一个时区,将自动更新。在此示例中,时间 12:00 GMT-7 被转换为 19:00 GMT,并且通知设置为本地时间 19:00,无论我们处于什么时区(19:00 GMT、19:00 GMT-5 或19:00 GMT-7)。
  3. 当 UILocalNotification timeZone 设置为本地时区(太平洋夏令时,GMT-7)时,触发日期按本地时间计算,如果用户转到另一个时区将自动更新时区。在这个例子中,时间是 12:00 GMT-7,所以通知将在本地时间 12:00 触发,无论我们处于什么时区(12:00 GMT、12:00 GMT-5 或 12:00 GMT- 7).

关于ios - 了解 UILocalNotification 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18424569/

相关文章:

c++ - -fvisibility=hidden 编译器未通过调试版本

python - 转换为 'datetime' 类型 : "hour must be in 0..23" 时出现问题

ios - 如何检查我们的应用程序的通知警报样式?

ios - 如何在 iOS 10 中替换折旧的 UILocalNotification 并添加 UNNotificationAction

ios - 崩溃 viewDidMoveToWindow :shouldAppearOrDisappear: on iOS 10

ios - UISwipeGestureRecognizer 只有一个方向工作

ios - 使用 Swift 在 iOS 中未触摸/触摸时调整屏幕亮度

python - Pandas 按具有重复日期时间的组重新采样

mysql - Perl 与 MySQL 比较日期

iOS本地同时通知但不同日期有不同的提醒消息