如何查找 NSDate 是否在指定范围内?

发布于 2024-10-31 01:27:08 字数 1439 浏览 0 评论 0原文

因此,我很难(没有双关语)查找 NSDate 是否在特定范围内。考虑一下:安排在下午 5:00 到下午 6:00 之间进行访问。我想知道预定的访问是否在当前时间的 +/- 15 分钟内。因此,我的安全范围是下午 4:45 到下午 6:15。我怎样才能知道预定的访问是否在范围之内或之外?这是我到目前为止得到的代码,它根本不起作用......

- (BOOL)isInScheduledRange {
//    NSLog(@"Start: %f", [self.start timeIntervalSinceNow]);
//    NSLog(@"End: %f", [self.end timeIntervalSinceNow]);
//    
//    if (([self.start timeIntervalSinceNow] >= -900) && ([self.end timeIntervalSinceNow] <= 3600)) {
//        NSLog(@"In Range");
//        
//        return YES;
//    }
//    
    NSDate *now = [[NSDate alloc] init];

//    if ((([self.start timeIntervalSinceDate:now] >= -900) && ([self.start timeIntervalSinceDate:now] <= 900)) && (([self.end timeIntervalSinceDate:now] <= -900) && ([self.end timeIntervalSinceDate:now] >= 900))) {
//        NSLog(@"In Range");
//    }

    NSLog(@"%@; %@; %@", now, self.start, self.end);
//    NSLog(@"%@; %@", [self.start timeIntervalSinceDate:now], [self.end timeIntervalSinceDate:now]);

    if ((([self.start timeIntervalSinceDate:now] >= -900) && ([self.start timeIntervalSinceDate:now] <= 0)) && (([self.end timeIntervalSinceDate:now] >= 0) && ([self.end timeIntervalSinceDate:now] <= 900))) {
        NSLog(@"In Range");
    }

    return NO;

    [now release];
}

我希望得到一些帮助。我在计算时间时遇到困难。

另一方面,无论我在什么平台上工作,我都讨厌处理时间......

So, I'm having a difficult time (no pun intended) finding if an NSDate is within a specific range. Consider this: a scheduled visit between 5:00 PM and 6:00 PM. I want to find if the scheduled visit is within +/- 15 minutes of the current time. Thus my safe range is 4:45 PM to 6:15 PM. How can I find out if the scheduled visit is in or out of the range? Here's the code I've got so far, which doesn't work at all...

- (BOOL)isInScheduledRange {
//    NSLog(@"Start: %f", [self.start timeIntervalSinceNow]);
//    NSLog(@"End: %f", [self.end timeIntervalSinceNow]);
//    
//    if (([self.start timeIntervalSinceNow] >= -900) && ([self.end timeIntervalSinceNow] <= 3600)) {
//        NSLog(@"In Range");
//        
//        return YES;
//    }
//    
    NSDate *now = [[NSDate alloc] init];

//    if ((([self.start timeIntervalSinceDate:now] >= -900) && ([self.start timeIntervalSinceDate:now] <= 900)) && (([self.end timeIntervalSinceDate:now] <= -900) && ([self.end timeIntervalSinceDate:now] >= 900))) {
//        NSLog(@"In Range");
//    }

    NSLog(@"%@; %@; %@", now, self.start, self.end);
//    NSLog(@"%@; %@", [self.start timeIntervalSinceDate:now], [self.end timeIntervalSinceDate:now]);

    if ((([self.start timeIntervalSinceDate:now] >= -900) && ([self.start timeIntervalSinceDate:now] <= 0)) && (([self.end timeIntervalSinceDate:now] >= 0) && ([self.end timeIntervalSinceDate:now] <= 900))) {
        NSLog(@"In Range");
    }

    return NO;

    [now release];
}

I'd appreciate some help. I'm having difficulties with this time calculation.

On a separate note, I hate dealing with time no matter what platform I'm working with...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光①夏 2024-11-07 01:27:08

您想要检查开始时间是否小于当前时间之后 900 秒,以及结束时间是否小于当前时间之前 900 秒。 timeIntervalSinceDate:如果调用的对象位于参数之后,则返回正数,您需要检查 start <= 900 和 end >= −900。此外,您还可以使用 timeIntervalSinceNow 来简化代码,而不是手动获取当前日期并将其传递给 timeIntervalSinceDate:。最后,如果您可以假设(或之前确保)开始在结束之前,那么您不需要两个中间测试,因为它们都必须为真,其他两个测试才为真。

if([self.start timeIntervalSinceNow] <= 900 && [self.end timeIntervalSinceNow] >= −900) {
    NSLog(@"In range")
}

You want to check if the start time is less than 900 seconds after the current time, and the end time is less than 900 seconds before the current time. timeIntervalSinceDate: returns a positive number if the object it is called on is after the argument, you need to check for start <= 900 and end >= −900. Also, you could simplify your code by using timeIntervalSinceNow instead of manually getting the current date and passing it to timeIntervalSinceDate:. Finally, if you can assume (or previously ensured) that the start is before the end, then you don't need the two middle tests, as they will both have to be true for both of the others to be true.

if([self.start timeIntervalSinceNow] <= 900 && [self.end timeIntervalSinceNow] >= −900) {
    NSLog(@"In range")
}
内心荒芜 2024-11-07 01:27:08

