| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 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. |
| 13 | |
| 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. |
| 16 | |
| 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. |
| 19 | */ |
| 20 | /** @file |
| 21 | * @brief time related functions. Week module |
| 22 | * |
| 23 | * @copyright Copyright © 2001-2009 by Intra2net AG |
| 24 | */ |
| 25 | |
| 26 | #ifndef __WEEK_HPP |
| 27 | #define __WEEK_HPP |
| 28 | |
| 29 | #include <string> |
| 30 | #include <bitset> |
| 31 | |
| 32 | namespace I2n { |
| 33 | namespace Time { |
| 34 | |
| 35 | /** |
| 36 | * @brief represents some days of a week. |
| 37 | * |
| 38 | */ |
| 39 | class Week |
| 40 | { |
| 41 | private: |
| 42 | /// Bitset storing the active days. Zero is Sunday. |
| 43 | std::bitset<7> Days; |
| 44 | /// Flag to indicate if this Week is valid |
| 45 | bool IsValid; |
| 46 | |
| 47 | public: |
| 48 | enum WeekDay { Su=0, Mo=1, Tu=2, We=3, Th=4, Fr=5, Sa=6, _WeekDay_END=7 }; |
| 49 | |
| 50 | Week(); |
| 51 | Week(const std::string& daystring); |
| 52 | Week(const std::bitset<7> &_days); |
| 53 | |
| 54 | bool is_valid() const; |
| 55 | |
| 56 | void clear(); |
| 57 | |
| 58 | operator std::bitset<7>() const |
| 59 | { return Days; } |
| 60 | |
| 61 | bool set(const std::string& daystring); |
| 62 | bool set(const WeekDay day, bool value=true); |
| 63 | |
| 64 | bool get(WeekDay day) const; |
| 65 | bool is_set(WeekDay day) const; |
| 66 | |
| 67 | bool all_set() const; |
| 68 | bool none_set() const; |
| 69 | |
| 70 | unsigned int days_till_set(WeekDay day) const; |
| 71 | unsigned int days_since_set(WeekDay day) const; |
| 72 | |
| 73 | std::string get_daystring() const; |
| 74 | std::string get_displaystring() const; |
| 75 | std::string get_netfilterstring() const; |
| 76 | |
| 77 | static std::string get_day_display(WeekDay day); |
| 78 | static std::string get_english_display(WeekDay day); |
| 79 | |
| 80 | // some operators for convenience: |
| 81 | |
| 82 | Week& operator&=(const Week& rhs) |
| 83 | { |
| 84 | Days &= rhs.Days; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | Week& operator|=(const Week& rhs) |
| 89 | { |
| 90 | Days |= rhs.Days; |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | Week& operator^=(const Week& rhs) |
| 95 | { |
| 96 | Days ^= rhs.Days; |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | bool operator==(const Week& rhs) const |
| 101 | { |
| 102 | return Days == rhs.Days; |
| 103 | } |
| 104 | |
| 105 | bool operator!=(const Week& rhs) const |
| 106 | { |
| 107 | return Days != rhs.Days; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * @brief delivers a week containing the days which are in both weeks. |
| 114 | * @param lhs first week. |
| 115 | * @param rhs second week. |
| 116 | * @return week which has only those days which are in both weeks. |
| 117 | */ |
| 118 | inline Week operator&(const Week& lhs, const Week& rhs) |
| 119 | { |
| 120 | I2n::Time::Week result(lhs); |
| 121 | return result &= rhs; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * @brief delivers a week containing the days which are at least in one of both weeks. |
| 127 | * @param lhs first week. |
| 128 | * @param rhs second week. |
| 129 | * @return week which has only those days which are at least in one of both weeks. |
| 130 | */ |
| 131 | inline Week operator|(const Week& lhs, const Week& rhs) |
| 132 | { |
| 133 | I2n::Time::Week result(lhs); |
| 134 | return result |= rhs; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * @brief delivers a week containing the days which are in only one of both weeks. |
| 140 | * @param lhs first week. |
| 141 | * @param rhs second week. |
| 142 | * @return week which has only those days which are in only one of both weeks. |
| 143 | */ |
| 144 | inline Week operator^(const Week& lhs, const Week& rhs) |
| 145 | { |
| 146 | I2n::Time::Week result(lhs); |
| 147 | return result ^= rhs; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #endif |