-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisholiday.h
232 lines (169 loc) · 4.85 KB
/
isholiday.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* GnuDialer - Complete, free predictive dialer
*
* Complete, free predictive dialer for contact centers.
*
* Copyright (C) 2006, GnuDialer Project
*
* Heath Schultz <[email protected]>
*
* This program is free software, distributed under the terms of
* the GNU General Public License.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h>
#include "exceptions.h"
#ifndef ISHOLIDAY
#define ISHOLIDAY
class StaticHoliday {
public:
StaticHoliday() {}
~StaticHoliday() {}
void SetDate(int month, int day) {
itsMonth = month;
itsDay = day;
}
bool IsToday(int month, int day) {
if (month == itsMonth && day == itsDay) return true;
else return false;
}
private:
int itsMonth, itsDay;
};
StaticHoliday ReturnStaticHoliday(int month, int day) {
StaticHoliday TheHoliday;
TheHoliday.SetDate(month,day);
return TheHoliday;
}
class DynamicHoliday {
public:
DynamicHoliday() { itsIsLast = false; }
~DynamicHoliday() {}
void SetDate(int month, int dayOfWeek, int whichWeek, bool isLast) {
itsMonth = month;
itsDayOfWeek = dayOfWeek;
itsWhichWeek = whichWeek;
itsIsLast = isLast;
}
bool IsToday(int month, int dayOfMonth, int dayOfWeek) {
int whichWeek = dayOfMonth / 7 + 1;
bool isLast = false;
int lastDayOfWeek = dayOfMonth + ( 7 - dayOfWeek ) + 1;
int daysInMonth = 28;
if (month + 1 == 4 || \
month + 1 == 6 || \
month + 1 == 9 || \
month + 1 == 11)
daysInMonth = 30;
else if (month != 2)
daysInMonth = 31;
if (lastDayOfWeek >= daysInMonth) isLast = true;
if (itsIsLast) {
if (itsDayOfWeek == dayOfWeek && itsMonth == month && isLast)
return true;
else return false;
}
if (!itsIsLast) {
if (itsDayOfWeek == dayOfWeek && itsMonth == month && itsWhichWeek == whichWeek)
return true;
else return false;
}
return false;
}
private:
int itsMonth, itsDayOfWeek, itsWhichWeek;
bool itsIsLast;
};
DynamicHoliday ReturnDynamicHoliday(int month, int dayOfWeek, int whichWeek, bool isLast) {
DynamicHoliday TheDynamicHoliday;
TheDynamicHoliday.SetDate(month,dayOfWeek,whichWeek,isLast);
return TheDynamicHoliday;
}
std::string preParseLine(std::string line) {
// Gets rid of the "add->" directive,
// and gets rid of any comments.
line = line.substr(5,line.length()-5);
if (line.find("#",0) != std::string::npos) {
line = line.substr(0,line.find("#",0) - 1);
}
return line;
}
StaticHoliday parseStaticHoliday(std::string line) {
std::stringstream HolidayStream;
HolidayStream << line;
std::string tempDay, tempMonth;
std::getline(HolidayStream,tempDay,'.');
std::getline(HolidayStream,tempMonth,'.');
return ReturnStaticHoliday(atoi(tempMonth.c_str()),atoi(tempDay.c_str()));
}
DynamicHoliday parseDynamicHoliday(std::string line) {
std::stringstream HolidayStream;
HolidayStream << line;
std::string tempDayOfWeek, tempMonth, tempWhichWeek;
std::getline(HolidayStream,tempWhichWeek,'.');
std::getline(HolidayStream,tempDayOfWeek,'.');
std::getline(HolidayStream,tempMonth,'.');
int whichWeek = 0;
bool isLast = false;
if (tempWhichWeek.find("L",0) != std::string::npos \
|| tempWhichWeek.find("l",0) != std::string::npos)
isLast = true;
else {
whichWeek = atoi(tempWhichWeek.c_str());
}
return ReturnDynamicHoliday(atoi(tempMonth.c_str()),atoi(tempDayOfWeek.c_str()),whichWeek,isLast);
}
bool isHoliday() {
bool isHolidayToday = false;
time_t thetime;
tm * ptm;
time(&thetime);
ptm = localtime(&thetime);
int month = ptm->tm_mon;
int day = ptm->tm_mday;
int dayofweek = ptm->tm_wday;
std::vector<StaticHoliday> TheStaticHolidays;
std::vector<DynamicHoliday> TheDynamicHolidays;
int parseStatus = 0; // 1 = in static, 2 = in dynamic
std::ifstream HolidaysIn; // <- haha
HolidaysIn.open("/usr/lib/gnudialer/timezones/holidays.txt");
// if (!HolidaysIn) std::cerr << "Unable to open holidays.txt!" << std::endl;
if (!HolidaysIn) {
throw xFileOpenError("holidays.txt");
}
for(std::string tempLine; std::getline(HolidaysIn,tempLine,'\n'); ) {
if (tempLine.find("[static]",0) != std::string::npos)
parseStatus = 1;
if (tempLine.find("[dynamic]",0) != std::string::npos)
parseStatus = 2;
if (tempLine.find("add->",0) != std::string::npos) {
if (tempLine[0] != ';' && tempLine[0] != '#') {
if (parseStatus == 1) {
TheStaticHolidays.push_back(parseStaticHoliday(preParseLine(tempLine)));
}
if (parseStatus == 2) {
TheDynamicHolidays.push_back(parseDynamicHoliday(preParseLine(tempLine)));
}
}
}
}
HolidaysIn.close();
for (unsigned int i = 0; i < TheStaticHolidays.size(); i++) {
if (TheStaticHolidays.at(i).IsToday(month,day)) {
isHolidayToday = true;
break;
}
}
for (unsigned int i = 0; i < TheDynamicHolidays.size(); i++) {
if (TheDynamicHolidays.at(i).IsToday(month,day,dayofweek)) {
isHolidayToday = true;
break;
}
}
return isHolidayToday;
}
#endif