File: Exclusion.cpp

package info (click to toggle)
timew 1.1.1+ds.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,348 kB
  • sloc: cpp: 22,428; python: 3,612; sh: 111; makefile: 15
file content (204 lines) | stat: -rw-r--r-- 6,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <cmake.h>
#include <Exclusion.h>
#include <Datetime.h>
#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 configuration, and are passed here as name/
// value strings.
//
//   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
//
Exclusion::Exclusion (const std::string& name, const std::string& value)
//void Exclusion::initialize (const std::string& 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] == "exclusions")
  {
    if (_tokens.size () == 4 &&
        _tokens[1] == "days"  &&
        _tokens[3] == "on") {
      _additive = true;
      return;
    }
    if (_tokens.size () == 4 &&
        _tokens[1] == "days"  &&
        _tokens[3] == "off")
    {
      _additive = false;
      return;
    }
    else if (Datetime::dayOfWeek (_tokens[1]) != -1)
    {
      _additive = false;
      return;
    }
  }

  throw format ("Unrecognized exclusion syntax: '{1}' '{2}'.", name, value);
}

////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Exclusion::tokens () const
{
  return _tokens;
}

////////////////////////////////////////////////////////////////////////////////
// Within range, yield a collection of recurring ranges as defined by _tokens.
//
//   exc monday <block> [<block> ...]  --> yields a day range for every monday,
//                                         and every block within
//   exc day on <date>                 --> yields single day range
//   exc day off <date>                --> yields single day range
//
std::vector <Range> Exclusion::ranges (const Range& range) const
{
  std::vector <Range> results;
  int dayOfWeek;

  if (_tokens[1] == "days" &&
      (_tokens[3] == "on" ||
       _tokens[3] == "off"))
  {
    auto day = _tokens[2];
    std::replace (day.begin (), day.end (), '_', '-');
    Datetime start (day);
    Datetime end (start);
    ++end;
    Range all_day (start, end);
    if (range.overlap (all_day))
      results.push_back (all_day);
  }

  else if ((dayOfWeek = Datetime::dayOfWeek (_tokens[1])) != -1)
  {
    Datetime start (range.start.year (), range.start.month (), range.start.day (), 0, 0, 0);

    Range myRange = {range};

    if (myRange.is_open())
    {
      myRange.end = Datetime("tomorrow");
    }

    while (start <= myRange.end)
    {
      if (start.dayOfWeek () == dayOfWeek)
      {
        Datetime end = start;
        ++end;

        // Now that 'start' and 'end' represent the correct day, compose a set
        // of Range objects for each time block.
        for (unsigned int block = 2; block < _tokens.size (); ++block)
        {
          auto r = rangeFromTimeBlock (_tokens[block], start, end);
          if (myRange.overlap (r))
            results.push_back (r);
        }
      }

      ++start;
    }
  }

  return results;
}

////////////////////////////////////////////////////////////////////////////////
bool Exclusion::additive () const
{
  return _additive;
}

////////////////////////////////////////////////////////////////////////////////
std::string Exclusion::dump () const
{
  return join (" ", _tokens);
}

////////////////////////////////////////////////////////////////////////////////
Range Exclusion::rangeFromTimeBlock (
  const std::string& block,
  const Datetime& start,
  const Datetime& end) const
{
  Pig pig (block);

  if (pig.skip ('<'))
  {
    int hh, mm, ss;
    if (pig.getHMS (hh, mm, ss))
      return Range (Datetime (start.year (), start.month (), start.day (),  0,  0,  0),
                    Datetime (start.year (), start.month (), start.day (), hh, mm, ss));
  }
  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),
                    Datetime (end.year (),   end.month (),   end.day (),    0,  0,  0));
  }
  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);
}

////////////////////////////////////////////////////////////////////////////////