所以,我很难(没有双关语(找到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];
}
我将不胜感激。我在计算这个时间时遇到困难。
另外,无论我使用什么平台,我都讨厌处理时间......
您要检查开始时间是否小于当前时间后 900 秒,结束时间是否小于当前时间前 900 秒。 timeIntervalSinceDate:
如果调用的对象在参数之后,则返回一个正数,您需要检查开始 <= 900 和结束>= −900。此外,您可以通过使用 timeIntervalSinceNow
来简化代码,而不是手动获取当前日期并将其传递给 timeIntervalSinceDate:
。最后,如果你可以假设(或事先确保(开始在结束之前,那么你就不需要两个中间测试,因为它们都必须为真,其他两个测试都是真的。
if([self.start timeIntervalSinceNow] <= 900 && [self.end timeIntervalSinceNow] >= −900) {
NSLog(@"In range")
}
这里有一些冗长(和愚蠢(的代码,解释了我解决这个问题的方法
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];
}
会造成内存泄漏。(释放然后返回;^((
这是一个距离问题 - 您想知道当前时间是否在目标时间的 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;
}
您需要创建三个 NSDate。然后使用intervalSinceReferenceDate将NSDate转换为时间间隔并进行比较。您可以使用 NSDateComponents手动设置 NSDate。