2012-03-26 237 views
1

由於「夜間時間」定義爲晚上8點到7點59分,我試圖計算兩個日期之間的白天時間和夜間時間量AM,以及上午8點到下午7點59分的「白天時間」。我將與當前時間戳比較的過去日期總是比現在少於24小時。計算兩個日期之間的日間時間和夜間時間

我該怎麼做?

+0

以及做香港專業教育學院嘗試了一個真正的骯髒的方式就是通過制定日期之間的秒數,然後,循環每秒添加到第二次。然後檢查它是否在時間值之間。不過似乎我的頭雖然圓了。 – ChrisM 2012-03-26 19:05:15

回答

4

當您開始處理夏令時等時,您很快就會變得非常複雜,因此您確實需要使用當前日曆並使用日曆/日期函數來計算時差。例如,如果時鐘在凌晨3點向前移動1小時,那麼根據您在時間更改當天的定義,應該有11個小時的「夜間」(而不是12個小時)。

我還沒有徹底測試此代碼,但它應該是相當接近:

// 8:00:00 AM in seconds 
static const NSTimeInterval daytimeStart = 28800; 
// 8:00:00 PM in seconds 
static const NSTimeInterval daytimeStop = 72000; 

// Convert date to a "time" which is the number of seconds since midnight, NOT considering time changes 
- (NSTimeInterval)getTimeForDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 

    NSDateComponents *dateComps = [cal components:NSHourCalendarUnit | 
                NSMinuteCalendarUnit | 
                NSSecondCalendarUnit 
             fromDate:date]; 
    NSTimeInterval time = dateComps.hour * 3600 + dateComps.minute * 60 + dateComps.second; 
    return time; 
} 

// Set the time to the specified hour and minutes/seconds to 0 
- (NSDate *)dateBySettingTimeToBeginningOfHour:(NSUInteger)hour ofDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 

    NSDateComponents *dateComps = [cal components:NSYearCalendarUnit | 
            NSMonthCalendarUnit | 
            NSDayCalendarUnit | 
            NSHourCalendarUnit | 
            NSMinuteCalendarUnit | 
            NSSecondCalendarUnit 
             fromDate:date]; 
    [dateComps setHour:hour]; 
    [dateComps setMinute:0]; 
    [dateComps setSecond:0]; 
    NSDate *newDate = [cal dateFromComponents:dateComps]; 
    return newDate; 
} 

// Add one day to the current date 
- (NSDate *)dateByAddingOneDayToDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 

    NSDateComponents *dateComps = [cal components:NSYearCalendarUnit | 
            NSMonthCalendarUnit | 
            NSDayCalendarUnit | 
            NSHourCalendarUnit | 
            NSMinuteCalendarUnit | 
            NSSecondCalendarUnit 
             fromDate:date]; 
    [dateComps setDay:[dateComps day] + 1]; 
    NSDate *newDate = [cal dateFromComponents:dateComps]; 
    return newDate; 
} 

// Check to see if it is daytime 
- (BOOL)isDayForDate:(NSDate *)date { 
    NSTimeInterval time = [self getTimeForDate:date]; 
    if (time >= daytimeStart) { 
     if (time < daytimeStop) { 
      return YES; 
     } 
    } 

    // Default 
    return NO; 
} 

// Check to see if it is night 
- (BOOL)isNightForDate:(NSDate *)date { 
    return ![self isDayForDate:date]; 
} 

// When do we transition from day to night or night to day next? 
- (NSDate *)dateForNextTransitionForDate:(NSDate *)date { 
    NSTimeInterval time = [self getTimeForDate:date]; 
    if (time < daytimeStart) { 
     return [self dateBySettingTimeToBeginningOfHour:8 ofDate:date]; 
    } 

    if (time < daytimeStop) { 
     return [self dateBySettingTimeToBeginningOfHour:20 ofDate:date]; 
    } 

    // Tomorrow morning 
    NSDate *newDate = [self dateByAddingOneDayToDate:date]; 
    newDate = [self dateBySettingTimeToBeginningOfHour:8 ofDate:newDate]; 
    return newDate; 
} 

- (NSTimeInterval)nightTimeFromDate:(NSDate *)startingDate toDate:(NSDate *)laterDate { 
    if ([startingDate compare:laterDate] != NSOrderedAscending) { 
     return 0; 
    } 

    NSTimeInterval nightTime = 0; 
    NSDate *now     = laterDate; 
    NSDate *transitionDate  = startingDate; 
    NSDate *loopDate   = startingDate; 
    BOOL isNight    = [self isNightForDate:startingDate]; 

    do { 
     transitionDate = [self dateForNextTransitionForDate:transitionDate]; 
     if ([transitionDate compare:now] == NSOrderedDescending) { 
      transitionDate = now; 
     } 

     if (isNight) { 
      nightTime = nightTime + [transitionDate timeIntervalSinceDate:loopDate]; 
     } 

     loopDate = transitionDate; 
     isNight = !isNight; 
    } while ([transitionDate compare:now] != NSOrderedSame); 

    return nightTime; 
} 

- (NSTimeInterval)dayTimeFromDate:(NSDate *)startingDate toDate:(NSDate *)laterDate { 
    if ([startingDate compare:laterDate] != NSOrderedAscending) { 
     return 0; 
    } 

    NSTimeInterval diff = [laterDate timeIntervalSinceDate:startingDate]; 
    diff = diff - [self nightTimeFromDate:startingDate toDate:laterDate]; 
    return diff; 
} 

// Example usage: 
NSDate *now = [NSDate date]; 
NSDate *date = [NSDate dateWithTimeInterval:-82800 sinceDate:now]; 

NSLog(@"%f", [self nightTimeFromDate:date toDate:now]); 
NSLog(@"%f", [self dayTimeFromDate:date toDate:now]); 

// Results are 43200.000000 and 39600.000000 right now in the middle of the day. 
+0

哇,非常感謝。會看看。我實際上是通過在循環中追加每秒的日期來獲得它的工作,直到達到當前日期,獲取日期組件小時並檢查它是否在8和20之間。 - 一個非常骯髒的解決方案。 – ChrisM 2012-03-26 21:53:10

+0

不客氣!我只做了一個小改動(在nightTimeFromDate中將isNight添加到循環中)使其更加高效。 – lnafziger 2012-03-26 22:00:54

+0

非常感謝。獲得與我的循環方法相同的結果,但您的解決方案更快。雖然我不明白它背後的邏輯。 – ChrisM 2012-03-27 18:40:50

1

白天=((今天最後一天)之間的天數* 12)+(#今天白天)+

夜間=((排名(對最後一天,白天#)今天最後一天)* 12)+夜間小時(#今天)+(在最後一天#的夜間時段)

編輯

int dthours = 0, nthours = 0; 

dthours += ternary with a calculation of 24 - starthour logic for daytime 
nthours += ternary with a calculation of 24 - starthour logic for nighttime 
dthours += ternary with a calculation of endhour logic for daytime 
nthours += ternary with a calculation of endhour logic for nighttime 

邏輯爲明天的夜間時段之間的天看起來像:

nthours += (endhour - 4 > 0) ? ((endhour - 16 > 0) ? endhour - 12 : 4) : endhour; 
+0

最後一天到今天之間沒有日子,總是不到24小時。 – ChrisM 2012-03-26 18:50:10

+0

意識到你的意思在幾秒鐘內......用86400替換24,用57600替換16,用14400替換4,以及用startsecond等開始計算秒數。 – 2012-03-26 19:19:43

+0

接受某個時間之間(86400 - 現在())會很棒 – 2012-03-26 19:43:57

相關問題