1

这应该很简单,但事实证明对我来说具有挑战性。我想知道计算[NSDate date]与未来事件 x 秒之间的秒数差异的最佳方法。有几种不同类型的事件,每个事件可能一天发生几次,并且在不同的时间发生,这取决于它恰好是一周中的哪一天。

我想做的是让用户从选择器中选择一个事件类型,然后根据他们的选择在通知中心为该事件的下一次发生设置警报。除了秒计算外,我一切正常。

因此,例如,假设现在是星期一上午 9 点。我想确定从现在到用户选择的事件之间的秒数,该事件在每周二、周四和周六每天上午 10 点、下午 4 点和晚上 11 点或周日下午 1 点定期发生。您将如何最有效地处理这个问题?

4

1 回答 1

1

当您谈论诸如“下周四下午 1 点”之类的时间或日期时,这些信息仅在日历上下文中才有意义。NSDate不会为您提供太多帮助。或许更恰当地命名为NSPointInTime. 这只是从某个较早的任意参考时间点过去的几秒钟。它没有工作日、午前/午后甚至一天中的小时的概念。

知道这类事情的两个对象是NSDateComponentsNSCalendar。他们一起工作,可以NSDate从“下周四下午 1 点”之类的规范创建一个。

您可以使用 将任何日期分解为组件-[NSCalendar components:fromDate:],然后可以使用其他NSDateComponents对象对各个信息片段执行算术运算。例如,找出今天的工作日及其与星期四的差异。然后用于-[NSCalendar dateByAddingComponents:toDate:options:]根据该偏移量创建一个新日期。

@interface NSCalendar (NextWeekday)

- (NSInteger)maxWeekday;

- (NSDate *)dateFromComponents:(NSDateComponents *)comps
                forNextWeekday:(NSInteger)weekday
                        atHour:(NSInteger)hour;

@end

@implementation NSCalendar (NextWeekday)

- (NSInteger)maxWeekday
{
    return [self maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
}

- (NSDate *)dateFromComponents:(NSDateComponents *)comps
                forNextWeekday:(NSInteger)weekday
                        atHour:(NSInteger)hour
{
    NSInteger diff = weekday - [comps weekday];
    if( diff < 0 ){
        diff += [self maxWeekday];
    }

    NSDateComponents * weekdayOffset = [NSDateComponents new];
    [weekdayOffset setWeekday:diff];

    [comps setHour:hour];

    return [self dateByAddingComponents:weekdayOffset
                                 toDate:[self dateFromComponents:comps]
                                options:0];
}


@end

#define GREGORIAN_THURSDAY 5

int main(int argc, const char * argv[])
{

    @autoreleasepool {

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

        NSDateComponents * wednesday = [NSDateComponents new];
        [wednesday setDay:3];
        [wednesday setWeekday:4];
        [wednesday setMonth:6];
        [wednesday setYear:2013];

        NSDateComponents * friday = [NSDateComponents new];
        [friday setDay:5];
        [friday setWeekday:6];
        [friday setMonth:6];
        [friday setYear:2013];

        NSDateComponents * now = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
                                        fromDate:[NSDate date]];


        NSDateComponents * lastSatOfDecember = [NSDateComponents new];
        [lastSatOfDecember setDay:29];
        [lastSatOfDecember setWeekday:7];
        [lastSatOfDecember setMonth:12];
        [lastSatOfDecember setYear:2012];

        NSLog(@"From Wednesday: %@", [cal dateFromComponents:wednesday
                                              forNextWeekday:GREGORIAN_THURSDAY
                                                      atHour:13]);
        NSLog(@"From Friday: %@", [cal dateFromComponents:friday
                                           forNextWeekday:GREGORIAN_THURSDAY
                                                   atHour:13]);
        NSLog(@"From now: %@", [cal dateFromComponents:now
                                        forNextWeekday:GREGORIAN_THURSDAY
                                                atHour:13]);
        NSLog(@"Crossing over the year: %@", [cal dateFromComponents:lastSatOfDecember
                                                     forNextWeekday:GREGORIAN_THURSDAY
                                                             atHour:13]);

    }
    return 0;
}
于 2013-07-01T19:46:56.837 回答