1

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)}

我在这里做错了什么?

4

2 回答 2

2

您的逻辑有一个问题,只是将 86,400 添加到您的时间间隔可能不是您想要的。例如,如果用户体验到夏令时,那么如果它发生在午夜附近,您将休息一个小时,甚至一天。更好的方法是向 NSCalendar 询问结果,其中考虑了用户本地时区。

NSDate *start = yourDate;
NSDateComponents *oneDay = [NSDateComponents new];
[oneDay setDay:1];

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *end = [cal dateByAddingComponents:oneDay toDate:start options:0];
于 2013-01-16T20:51:05.520 回答
0

NSDate 与 UTC/GMT 相关。当您“调整”太平洋时间时,您会造成完全的混乱。

于 2012-12-03T22:49:18.170 回答