From e626d9304411660c999a95db7b0a4770e5911703 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 21 Apr 2016 23:57:33 -0400 Subject: [PATCH] helper: Added combineHolidaysAndExclusions --- src/helper.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/timew.h | 1 + 2 files changed, 46 insertions(+) diff --git a/src/helper.cpp b/src/helper.cpp index 5473038e..8feacde0 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -479,3 +479,48 @@ Daterange overallRangeFromIntervals (const std::vector & intervals) } //////////////////////////////////////////////////////////////////////////////// +std::vector combineHolidaysAndExclusions ( + const Daterange& range, + const Rules& rules, + const std::vector & exclusions) +{ + // Start with the set of all holidays, intersected with range. + std::vector results; + results = addRanges (range, results, rangesFromHolidays (rules)); + + // Find exclusions 'exc day on ' and remove from holidays. + // Find exlcusions 'exc day off ' and add to holidays. + std::vector daysOn; + std::vector daysOff; + for (auto& exclusion : exclusions) + { + if (exclusion.tokens ()[1] == "day") + { + if (exclusion.additive ()) + for (auto& range : exclusion.ranges (range)) + daysOn.push_back (range); + else + for (auto& range : exclusion.ranges (range)) + daysOff.push_back (range); + } + } + + // daysOff are combined with existing holidays. + results = addRanges (range, results, daysOff); + + // daysOn are subtracted from the existing holidays. + results = subtractRanges (range, results, daysOn); + + // Expand all exclusions that are not 'exc day ...' into excluded ranges that + // overlage with range. + std::vector exclusionRanges; + for (auto& exclusion : exclusions) + if (exclusion.tokens ()[1] != "day") + for (auto& range : exclusion.ranges (range)) + exclusionRanges.push_back (range); + + results = addRanges (range, results, exclusionRanges); + return results; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/timew.h b/src/timew.h index 36b1496f..dc335d31 100644 --- a/src/timew.h +++ b/src/timew.h @@ -58,6 +58,7 @@ std::vector rangesFromHolidays (const Rules&); std::vector addRanges (const Daterange&, const std::vector &, const std::vector &); std::vector subtractRanges (const Daterange&, const std::vector &, const std::vector &); Daterange overallRangeFromIntervals (const std::vector &); +std::vector combineHolidaysAndExclusions (const Daterange&, const Rules&, const std::vector &); // utiƀ.cpp std::string osName ();