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