2013 年 7 月 8 日编辑:Apple 有一组出色的 WWDC 视频,它们真正帮助我理解了 Objective-C 中的各种日期和时间类,以及如何正确地执行时间计算/操作。
“常见日期和时间挑战的解决方案”(高清视频、标清视频、幻灯片(PDF))(WWDC 2013)
“执行日历计算”(标清视频、幻灯片(PDF))(WWDC 2011)
注意:链接需要免费的 Apple Developer 会员资格。
在这个 SO question中,我问我如何计算某个日期和时间(“下周日下午 5 点”)。感谢我得到的答案,我想出了以下代码:
- (NSDate *) toPacificTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
NSInteger seconds = [tz secondsFromGMTForDate: self];
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
}
- (void)handleLiveShowReminders
{
NSDate *gmtNow = [NSDate date];
NSDate *now = [gmtNow toPacificTime];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:now];
NSInteger weekday = [dateComponents weekday];
NSInteger daysTillNextSunday = 8 - weekday;
int secondsInDay = 86400; // 24 * 60 * 60
NSDate *nextSunday = [now dateByAddingTimeInterval:secondsInDay * daysTillNextSunday];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:nextSunday];
[components setHour:17];
[components setMinute:00];
[components setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
NSDate *nextSunday5PM = [calendar dateFromComponents:components];
warningInterval = -300; // we want the notification to fire 5 minutes beforehand
NSDate *alertDate = [nextSunday5PM dateByAddingTimeInterval:(NSTimeInterval)warningInterval];
UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertDate;
notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
notifyAlarm.repeatInterval = NSWeekCalendarUnit;
notifyAlarm.soundName = @"alert.aif";
notifyAlarm.alertBody = @"LIVE SHOW REMINDER: The live show is about to start!";
[[UIApplication sharedApplication] scheduleLocalNotification:notifyAlarm];
}
}
问题是,虽然这段代码对我有用,但它不适用于不在 PST 时区的任何人,正如我从 EST 的用户那里收到的调试输出所示:
number of notifications = 1
Notification #1
===================
Body: LIVE SHOW REMINDER: The live show is about to start!
Details: <UIConcreteLocalNotification: 0x1d5f2200>
{fire date = Sunday, December 9, 2012, 4:30:00 PM Pacific Standard Time,
time zone = America/Los_Angeles (PST) offset -28800,
repeat interval = NSWeekCalendarUnit
repeat count = UILocalNotificationInfiniteRepeatCount,
next fire date = Sunday, December 9, 2012, 4:30:00 PM Eastern Standard Time,
user info = (null)}
我在这里做错了什么?