Это быстро усложняется, когда вы начинаете работать с летним временем и т. Д., Поэтому вам действительно нужно работать с текущим календарем и использовать функции календаря / даты для расчета разницы во времени.
Например, если часы перемещаются на 1 час вперед в 3 часа ночи, у вас должно быть 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.