author | alwin <alwin> | 2005-03-21 14:31:01 (UTC) |
---|---|---|
committer | alwin <alwin> | 2005-03-21 14:31:01 (UTC) |
commit | 5b2632bcae03b159d137fe9489eb2620c0f65f1a (patch) (unidiff) | |
tree | 7984efeeff1bba3083494b38378aaa223a82d117 /core/pim/datebook/holiday/national/nationalholiday.cpp | |
parent | 18825d8195f05d1a82affc0111f524371dbdeb5f (diff) | |
download | opie-5b2632bcae03b159d137fe9489eb2620c0f65f1a.zip opie-5b2632bcae03b159d137fe9489eb2620c0f65f1a.tar.gz opie-5b2632bcae03b159d137fe9489eb2620c0f65f1a.tar.bz2 |
design bug fixed.
xml-file now may contain floating holidays. syntax of it - see german.xml as
example and/or README file in OPIEDIR/etc/nationaldays/
Diffstat (limited to 'core/pim/datebook/holiday/national/nationalholiday.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/pim/datebook/holiday/national/nationalholiday.cpp | 155 |
1 files changed, 150 insertions, 5 deletions
diff --git a/core/pim/datebook/holiday/national/nationalholiday.cpp b/core/pim/datebook/holiday/national/nationalholiday.cpp index 52f3489..a7ab9a5 100644 --- a/core/pim/datebook/holiday/national/nationalholiday.cpp +++ b/core/pim/datebook/holiday/national/nationalholiday.cpp | |||
@@ -35,23 +35,163 @@ void NationalHoliday::load_days() | |||
35 | Config cfg("nationaldays"); | 35 | Config cfg("nationaldays"); |
36 | cfg.setGroup("entries"); | 36 | cfg.setGroup("entries"); |
37 | QStringList::ConstIterator it; | 37 | QStringList::ConstIterator it; |
38 | floatingDates.clear(); | ||
38 | NHcfg readit; | 39 | NHcfg readit; |
39 | for (it=files.begin();it!=files.end();++it) { | 40 | for (it=files.begin();it!=files.end();++it) { |
40 | if (!readit.load(QPEApplication::qpeDir()+"/etc/nationaldays/"+(*it))) { | 41 | if (!readit.load(QPEApplication::qpeDir()+"/etc/nationaldays/"+(*it))) { |
41 | continue; | 42 | continue; |
42 | } | 43 | } |
43 | tholidaylist::ConstIterator it; | ||
44 | for (it=readit.days().begin();it!=readit.days().end();++it) { | ||
45 | _days[it.key()]+=(it.data()); | ||
46 | } | ||
47 | 44 | ||
45 | tentrylist::ConstIterator it; | ||
46 | |||
47 | for (it=readit.fixDates().begin();it!=readit.fixDates().end();++it) { | ||
48 | _days[(*it).date()]+=(*it).name(); | ||
49 | } | ||
50 | floatingDates+=readit.floatingDates(); | ||
48 | } | 51 | } |
49 | init_done = true; | 52 | init_done = true; |
50 | } | 53 | } |
51 | 54 | ||
55 | void NationalHoliday::calc_easterDate() | ||
56 | { | ||
57 | unsigned long n = 0; | ||
58 | unsigned long p = 0; | ||
59 | |||
60 | if ( _lastyear > 1582 ) { | ||
61 | unsigned long a = _lastyear%19; | ||
62 | unsigned long b = _lastyear/100; | ||
63 | unsigned long c = _lastyear%100; | ||
64 | unsigned long d = b/4; | ||
65 | unsigned long e = b%4; | ||
66 | unsigned long f = (b+8)/25; | ||
67 | unsigned long g = (b+f+1)/3; | ||
68 | unsigned long h = (19*a+b-d-g+15)%30; | ||
69 | unsigned long i = c/4; | ||
70 | unsigned long j = c%4; | ||
71 | unsigned long k = j%100; | ||
72 | unsigned long l = (32+2*e+2*i-h-k)%7; | ||
73 | unsigned long m = (a+11*h+22*l)/451; | ||
74 | n = (h+l-7*m+114)/31; | ||
75 | p = (h+l-7*m+114)%31; | ||
76 | } else { | ||
77 | unsigned long a = _lastyear%4; | ||
78 | unsigned long b = _lastyear%7; | ||
79 | unsigned long c = _lastyear%19; | ||
80 | unsigned long d = (19*c+15)%30; | ||
81 | unsigned long e = (2*a+4*b-d+34)%7; | ||
82 | n = (d+e+114)/31; | ||
83 | p = (d+e+114)%31; | ||
84 | } | ||
85 | p++; | ||
86 | easterDate = QDate(_lastyear,n,p); | ||
87 | odebug << "Easterdate = " << easterDate << oendl; | ||
88 | } | ||
89 | |||
90 | |||
91 | void NationalHoliday::setyear(const QDate&aDate) | ||
92 | { | ||
93 | if (aDate.year()==_lastyear) return; | ||
94 | odebug << "calc year" << oendl; | ||
95 | _lastyear = aDate.year(); | ||
96 | _fdays.clear(); | ||
97 | calc_easterDate(); | ||
98 | tentrylist::ConstIterator it; | ||
99 | QDate tempdate,t; | ||
100 | |||
101 | for (it = floatingDates.begin();it!=floatingDates.end();++it) { | ||
102 | odebug << "Float day " << (*it).name() << oendl; | ||
103 | |||
104 | if ( (*it).date().year()==9999) { | ||
105 | tempdate = QDate(easterDate.year(),easterDate.month(),easterDate.day()); | ||
106 | } else { | ||
107 | tempdate = QDate(_lastyear,(*it).date().month(),(*it).date().day()); | ||
108 | } | ||
109 | |||
110 | odebug << "Start calc date: " << tempdate << oendl; | ||
111 | int weekday = weektonumber((*it).weekday()); | ||
112 | int mo; | ||
113 | |||
114 | if (weekday>0) { | ||
115 | /* specific weekday set */ | ||
116 | bool dir = true; | ||
117 | if (!(*it).daydep().isEmpty()) { | ||
118 | dir = ((*it).daydep()=="before"?true:false); | ||
119 | tempdate = movedateday(tempdate,weekday,dir); | ||
120 | } else if (!(*it).dayofmonth().isEmpty() && | ||
121 | (mo=monthtonumber((*it).month())) !=0 ) { | ||
122 | t = QDate(_lastyear,mo,1); | ||
123 | if ((*it).dayofmonth()=="last") { | ||
124 | int l = t.daysInMonth(); | ||
125 | tempdate.setYMD(_lastyear,mo,l); | ||
126 | tempdate = movedateday(tempdate,weekday,true); | ||
127 | } else { | ||
128 | tempdate = movedateday(t,weekday,false); | ||
129 | tempdate = tempdate.addDays(dayoftoint((*it).dayofmonth())); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | } | ||
134 | tempdate = tempdate.addDays((*it).offset()); | ||
135 | odebug << "Moved date to " << tempdate << oendl; | ||
136 | _fdays[tempdate]+=(*it).name(); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | int NationalHoliday::dayoftoint(const QString&d) | ||
141 | { | ||
142 | if (d=="first") return 0; | ||
143 | else if (d=="second") return 7; | ||
144 | else if (d=="third") return 14; | ||
145 | else if (d=="fourth") return 21; | ||
146 | return 0; | ||
147 | } | ||
148 | |||
149 | int NationalHoliday::monthtonumber(const QString&m) | ||
150 | { | ||
151 | if (m=="january")return 1; | ||
152 | else if (m=="february")return 2; | ||
153 | else if (m=="march") return 3; | ||
154 | else if (m=="april") return 4; | ||
155 | else if (m=="may") return 5; | ||
156 | else if (m=="june") return 6; | ||
157 | else if (m=="july") return 7; | ||
158 | else if (m=="august") return 8; | ||
159 | else if (m=="september") return 9; | ||
160 | else if (m=="october") return 10; | ||
161 | else if (m=="november") return 11; | ||
162 | else if (m=="december") return 12; | ||
163 | return 0; | ||
164 | } | ||
165 | |||
166 | QDate NationalHoliday::movedateday(const QDate&start,int weekday,bool direction) | ||
167 | { | ||
168 | QDate d = start; | ||
169 | if (weekday==0) return d; | ||
170 | int c = (direction?-1:1); | ||
171 | |||
172 | while (d.dayOfWeek()!=weekday) { | ||
173 | d = d.addDays(c); | ||
174 | } | ||
175 | return d; | ||
176 | } | ||
177 | |||
178 | int NationalHoliday::weektonumber(const QString&w) | ||
179 | { | ||
180 | if (w=="monday") return 1; | ||
181 | else if (w=="tuesday") return 2; | ||
182 | else if (w=="wednesday") return 3; | ||
183 | else if (w=="thursday" ) return 4; | ||
184 | else if (w=="friday") return 5; | ||
185 | else if (w=="saturday") return 6; | ||
186 | else if (w=="sunday") return 7; | ||
187 | return 0; | ||
188 | } | ||
189 | |||
52 | QStringList NationalHoliday::entries(const QDate&aDate) | 190 | QStringList NationalHoliday::entries(const QDate&aDate) |
53 | { | 191 | { |
54 | load_days(); | 192 | load_days(); |
193 | setyear(aDate); | ||
194 | |||
55 | QStringList ret; | 195 | QStringList ret; |
56 | QDate d(0,aDate.month(),aDate.day()); | 196 | QDate d(0,aDate.month(),aDate.day()); |
57 | 197 | ||
@@ -59,7 +199,11 @@ QStringList NationalHoliday::entries(const QDate&aDate) | |||
59 | if (it!=_days.end()) { | 199 | if (it!=_days.end()) { |
60 | ret+=*it; | 200 | ret+=*it; |
61 | } | 201 | } |
62 | 202 | QDate d2(_lastyear,d.month(),d.day()); | |
203 | it = _fdays.find(d2); | ||
204 | if (it!=_fdays.end()) { | ||
205 | ret+=*it; | ||
206 | } | ||
63 | return ret; | 207 | return ret; |
64 | } | 208 | } |
65 | 209 | ||
@@ -71,6 +215,7 @@ QStringList NationalHoliday::entries(unsigned year, unsigned month, unsigned day | |||
71 | QMap<QDate,QStringList> NationalHoliday::entries(const QDate&start,const QDate&end) | 215 | QMap<QDate,QStringList> NationalHoliday::entries(const QDate&start,const QDate&end) |
72 | { | 216 | { |
73 | load_days(); | 217 | load_days(); |
218 | setyear(start); | ||
74 | QMap<QDate,QStringList> ret; | 219 | QMap<QDate,QStringList> ret; |
75 | if (start==end) { | 220 | if (start==end) { |
76 | ret[start]=entries(start); | 221 | ret[start]=entries(start); |