-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interval.cpp
128 lines (111 loc) · 2.24 KB
/
Interval.cpp
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
#include "Interval.hpp"
#include <ostream>
#include <SoftwareSerial.h>
#include "Clock.hpp"
namespace sleep
{
namespace
{
Interval MakeInterval(Period const period, Day const day)
{
switch (period)
{
case Period::Day:
{
return Interval(Clock::Get(Period::Day, day), Clock::Get(Period::BeforeNight, day));
}
case Period::BeforeNight:
{
return Interval(Clock::Get(Period::BeforeNight, day), Clock::Get(Period::Night, day));
}
case Period::Night:
{
return Interval(Clock::Get(Period::Night, day), Clock::Get(Period::AfterNight, day));
}
case Period::AfterNight:
{
return Interval(Clock::Get(Period::AfterNight, day), Clock::Get(Period::Day, day));
}
}
}
Interval AccessInterval(Period const period, Day const day)
{
switch (period)
{
case Period::Day:
{
return MakeInterval(period, day);
}
case Period::BeforeNight:
{
return MakeInterval(period, day);
}
case Period::Night:
{
return MakeInterval(period, day);
}
case Period::AfterNight:
{
return MakeInterval(period, day);
}
}
}
}
Interval::Interval(WallClock const & o_begin, WallClock const & o_end)
: begin(o_begin)
, end(o_end)
{
}
Interval & Interval::operator =(Interval const & other)
{
if (this != &other)
{
begin = other.begin;
end = other.end;
}
return *this;
}
bool Interval::contains(WallClock const & wallClock) const
{
if (end < begin)
{
// cross 00:00
return ((wallClock >= begin) or (wallClock < end));
}
else
{
return ((wallClock >= begin) and (wallClock < end));
}
}
Minute Interval::duration() const
{
return end - begin;
}
void Interval::print() const
{
Serial.print("[");
begin.print();
Serial.print("-");
end.print();
Serial.print("]");
}
Interval Interval::Get(Period const period, Day const day)
{
return AccessInterval(period, day);
}
void Interval::UpdateIntervals()
{
for (auto const period: Period_All)
{
for (Day const day: Week())
{
AccessInterval(period, day) = MakeInterval(period, day);
}
}
}
std::ostream & operator <<(std::ostream & stream, Interval const & interval)
{
stream << "[" << interval.begin << "-" << interval.end << "]";
return stream;
}
}