gpt4 book ai didi

objective-c - 如何从 NSDate 中减去持续时间,但不包括周末?

转载 作者:太空狗 更新时间:2023-10-30 03:43:32 26 4
gpt4 key购买 nike

以今天为例,如何判断是230个工作日之前的日期?

我知道如何使用 while 循环检查日期并在工作日减去 1 来迭代执行此操作,但我想知道是否有更好的方法。

另外,我们以周日下午 1 点为例,从该时间减去 3 个工作日和 2 小时。首先,从周末减去工作时间没有意义。所以它必须将时间移到星期五的 23:59:59,然后减去这 3 天 2 小时。

如果是星期一凌晨 1:30,我从那个时间减去 5 天和 3 个工作小时,那么结果应该是前一周的星期五下午 22:30。


测试凯文方法的代码:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dc = [[NSDateComponents new] autorelease];
dc.month = 12;
dc.day = 19;
dc.year = 2011;
dc.hour = 1;
dc.minute = 0;
dc.second = 0;

NSDate *date = [cal dateFromComponents:dc];

NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:nil locale:nil]);
date = dateBySubtractingWorkOffset(date, 0, 2);
NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:nil locale:nil]);

输出日志:

2011-12-02 16:33:46.878 otest[7124:707] 2011-12-19 01:00:00 -0500
2011-12-02 16:33:47.659 otest[7124:707] 2011-12-18 23:00:00 -0500

它永远不应该是 12-18,因为那是星期天。

最佳答案

算出你约会的最后一个周末有多长,从你的日期和你的偏移量中减去那个数额。现在您可以将您的偏移量除以 5 以计算出您的偏移量中有多少周,然后将其乘以 7 并从您的日期中减去这个新值。取你之前的偏移量(你除以 5 的那个)并将它除以 5,以获得剩余天数。如果它大于 0,请从您的日期中减去偏移量 + 2(周末)。

请注意,这假设每个工作日都是工作日。公司假期往往会使该假设无效。如果您需要处理假期,就会遇到更棘手的问题。

更新:这里尝试修复 David 的代码以实际表达这里的想法:

NSDate *dateBySubtractingWorkOffset(NSDate *date, NSUInteger days, NSUInteger hours) {
const int secsInHour = 60*60;
const int secsInDay = 24*secsInHour;
NSTimeInterval offset = days*secsInDay + hours*secsInHour;
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

// figure out distance from last weekend
{
NSUInteger units = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit;
NSDateComponents *dc = [cal components:units fromDate:date];
if (dc.weekday == 1 || dc.weekday == 7) {
// we're in the weekend already. Let's just back up until friday
// and then we can start our calculations there
} else {
// figure out our offset from sunday 23:59:59
dc.day -= (dc.weekday - 1);
dc.weekday = 1;
dc.hour = 23;
dc.minute = 23;
dc.second = 23;
NSDate *sunday = [cal dateFromComponents:dc];
NSTimeInterval newOffset = [date timeIntervalSinceDate:sunday];
if (offset < newOffset) {
// our offset doesn't even go back to sunday, we don't need any calculations
return [date dateByAddingTimeInterval:-offset];
}
offset -= [date timeIntervalSinceDate:sunday];
// Now we can jump back to Friday with our new offset
}
// Calculate last friday at 23:59:59
dc.day -= (dc.weekday % 7 + 1);
dc.hour = 23;
dc.minute = 59;
dc.second = 59;
date = [cal dateFromComponents:dc];
}

// We're now set to Friday 23:59:59
// Lets figure out how many weeks we have
int secsInWorkWeek = 5*secsInDay;
NSInteger weeks = (NSInteger)trunc(offset / secsInWorkWeek);
offset -= weeks*secsInWorkWeek;
if (weeks > 0) {
// subtract that many weeks from the date
NSDateComponents *dc = [[NSDateComponents alloc] init];
dc.week = -weeks;
date = [cal dateByAddingComponents:dc toDate:date options:0];
[dc release];
}
// now we can just subtract our remaining offset from the date
return [date dateByAddingTimeInterval:-offset];
}

关于objective-c - 如何从 NSDate 中减去持续时间,但不包括周末?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320213/

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