这是一些冗长(而且愚蠢)的代码,解释了我解决这个问题的方法

NSTimeInterval secondsBeforeStart = [self.start timeIntervalSinceNow];
if ( secondsBeforeStart > (15 * 60))
{
    // The result for '[self.start timeIntervalSinceNow]' is positive as long
    //  as 'self.start' remains in the future. We're not in range until there
    //  are 900 seconds to go or less.
    NSLog( @"Still time to chill.");

    // More than fifteen minutes to go so return NO.
    return NO;
}
else
{
    NSLog( @"OMG did I miss it!!!");
    NSTimeInterval secondsBeforeEnd = [self.end timeIntervalSinceNow];
    if ( secondsBeforeEnd < -( 15 * 60))
    {
        // The result for '[self.end timeIntervalSinceNow]' is negative when
        //  'self.end' is in the past. 
        // It's been more than 900 seconds since the end of the appointment
        //  so we've missed it.
        NSLog( @"Ahhhhh!!!");

        // More than 900 seconds past the end of the event so return NO.
        return NO;
    }
    else
    {
        // We are somewhere between 900 seconds before the start and
        //  900 seconds before the end so we are golden.
        NSLog( @"Whew, we made it.");
        return YES;
    }
}

我编码的方式是

BOOL inRange = NO;  // Assume we are not in the proper time range.

if ( [self.start timeIntervalSinceNow] <= ( 15 * 60))
{
    // 'Now' is at least 900 seconds before the start time but could be later.
    if ( [self.end timeIntervalSinceNow] >= -( 15 * 60))
    {
        // 'Now' is not yet 900 seconds past the end time.
        inRange = YES
    }
}

return inRange;

注意:我实际上没有编译这个,但我很确定逻辑是正确的。

最后,您确实注意到最后两行

    return NO;

    [now release];
}

会造成内存泄漏。 (释放然后返回;^))

Here's some verbose (and silly) code that explains my approach to this problem

NSTimeInterval secondsBeforeStart = [self.start timeIntervalSinceNow];
if ( secondsBeforeStart > (15 * 60))
{
    // The result for '[self.start timeIntervalSinceNow]' is positive as long
    //  as 'self.start' remains in the future. We're not in range until there
    //  are 900 seconds to go or less.
    NSLog( @"Still time to chill.");

    // More than fifteen minutes to go so return NO.
    return NO;
}
else
{
    NSLog( @"OMG did I miss it!!!");
    NSTimeInterval secondsBeforeEnd = [self.end timeIntervalSinceNow];
    if ( secondsBeforeEnd < -( 15 * 60))
    {
        // The result for '[self.end timeIntervalSinceNow]' is negative when
        //  'self.end' is in the past. 
        // It's been more than 900 seconds since the end of the appointment
        //  so we've missed it.
        NSLog( @"Ahhhhh!!!");

        // More than 900 seconds past the end of the event so return NO.
        return NO;
    }
    else
    {
        // We are somewhere between 900 seconds before the start and
        //  900 seconds before the end so we are golden.
        NSLog( @"Whew, we made it.");
        return YES;
    }
}

The way I would have coded it would have been

BOOL inRange = NO;  // Assume we are not in the proper time range.

if ( [self.start timeIntervalSinceNow] <= ( 15 * 60))
{
    // 'Now' is at least 900 seconds before the start time but could be later.
    if ( [self.end timeIntervalSinceNow] >= -( 15 * 60))
    {
        // 'Now' is not yet 900 seconds past the end time.
        inRange = YES
    }
}

return inRange;

Note: I haven't actually compiled this but I'm pretty sure the logic is correct.

Finally, you did notice that your last two lines

    return NO;

    [now release];
}

would have created a memory leak. (Release and then return ;^))

沙沙粒小 2024-11-07 01:27:08

这是一个距离问题 - 您想知道当前时间是否在目标时间的 900 秒之内。通过取两点之间的差的绝对值来计算距离问题。通过取差值的绝对值,两个样本的顺序并不重要。

/** 
 * @brief Return YES if the current time is within 900 seconds of the meeting time (NSDate* time).
 */
-(BOOL)currentTimeIsWithin900SecondsOf:(NSDate*)time
{
    NSDate* now = [NSDate date];
    return fabs( [now timeIntervalSinceReferenceDate] - [time timeIntervalSinceReferenceDate] ) < 900;
}

This is a distance problem - you want to know if the current time is within 900 seconds of the target time. Compute distance problems by taking the absolute value of the difference between the two points. By taking the absolute value of the difference, the order of the two samples doesn't matter.

/** 
 * @brief Return YES if the current time is within 900 seconds of the meeting time (NSDate* time).
 */
-(BOOL)currentTimeIsWithin900SecondsOf:(NSDate*)time
{
    NSDate* now = [NSDate date];
    return fabs( [now timeIntervalSinceReferenceDate] - [time timeIntervalSinceReferenceDate] ) < 900;
}
聚集的泪 2024-11-07 01:27:08

您需要创建三个 NSDate。然后使用 IntervalSinceReferenceDate 将 NSDates 转换为时间间隔并进行比较。您可以使用 NSDateComponents 手动设置 NSDate。

You need to create three NSDates. Then convert NSDates to time intervals using intervalSinceReferenceDate and compare those. You manually set up NSDates by using NSDateComponents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文