2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
34 * @param daystring Active days as string. 0 is Sunday. Call is_sane() afterwards.
36 Week::Week(const std::string& daystring)
38 IsValid = set(daystring);
43 * @param days Active days as bitset. 0 is Sunday
45 Week::Week(const std::bitset<7> &days)
60 * Check if this Week object is valid, f.e. after set().
61 * @return True if valid, false otherwise
63 bool Week::is_valid() const
69 * Clear all active days
78 * Set active days as string. Clears unset values.
79 * Active days will remain untouched if there's an error
80 * which you can query via is_valid().
81 * @param daystring Active days as string
82 * @return True if all is fine, false otherwise
84 bool Week::set(const std::string& daystring)
89 string::size_type len=daystring.length();
90 for (string::size_type p=0; p < len; p++)
97 if (!(c >> wnr) || wnr > 6)
105 // Atomic switch-over
112 * Set or disable one weekday.
113 * @param day Day to modify
114 * @param value True for active, false to disable
115 * @return True if all is fine, false otherwise
117 bool Week::set(const WeekDay day, bool value)
119 if (day >= _WeekDay_END)
125 Days.set(static_cast<size_t>(day), value);
131 * Check if a particular day is active
132 * @param day Day to check
133 * @return True if day is active, false otherwise
135 bool Week::get(WeekDay day) const
137 return Days.test(static_cast<size_t>(day));
141 * Check if a particular day is active
142 * @param day Day to check
143 * @return True if day is active, false otherwise
145 bool Week::is_set(WeekDay day) const
147 return Days.test(static_cast<size_t>(day));
151 * Check if all days are active
152 * @return True if all days are active, false otherwise
154 bool Week::all_set() const
156 return (Days.count()==7);
160 * Check if no days is active
161 * @return True if no day is active, false otherwise
163 bool Week::none_set() const
169 @brief returns the number of days till the next weekday which is set
170 @param start weekday to start checking
171 @note returns 0 if the start-day is set
173 unsigned int Week::days_till_set(WeekDay start) const
178 for (unsigned int days=0; days < 8; days++)
180 unsigned int check=start+days;
183 if (is_set(static_cast<WeekDay>(check)))
187 throw logic_error("can't find next weekday");
190 return 0; //lint !e527
194 @brief returns the number of days since the previous weekday which is set
195 @param start weekday to start checking
196 @note returns 0 if the start-day is set
198 unsigned int Week::days_since_set(WeekDay start) const
203 for (unsigned int days=0; days < 8; days++)
205 int check=start-static_cast<int>(days);
208 if (is_set(static_cast<WeekDay>(check)))
212 throw logic_error("can't find next weekday");
215 return 0; //lint !e527
219 * Get days as number string compatible for set() like "345".
220 * @return Days as string
222 std::string Week::get_daystring() const
226 for (unsigned int i = 0; i < 7; i++)
234 * Get days as string presentable to the user
235 * @return Days as string
237 std::string Week::get_displaystring() const
241 // We want to display from Monday to Sunday so reorder
242 // the bitset and make monday bit 0 and sunday bit 6.
243 bitset<7> reordered = Days >> 1;
244 reordered.set(6, Days[0]);
246 for (unsigned int i = 0; i < 7; ++i)
249 if (!reordered.test(i))
252 if (!weekdays_str.empty())
253 weekdays_str += ", ";
255 // get day string (revert the reorder)
256 weekdays_str += get_day_display(i == 6 ? Su : static_cast<WeekDay>(i+1));
258 // check if we can group two or more days
259 unsigned int group_days = i;
260 while (group_days < 6 && reordered.test(group_days+1))
263 // Were we able to group two or more days?
264 if (group_days > i+1)
267 weekdays_str += "-" + get_day_display(i == 6 ? Su : static_cast<WeekDay>(i+1));
275 * Get days as string suitable for netfilter rules
278 std::string Week::get_netfilterstring() const
282 for (unsigned int i = 0; i < 7; i++)
287 out+=get_english_display(static_cast<WeekDay>(i));;
294 * Convert WeekDay to readable string
295 * @param day WeekDay to convert
296 * @return Weekday as string
298 std::string Week::get_day_display(WeekDay day)
305 weekday_str = i18n("Mon");
308 weekday_str = i18n("Tue");
311 weekday_str = i18n("Wed");
314 weekday_str = i18n("Thu");
317 weekday_str = i18n("Fri");
320 weekday_str = i18n("Sat");
323 weekday_str = i18n("Sun");
327 } //lint !e788: Don't complain about unused _WeekDay_END
333 * Convert WeekDay to readable string without i18n
334 * @param day WeekDay to convert
335 * @return WeekDay as string in English
337 std::string Week::get_english_display(WeekDay day)
366 } //lint !e788: Don't complain about unused _WeekDay_END