我刚刚在 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
结论:
- 当 UILocalNotification
timeZone
为 nil 时,触发日期在时间上是固定的。这意味着通知将在格林威治标准时间 7 点下午 12:00、格林威治标准时间 5 点下午 2:00 或格林威治标准时间 7:00 触发。
- 当 UILocalNotification
timeZone
设置为 GMT 时,触发日期以 GMT 时间计算,并且如果用户转到另一个时区,将自动更新。在此示例中,时间 12:00 GMT-7 转换为 19:00 GMT,并且通知设置为本地时间 19:00,无论我们在哪个时区(格林威治标准时间 19:00、格林威治标准时间 19:00 或19:00 GMT-7)。
- 当 UILocalNotification
timeZone
设置为本地时区(太平洋夏令时间,GMT-7)时,触发日期以本地时间计算,如果用户转到另一个时区,将自动更新。在此示例中,时间为 12:00 GMT-7,因此无论我们在哪个时区(格林威治标准时间 12:00、格林威治标准时间 12:00 或格林威治标准时间 12:00),通知都将在当地时间 12:00 触发7)。