计算两个日期之间的日间时间和夜晚时间

时间:2012-03-26 18:03:32

标签: objective-c cocoa date time nsdate

我试图以秒为单位计算两个日期之间的日间时间和夜间时间,假设“夜间时间”定义为上午8:00到上午7:59,“白天时间”定义为8: 00:00到7:59 PM。我将与当前时间戳进行比较的过去日期总是不到24小时。

我该怎么做?

2 个答案:

答案 0 :(得分:4)

当您开始处理夏令时等时,这很快变得复杂,因此您确实需要使用当前日历并使用日历/日期函数来计算时差。

例如,如果时钟在凌晨3:00向前移动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.

答案 1 :(得分: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;