helper: Added combineHolidaysAndExclusions

This commit is contained in:
Paul Beckingham 2016-04-21 23:57:33 -04:00
parent 837f166dc3
commit e626d93044
2 changed files with 46 additions and 0 deletions

View file

@ -479,3 +479,48 @@ Daterange overallRangeFromIntervals (const std::vector <Interval>& intervals)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::vector <Daterange> combineHolidaysAndExclusions (
const Daterange& range,
const Rules& rules,
const std::vector <Exclusion>& exclusions)
{
// Start with the set of all holidays, intersected with range.
std::vector <Daterange> results;
results = addRanges (range, results, rangesFromHolidays (rules));
// Find exclusions 'exc day on <date>' and remove from holidays.
// Find exlcusions 'exc day off <date>' and add to holidays.
std::vector <Daterange> daysOn;
std::vector <Daterange> 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 <Daterange> 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;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -58,6 +58,7 @@ std::vector <Daterange> rangesFromHolidays (const Rules&);
std::vector <Daterange> addRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&); std::vector <Daterange> addRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&);
std::vector <Daterange> subtractRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&); std::vector <Daterange> subtractRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&);
Daterange overallRangeFromIntervals (const std::vector <Interval>&); Daterange overallRangeFromIntervals (const std::vector <Interval>&);
std::vector <Daterange> combineHolidaysAndExclusions (const Daterange&, const Rules&, const std::vector <Exclusion>&);
// utiŀ.cpp // utiŀ.cpp
std::string osName (); std::string osName ();