2011-04-07 82 views
6

所以,我有一個困難的時間(沒有雙關語意圖)發現如果一個NSDate是在一個特定的範圍內。考慮一下:下午5點到下午6點之間的預定訪問。我想查看預定的訪問是否在當前時間的+/- 15分鐘內。因此,我的安全範圍是下午4:45至6:15。我如何知道預定的訪問是否在範圍之內?這是我迄今爲止的代碼,它根本不起作用...如何查找NSDate是否在指定範圍內?

- (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]; 
} 

我很感激一些幫助。我在計算這個時間方面遇到困難。

在一個單獨的說明,我討厭使用時間,無論我與...

回答

5

你要檢查是否啓動時間小於900秒後的當前時間,結束時間小於900秒之前,在當前的時間。 timeIntervalSinceDate:返回一個正數,如果它被調用的對象是之後的參數,則需要檢查開始< = 900並結束> = -900。此外,您可以通過使用timeIntervalSinceNow而不是手動獲取當前日期並將其傳遞到timeIntervalSinceDate:來簡化代碼。最後,如果你可以假設(或先前確保)開始是在結束之前,那麼你不需要兩個中間測試,因爲它們都必須對其他兩個都是正確的。

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

這是比我的更好的答案。 – Moshe 2011-04-07 01:36:59

+0

這工作,並且似乎我的第一個註釋嘗試是最接近的。謝謝您的幫助! – Gup3rSuR4c 2011-04-07 03:49:21

0

工作什麼平臺你需要創建三個NSDates。然後使用intervalSinceReferenceDate將NSDates轉換爲時間間隔並對其進行比較。您可以使用NSDateComponents手動設置NSDates。

3

這裏的一些詳細(傻)代碼,以解釋我的方法解決這個問題

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]; 
} 

將創造一個內存泄漏。 (發佈然後返回; ^))

+0

你和@ughoavgfhw之間的選擇很艱難,因爲你們基本上都是這麼說的,但最終@ ughoavgfhw的代碼與我寫的代碼基本相同,所以我和他一起去了。 **然而**,我非常感謝你的*大聲笑*帖子和泄漏小費。 – Gup3rSuR4c 2011-04-07 03:55:50

2

這是一個距離問題 - 您想知道當前時間是否在目標時間的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; 
} 
相關問題