gpt4 book ai didi

objective-c - 计算两个日期之间的白天时间和夜间时间

转载 作者:行者123 更新时间:2023-12-03 16:52:49 25 4
gpt4 key购买 nike

我正在尝试以秒为单位计算两个日期之间的白天时间和夜间时间,假设“夜间时间”定义为晚上 8:00 到上午 7:59,“白天时间”定义为 8:上午 00 点至晚上 7:59。我将与当前时间戳进行比较的过去日期始终比现在时间短 24 小时。

我该怎么做?

最佳答案

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

例如,如果时钟在凌晨 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.

关于objective-c - 计算两个日期之间的白天时间和夜间时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com