Exclusion: New syntax

- Removed ::serialize which is unused.
- Moved to a two-arg ctor, rather than a 2-step init.
This commit is contained in:
Paul Beckingham 2016-04-27 18:01:35 -04:00
parent 07077ebee4
commit 500fa9784a
2 changed files with 44 additions and 48 deletions

View file

@ -30,41 +30,46 @@
#include <Pig.h>
#include <shared.h>
#include <format.h>
#include <algorithm>
////////////////////////////////////////////////////////////////////////////////
// An exclusion represents untrackable time such as holidays, weekends, evenings
// and lunch. By default there are none, but they may be configured. Once there
// are exclusions defined, the :fill functionality is enabled.
//
// Exclusions are instantiated from the 'define exclusions:' rule. This method
// simply validates.
// Exclusions are instantiated from configuration, and are passed here as name/
// value strings.
//
// Syntax:
// exc monday <block> [<block> ...]
// exc day on <date>
// exc day off <date>
// Name Value
// -------------------------- ------------------------
// exclusions.days.2016_01_01 on
// exclusions.days.2016_01_02 off
// exclusions.friday <8:00 12:00-12:45 >17:30
// exclusions.monday <8:00 12:00-12:45 >17:30
// exclusions.thursday <8:00 12:00-12:45 >17:30
// exclusions.tuesday <8:00 12:00-12:45 >18:30
// exclusions.wednesday <8:00 12:00-13:30 >17:30
//
// block:
// <HH:MM:SS | HH:MM:SS-HH:MM:SS | >HH:MM:SS
//
void Exclusion::initialize (const std::string& line)
Exclusion::Exclusion (const std::string& name, const std::string& value)
//void Exclusion::initialize (const std::string& line)
{
_tokens = split (line);
_tokens = split (name, '.');
for (auto& token : split (value))
_tokens.push_back (token);
// Validate syntax only. Do nothing with the data.
if (_tokens.size () >= 2 &&
_tokens[0] == "exc")
_tokens[0] == "exclusions")
{
if (_tokens.size () == 4 &&
_tokens[1] == "day" &&
_tokens[2] == "on")
{
_tokens[1] == "days" &&
_tokens[3] == "on") {
_additive = true;
return;
}
if (_tokens.size () == 4 &&
_tokens[1] == "day" &&
_tokens[2] == "off")
_tokens[1] == "days" &&
_tokens[3] == "off")
{
_additive = false;
return;
@ -76,7 +81,7 @@ void Exclusion::initialize (const std::string& line)
}
}
throw format ("Unrecognized exclusion syntax: '{1}'.", line);
throw format ("Unrecognized exclusion syntax: '{1}' '{2}'.", name, value);
}
////////////////////////////////////////////////////////////////////////////////
@ -98,16 +103,18 @@ std::vector <Range> Exclusion::ranges (const Range& range) const
std::vector <Range> results;
int dayOfWeek;
if (_tokens[1] == "day" &&
(_tokens[2] == "on" ||
_tokens[2] == "off"))
if (_tokens[1] == "days" &&
(_tokens[3] == "on" ||
_tokens[3] == "off"))
{
Datetime start (_tokens[3]);
auto day = _tokens[2];
std::replace (day.begin (), day.end (), '_', '-');
Datetime start (day);
Datetime end (start);
++end;
Range day (start, end);
if (range.overlap (day))
results.push_back (day);
Range all_day (start, end);
if (range.overlap (all_day))
results.push_back (all_day);
}
else if ((dayOfWeek = Datetime::dayOfWeek (_tokens[1])) != -1)
@ -139,12 +146,6 @@ bool Exclusion::additive () const
return _additive;
}
////////////////////////////////////////////////////////////////////////////////
std::string Exclusion::serialize () const
{
return std::string ("exc") + join (" ", _tokens);
}
////////////////////////////////////////////////////////////////////////////////
std::string Exclusion::dump () const
{
@ -164,26 +165,24 @@ Range Exclusion::rangeFromTimeBlock (
int hh, mm, ss;
if (pig.getHMS (hh, mm, ss))
return Range (start, Datetime (start.year (), start.month (), start.day (), hh, mm, ss));
throw format ("Malformed time block '{1}'.", block);
}
else if (pig.skip ('>'))
{
int hh, mm, ss;
if (pig.getHMS (hh, mm, ss))
return Range (Datetime (start.year (), start.month (), start.day (), hh, mm, ss), end);
throw format ("Malformed time block '{1}'.", block);
}
int hh1, mm1, ss1;
int hh2, mm2, ss2;
if (pig.getHMS (hh1, mm1, ss1) &&
pig.skip ('-') &&
pig.getHMS (hh2, mm2, ss2))
return Range (
Datetime (start.year (), start.month (), start.day (), hh1, mm1, ss1),
Datetime (start.year (), start.month (), start.day (), hh2, mm2, ss2));
else
{
int hh1, mm1, ss1;
int hh2, mm2, ss2;
if (pig.getHMS (hh1, mm1, ss1) &&
pig.skip ('-') &&
pig.getHMS (hh2, mm2, ss2))
return Range (
Datetime (start.year (), start.month (), start.day (), hh1, mm1, ss1),
Datetime (start.year (), start.month (), start.day (), hh2, mm2, ss2));
}
throw format ("Malformed time block '{1}'.", block);
}

View file

@ -35,13 +35,10 @@
class Exclusion
{
public:
Exclusion () = default;
void initialize (const std::string&);
Exclusion (const std::string&, const std::string&);
std::vector <std::string> tokens () const;
std::vector <Range> ranges (const Range&) const;
bool additive () const;
std::string serialize () const;
std::string dump () const;
private: