了解UILocalNotification时区 [英] Understanding UILocalNotification timeZone

查看:180
本文介绍了了解UILocalNotification时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文档中:


fireDate中指定的日期是根据此属性的值
进行解释的。如果您指定nil(默认值),则日期消息为
,解释为绝对的GMT时间,适用于
作为倒计时计时器的情况。如果您将一个有效的NSTimeZone对象分配给此
属性,则日期会被解释为在时区有变化时自动调整
的挂钟时间;一个适用于这种情况的
示例是一个闹钟。

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.

假设我安排了本地通知(绝对时间)明天中午GMT我在西雅图下午1点(太平洋时间,GMT-8)安排这件事,然后立即前往芝加哥11点(中部时间,格林威治标准时间-6)。我的设备可以从太平洋时间到中部时间进行调整。在以下三种情况下,请告知我何时通知:

Suppose I schedule an local notification for (in absolute time) noon tomorrow GMT. I schedule this in Seattle at 1PM (Pacific Time, GMT-8), then immediately travel to Chicago arriving 11PM (Central Time, GMT-6). My device adjust from Pacific Time to Central Time. Please help me understand when my notification will occur in the following three cases:


  1. UILocalNotification timeZone设置为nil。

  2. UILocalNotification timeZone设置为GMT。

  3. UILocalNotification timeZone设置为太平洋时间。


推荐答案

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

I just ran some tests on iOS 6.1.3. Here's what I got:

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

I'm in Seattle, at 1:00PM (Pacific Daylight Time, GMT-7). I created a 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)

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

Then I created and scheduled the notifications:

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)刚刚创建的通知的日志: p>

Logs of the notifications just created, in Seattle (Pacific Daylight Time, 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)。

I changed the phone's timezone to Chicago, where now it is 3:00PM (Central Daylight Time, GMT-5).

通知日志,在芝加哥(中央大街光线时间GMT-5)

Logs of the notifications, in Chicago (Central Daylight Time, 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 为零,火灾日期是及时固定的。这意味着通知将在格林尼治标准时间GMT-7 12:00,格林尼治标准时间GMT-5或格林尼治标准时间7:00下午12:00发射。

  2. 当UILocalNotification timeZone 设置为GMT,火灾日期是为GMT时间计算的,如果用户进入另一个时区,将自动更新。在这个例子中,格林尼治标准时间GMT + 7的时间转换为格林尼治标准时间19:00,通知设置为当地时间19:00,无论我们是什么时区(格林尼治标准时间19:00,格林尼治标准时间-5分钟19时,或

  3. 当UILocalNotification timeZone 设置为本地时区(太平洋夏令时GMT-7 ),为当地时间计算火灾日期,如果用户进入另一个时区,将自动更新。在这个例子中,时间是GMT GMT-7的12:00,所以通知将在当地时间12:00发射,无论我们是什么时区(GMT GMT,12:00 GMT GMT-5或12:00 GMT- 7)。

  1. When UILocalNotification timeZone is nil, the fire date is fixed in time. That means the notification will be fired at 12:00PM GMT-7, 2:00PM GMT-5, or 7:00 GMT.
  2. When UILocalNotification timeZone is set to GMT, the fire date is calculated for GMT time and will auto-update if the user goes to another time zone. In this example, the time 12:00 GMT-7 was converted to 19:00 GMT, and the notification was set to 19:00 local time, whatever time zone we are (19:00 GMT, 19:00 GMT-5 or 19:00 GMT-7).
  3. When UILocalNotification timeZone is set to the local time zone (Pacific Daylight Time, GMT-7), the fire date is calculated for the local time and will auto-update if the user goes to another time zone. In this example, the time was 12:00 GMT-7, so the notification will be fired at 12:00 local time, whatever time zone we are (12:00 GMT, 12:00 GMT-5 or 12:00 GMT-7).

这篇关于了解UILocalNotification时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