计算跳过假期和周末的工作日

我有这个问题,流程从初始日期开始,比方说 2022-04-14,我必须将此日期添加十天,但我必须考虑周末和假期,所以也许如果在初始日期和最终日期之间我们有一个周末和一个假期的日期最终日期是 2022-04-27。如果初次约会是在周末或节假日开始的,也是必要的考虑。
这就是问题所在。

我的第一个方法是创建一个循环,每天检查初始日期加十天和每个星期六、星期日和节假日总和一天,所以我将有十天加三天,这个结果将添加到我的初始日期到最后计算最终日期。

我的问题是,是否有其他解决方案或实施方式可以更有效?因为这可能在未来会被很多人使用。


慕虎7371278
浏览 134回答 2
2回答

动漫人物

另外不要忘记在添加累积的额外天数(周末和假期)时,这些天数可能会涵盖新的周末和假期,因此您必须“递归”执行此操作。最简单的解决方案最简单的解决方案可以从初始日期开始,将其增加一天,然后检查每一天是否是可跳过的(周末或假期)。如果不是,请减少天数,然后重复直到添加所需的天数。这就是它的样子:func addDays(start time.Time, days int) (end time.Time) {&nbsp; &nbsp; for end = start; days > 0; {&nbsp; &nbsp; &nbsp; &nbsp; end = end.AddDate(0, 0, 1)&nbsp; &nbsp; &nbsp; &nbsp; if !skippable(end) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; days--&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return end}func skippable(day time.Time) bool {&nbsp; &nbsp; if wd := day.Weekday(); wd == time.Saturday || wd == time.Sunday {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; if isHoliday(day) {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; return false}func isHoliday(day time.Time) bool {&nbsp; &nbsp; return false // TODO}测试它:d := time.Date(2022, time.April, 14, 0, 0, 0, 0, time.UTC)fmt.Println(addDays(d, 0))fmt.Println(addDays(d, 1))fmt.Println(addDays(d, 10))哪些输出(在Go Playground上尝试):2022-04-14 00:00:00 +0000 UTC2022-04-15 00:00:00 +0000 UTC2022-04-28 00:00:00 +0000 UTC更快的解决方案更快的解决方案可以避免循环日复一日。计算周末天数:知道初始日期是哪一天,知道你要步进多少天,我们就可以计算出中间的周末天数。例如,如果我们必须步进 14 天,即整整 2 周,那肯定正好包括 4 个周末。如果我们必须多走一步,例如 16 天,那也包括 2 个完整的星期(4 个周末),以及可选的 1 或 2 天,我们可以轻松检查。计算假期:我们可能会使用一个技巧来在排序切片(按日期排序)中列出假期,因此我们可以轻松/快速地找到 2 个日期之间的天数。我们可以在一个排序的切片中对某个时间段的开始和结束日期进行二分查找,一个时间段中的假期数就是这两个索引之间的元素数。注意:周末假期不得包含在此切片中(否则会被计算两次)。让我们看看这个实现的样子:// holidays is a sorted list of holidaysvar holidays = []time.Time{&nbsp; &nbsp; time.Date(2022, time.April, 15, 0, 0, 0, 0, time.UTC),}func addDaysFast(start time.Time, days int) (end time.Time) {&nbsp; &nbsp; weekendDays := days / 7 * 2 // Full weeks&nbsp; &nbsp; // Account for weekends if there's fraction week:&nbsp; &nbsp; for day, fraction := start.AddDate(0, 0, 1), days%7; fraction > 0; day, fraction = day.AddDate(0, 0, 1), fraction-1 {&nbsp; &nbsp; &nbsp; &nbsp; if wd := day.Weekday(); wd == time.Saturday || wd == time.Sunday {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; weekendDays++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; end = start.AddDate(0, 0, days+weekendDays)&nbsp; &nbsp; first := sort.Search(len(holidays), func(i int) bool {&nbsp; &nbsp; &nbsp; &nbsp; return !holidays[i].Before(start)&nbsp; &nbsp; })&nbsp; &nbsp; last := sort.Search(len(holidays), func(i int) bool {&nbsp; &nbsp; &nbsp; &nbsp; return !holidays[i].Before(end)&nbsp; &nbsp; })&nbsp; &nbsp; // There are last - first holidays&nbsp; in the range [start..end]&nbsp; &nbsp; numHolidays := last - first&nbsp; &nbsp; if last < len(holidays) && holidays[last].Equal(end) {&nbsp; &nbsp; &nbsp; &nbsp; numHolidays++ // end is exactly a holiday&nbsp; &nbsp; }&nbsp; &nbsp; if numHolidays == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return end // We're done&nbsp; &nbsp; }&nbsp; &nbsp; // We have to add numHolidays, using the same "rules" above:&nbsp; &nbsp; return addDaysFast(end, numHolidays)}测试它:d := time.Date(2022, time.April, 14, 0, 0, 0, 0, time.UTC)fmt.Println(addDaysFast(d, 0))fmt.Println(addDaysFast(d, 1))fmt.Println(addDaysFast(d, 10))输出(在Go Playground上尝试):2022-04-14 00:00:00 +0000 UTC2022-04-18 00:00:00 +0000 UTC2022-04-29 00:00:00 +0000 UTC改善addDaysFast()仍然有改进的方法addDaysFast():检查小数周中周末天数的初始循环可以用算术计算代替(参见示例)递归可以用迭代解决方案代替另一种解决方案可以将周末列为假期,因此可以消除计算周末的第一部分(不得包括重复项)

RISEBY

我会像这样计算工作日。除了假日查找之外,它没有循环并且具有 O(1) 时间和空间复杂度:计算范围的开始日期和结束日期之间的天数将其除以 7。商是范围内的周数;余下的是整日剩余的小数周。范围内的基本工作日数是范围内的周数乘以 5,因为每 7 天的时间段,无论何时开始,都包含 2 个周末。末尾的小数周,在整天中,必须进行调整以删除任何周末。这是小数周中的整数天数和范围结束日期的星期几的函数。假期查询留给读者作为练习,因为这太过依赖于文化、地区和业务而无法解决。应该注意,这里的逻辑中有一个内置的假设,即“假期”不会出现在周六或周日。func BusinessDays(start time.Time, end time.Time) int {&nbsp; &nbsp; from := toDate(start)&nbsp; &nbsp; thru := toDate(end)&nbsp; &nbsp; weeks, days := delta(from, thru)&nbsp; &nbsp; adjustedDays := adjustDays(days, thru.Weekday())&nbsp; &nbsp; businessDays := ( ( weeks * 5) + adjustedDays ) - holidaysInRange(from, thru)&nbsp; &nbsp; return businessDays}func toDate(t time.Time) time.Time {&nbsp; &nbsp; y, m, d := t.Date()&nbsp; &nbsp; adjusted := time.Date(y, m, d, 0, 0, 0, 0, t.Location())&nbsp; &nbsp; return adjusted}func holidaysInRange(from, thru time.Time) (cnt int) {&nbsp; &nbsp; // TODO: Actual implementation left as an exercise for the reader&nbsp; &nbsp; return cnt}func delta(from, thru time.Time) (weeks, days int) {&nbsp; &nbsp; const seconds_per_day = 86400&nbsp; &nbsp; totalDays := (thru.Unix() - from.Unix()) / seconds_per_day&nbsp; &nbsp; weeks = int(totalDays / 7)&nbsp; &nbsp; days = int(totalDays % 7)&nbsp; &nbsp; return weeks, days}func adjustDays(days int, lastDay time.Weekday) int {&nbsp; &nbsp; adjusted := days&nbsp; &nbsp; switch days {&nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 2&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Monday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Monday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 2&nbsp; &nbsp; &nbsp; &nbsp; case time.Tuesday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 4:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Monday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Tuesday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 2&nbsp; &nbsp; &nbsp; &nbsp; case time.Wednesday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 5:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Monday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Tuesday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Wednesday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 2&nbsp; &nbsp; &nbsp; &nbsp; case time.Thursday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 6:&nbsp; &nbsp; &nbsp; &nbsp; switch lastDay {&nbsp; &nbsp; &nbsp; &nbsp; case time.Sunday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Monday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Tuesday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Wednesday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Thursday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 2&nbsp; &nbsp; &nbsp; &nbsp; case time.Friday:&nbsp; &nbsp; &nbsp; &nbsp; case time.Saturday:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjusted -= 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return adjusted}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go