summaryrefslogtreecommitdiff
path: root/kcalcore/recurrencerule.cpp
blob: 40f63c1fe0df8ad3a1a158a354a0232d654d18b5 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
/*
  This file is part of the kcalcore library.

  Copyright (c) 2005 Reinhold Kainhofer <reinhold@kainhofe.com>
  Copyright (c) 2006-2008 David Jarvie <djarvie@kde.org>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/
#include "recurrencerule.h"

#include <KDebug>

#include <QtCore/QStringList>
#include <QtCore/QTime>

using namespace KCalCore;

// Maximum number of intervals to process
const int LOOP_LIMIT = 10000;

static QString dumpTime(const KDateTime &dt);     // for debugging

/*=========================================================================
=                                                                         =
= IMPORTANT CODING NOTE:                                                  =
=                                                                         =
= Recurrence handling code is time critical, especially for sub-daily     =
= recurrences. For example, if getNextDate() is called repeatedly to      =
= check all consecutive occurrences over a few years, on a slow machine   =
= this could take many seconds to complete in the worst case. Simple      =
= sub-daily recurrences are optimised by use of mTimedRepetition.         =
=                                                                         =
==========================================================================*/

/**************************************************************************
 *                               DateHelper                               *
 **************************************************************************/
//@cond PRIVATE
class DateHelper
{
public:
#ifndef NDEBUG
    static QString dayName(short day);
#endif
    static QDate getNthWeek(int year, int weeknumber, short weekstart = 1);
    static int weekNumbersInYear(int year, short weekstart = 1);
    static int getWeekNumber(const QDate &date, short weekstart, int *year = 0);
    static int getWeekNumberNeg(const QDate &date, short weekstart, int *year = 0);
    // Convert to QDate, allowing for day < 0.
    // month and day must be non-zero.
    static QDate getDate(int year, int month, int day)
    {
        if (day >= 0) {
            return QDate(year, month, day);
        } else {
            if (++month > 12) {
                month = 1;
                ++year;
            }
            return QDate(year, month, 1).addDays(day);
        }
    }
};

#ifndef NDEBUG
// TODO: Move to a general library / class, as we need the same in the iCal
//       generator and in the xcal format
QString DateHelper::dayName(short day)
{
    switch (day) {
    case 1:
        return QLatin1String("MO");
    case 2:
        return QLatin1String("TU");
    case 3:
        return QLatin1String("WE");
    case 4:
        return QLatin1String("TH");
    case 5:
        return QLatin1String("FR");
    case 6:
        return QLatin1String("SA");
    case 7:
        return QLatin1String("SU");
    default:
        return QLatin1String("??");
    }
}
#endif

QDate DateHelper::getNthWeek(int year, int weeknumber, short weekstart)
{
    if (weeknumber == 0) {
        return QDate();
    }

    // Adjust this to the first day of week #1 of the year and add 7*weekno days.
    QDate dt(year, 1, 4);   // Week #1 is the week that contains Jan 4
    int adjust = -(7 + dt.dayOfWeek() - weekstart) % 7;
    if (weeknumber > 0) {
        dt = dt.addDays(7 * (weeknumber-1) + adjust);
    } else if (weeknumber < 0) {
        dt = dt.addYears(1);
        dt = dt.addDays(7 * weeknumber + adjust);
    }
    return dt;
}

int DateHelper::getWeekNumber(const QDate &date, short weekstart, int *year)
{
    int y = date.year();
    QDate dt(y, 1, 4);   // <= definitely in week #1
    dt = dt.addDays(-(7 + dt.dayOfWeek() - weekstart) % 7);     // begin of week #1

    int daysto = dt.daysTo(date);
    if (daysto < 0) {
        // in first week of year
        --y;
        dt = QDate(y, 1, 4);
        dt = dt.addDays(-(7 + dt.dayOfWeek() - weekstart) % 7);     // begin of week #1
        daysto = dt.daysTo(date);
    } else if (daysto > 355) {
        // near the end of the year - check if it's next year
        QDate dtn(y+1, 1, 4);   // <= definitely first week of next year
        dtn = dtn.addDays(-(7 + dtn.dayOfWeek() - weekstart) % 7);
        int dayston = dtn.daysTo(date);
        if (dayston >= 0) {
            // in first week of next year;
            ++y;
            daysto = dayston;
        }
    }
    if (year) {
        *year = y;
    }
    return daysto / 7 + 1;
}

int DateHelper::weekNumbersInYear(int year, short weekstart)
{
    QDate dt(year, 1, weekstart);
    QDate dt1(year + 1, 1, weekstart);
    return dt.daysTo(dt1) / 7;
}

// Week number from the end of the year
int DateHelper::getWeekNumberNeg(const QDate &date, short weekstart, int *year)
{
    int weekpos = getWeekNumber(date, weekstart, year);
    return weekNumbersInYear(*year, weekstart) - weekpos - 1;
}
//@endcond

/**************************************************************************
 *                               WDayPos                                  *
 **************************************************************************/

bool RecurrenceRule::WDayPos::operator==(const RecurrenceRule::WDayPos &pos2) const
{
    return mDay == pos2.mDay && mPos == pos2.mPos;
}

bool RecurrenceRule::WDayPos::operator!=(const RecurrenceRule::WDayPos &pos2) const
{
    return !operator==(pos2);
}

/**************************************************************************
 *                               Constraint                               *
 **************************************************************************/
//@cond PRIVATE
class Constraint
{
public:
    typedef QList<Constraint> List;

    Constraint() {}
    explicit Constraint(KDateTime::Spec, int wkst = 1);
    Constraint(const KDateTime &dt, RecurrenceRule::PeriodType type, int wkst);
    void clear();
    void setYear(int n)
    {
        year = n;
        useCachedDt = false;
    }
    void setMonth(int n)
    {
        month = n;
        useCachedDt = false;
    }
    void setDay(int n)
    {
        day = n;
        useCachedDt = false;
    }
    void setHour(int n)
    {
        hour = n;
        useCachedDt = false;
    }
    void setMinute(int n)
    {
        minute = n;
        useCachedDt = false;
    }
    void setSecond(int n)
    {
        second = n;
        useCachedDt = false;
    }
    void setWeekday(int n)
    {
        weekday = n;
        useCachedDt = false;
    }
    void setWeekdaynr(int n)
    {
        weekdaynr = n;
        useCachedDt = false;
    }
    void setWeeknumber(int n)
    {
        weeknumber = n;
        useCachedDt = false;
    }
    void setYearday(int n)
    {
        yearday = n;
        useCachedDt = false;
    }
    void setWeekstart(int n)
    {
        weekstart = n;
        useCachedDt = false;
    }
    void setSecondOccurrence(int n)
    {
        secondOccurrence = n;
        useCachedDt = false;
    }

    int year;       // 0 means unspecified
    int month;      // 0 means unspecified
    int day;        // 0 means unspecified
    int hour;       // -1 means unspecified
    int minute;     // -1 means unspecified
    int second;     // -1 means unspecified
    int weekday;    //  0 means unspecified
    int weekdaynr;  // index of weekday in month/year (0=unspecified)
    int weeknumber; //  0 means unspecified
    int yearday;    //  0 means unspecified
    int weekstart;  //  first day of week (1=monday, 7=sunday, 0=unspec.)
    KDateTime::Spec timespec;   // time zone etc. to use
    bool secondOccurrence;  // the time is the second occurrence during daylight savings shift

    bool readDateTime(const KDateTime &dt, RecurrenceRule::PeriodType type);
    bool matches(const QDate &dt, RecurrenceRule::PeriodType type) const;
    bool matches(const KDateTime &dt, RecurrenceRule::PeriodType type) const;
    bool merge(const Constraint &interval);
    bool isConsistent() const;
    bool isConsistent(RecurrenceRule::PeriodType period) const;
    bool increase(RecurrenceRule::PeriodType type, int freq);
    KDateTime intervalDateTime(RecurrenceRule::PeriodType type) const;
    QList<KDateTime> dateTimes(RecurrenceRule::PeriodType type) const;
    void appendDateTime(const QDate &date, const QTime &time, QList<KDateTime> &list) const;
    void dump() const;

private:
    mutable bool useCachedDt;
    mutable KDateTime cachedDt;
};

Constraint::Constraint(KDateTime::Spec spec, int wkst)
    : weekstart(wkst),
      timespec(spec)
{
    clear();
}

Constraint::Constraint(const KDateTime &dt, RecurrenceRule::PeriodType type, int wkst)
    : weekstart(wkst),
      timespec(dt.timeSpec())
{
    clear();
    readDateTime(dt, type);
}

void Constraint::clear()
{
    year = 0;
    month = 0;
    day = 0;
    hour = -1;
    minute = -1;
    second = -1;
    weekday = 0;
    weekdaynr = 0;
    weeknumber = 0;
    yearday = 0;
    secondOccurrence = false;
    useCachedDt = false;
}

bool Constraint::matches(const QDate &dt, RecurrenceRule::PeriodType type) const
{
    // If the event recurs in week 53 or 1, the day might not belong to the same
    // year as the week it is in. E.g. Jan 1, 2005 is in week 53 of year 2004.
    // So we can't simply check the year in that case!
    if (weeknumber == 0) {
        if (year > 0 && year != dt.year()) {
            return false;
        }
    } else {
        int y;
        if (weeknumber > 0 &&
                weeknumber != DateHelper::getWeekNumber(dt, weekstart, &y)) {
            return false;
        }
        if (weeknumber < 0 &&
                weeknumber != DateHelper::getWeekNumberNeg(dt, weekstart, &y)) {
            return false;
        }
        if (year > 0 && year != y) {
            return false;
        }
    }

    if (month > 0 && month != dt.month()) {
        return false;
    }
    if (day > 0 && day != dt.day()) {
        return false;
    }
    if (day < 0 && dt.day() != (dt.daysInMonth() + day + 1)) {
        return false;
    }
    if (weekday > 0) {
        if (weekday != dt.dayOfWeek()) {
            return false;
        }
        if (weekdaynr != 0) {
            // If it's a yearly recurrence and a month is given, the position is
            // still in the month, not in the year.
            if ((type == RecurrenceRule::rMonthly) ||
                    (type == RecurrenceRule::rYearly && month > 0)) {
                // Monthly
                if (weekdaynr > 0 &&
                        weekdaynr != (dt.day() - 1) / 7 + 1) {
                    return false;
                }
                if (weekdaynr < 0 &&
                        weekdaynr != -((dt.daysInMonth() - dt.day()) / 7 + 1)) {
                    return false;
                }
            } else {
                // Yearly
                if (weekdaynr > 0 &&
                        weekdaynr != (dt.dayOfYear() - 1) / 7 + 1) {
                    return false;
                }
                if (weekdaynr < 0 &&
                        weekdaynr != -((dt.daysInYear() - dt.dayOfYear()) / 7 + 1)) {
                    return false;
                }
            }
        }
    }
    if (yearday > 0 && yearday != dt.dayOfYear()) {
        return false;
    }
    if (yearday < 0 && yearday != dt.daysInYear() - dt.dayOfYear() + 1) {
        return false;
    }
    return true;
}

/* Check for a match with the specified date/time.
 * The date/time's time specification must correspond with that of the start date/time.
 */
bool Constraint::matches(const KDateTime &dt, RecurrenceRule::PeriodType type) const
{
    if ((hour >= 0 && (hour != dt.time().hour() ||
                       secondOccurrence != dt.isSecondOccurrence())) ||
            (minute >= 0 && minute != dt.time().minute()) ||
            (second >= 0 && second != dt.time().second()) ||
            !matches(dt.date(), type)) {
        return false;
    }
    return true;
}

bool Constraint::isConsistent(RecurrenceRule::PeriodType /*period*/) const
{
    // TODO: Check for consistency, e.g. byyearday=3 and bymonth=10
    return true;
}

// Return a date/time set to the constraint values, but with those parts less
// significant than the given period type set to 1 (for dates) or 0 (for times).
KDateTime Constraint::intervalDateTime(RecurrenceRule::PeriodType type) const
{
    if (useCachedDt) {
        return cachedDt;
    }
    QDate d;
    QTime t(0, 0, 0);
    bool subdaily = true;
    switch (type) {
    case RecurrenceRule::rSecondly:
        t.setHMS(hour, minute, second);
        break;
    case RecurrenceRule::rMinutely:
        t.setHMS(hour, minute, 0);
        break;
    case RecurrenceRule::rHourly:
        t.setHMS(hour, 0, 0);
        break;
    case RecurrenceRule::rDaily:
        break;
    case RecurrenceRule::rWeekly:
        d = DateHelper::getNthWeek(year, weeknumber, weekstart);
        subdaily = false;
        break;
    case RecurrenceRule::rMonthly:
        d.setYMD(year, month, 1);
        subdaily = false;
        break;
    case RecurrenceRule::rYearly:
        d.setYMD(year, 1, 1);
        subdaily = false;
        break;
    default:
        break;
    }
    if (subdaily) {
        d = DateHelper::getDate(year, (month>0)?month:1, day?day:1);
    }
    cachedDt = KDateTime(d, t, timespec);
    if (secondOccurrence) {
        cachedDt.setSecondOccurrence(true);
    }
    useCachedDt = true;
    return cachedDt;
}

bool Constraint::merge(const Constraint &interval)
{
#define mergeConstraint( name, cmparison ) \
  if ( interval.name cmparison ) { \
    if ( !( name cmparison ) ) { \
      name = interval.name; \
    } else if ( name != interval.name ) { \
      return false;\
    } \
  }

    useCachedDt = false;

    mergeConstraint(year, > 0);
    mergeConstraint(month, > 0);
    mergeConstraint(day, != 0);
    mergeConstraint(hour, >= 0);
    mergeConstraint(minute, >= 0);
    mergeConstraint(second, >= 0);

    mergeConstraint(weekday, != 0);
    mergeConstraint(weekdaynr, != 0);
    mergeConstraint(weeknumber, != 0);
    mergeConstraint(yearday, != 0);

#undef mergeConstraint
    return true;
}

//           Y  M  D | H  Mn S | WD #WD | WN | YD
// required:
//           x       | x  x  x |        |    |
// 0) Trivial: Exact date given, maybe other restrictions
//           x  x  x | x  x  x |        |    |
// 1) Easy case: no weekly restrictions -> at most a loop through possible dates
//           x  +  + | x  x  x |  -  -  |  - |  -
// 2) Year day is given -> date known
//           x       | x  x  x |        |    |  +
// 3) week number is given -> loop through all days of that week. Further
//    restrictions will be applied in the end, when we check all dates for
//    consistency with the constraints
//           x       | x  x  x |        |  + | (-)
// 4) week day is specified ->
//           x       | x  x  x |  x  ?  | (-)| (-)
// 5) All possiblecases have already been treated, so this must be an error!

QList<KDateTime> Constraint::dateTimes(RecurrenceRule::PeriodType type) const
{
    QList<KDateTime> result;
    bool done = false;
    if (!isConsistent(type)) {
        return result;
    }

    // TODO_Recurrence: Handle all-day
    QTime tm(hour, minute, second);

    if (!done && day && month > 0) {
        appendDateTime(DateHelper::getDate(year, month, day), tm, result);
        done = true;
    }

    if (!done && weekday == 0 && weeknumber == 0 && yearday == 0) {
        // Easy case: date is given, not restrictions by week or yearday
        uint mstart = (month > 0) ? month : 1;
        uint mend = (month <= 0) ? 12 : month;
        for (uint m = mstart; m <= mend; ++m) {
            uint dstart, dend;
            if (day > 0) {
                dstart = dend = day;
            } else if (day < 0) {
                QDate date(year, month, 1);
                dstart = dend = date.daysInMonth() + day + 1;
            } else {
                QDate date(year, month, 1);
                dstart = 1;
                dend = date.daysInMonth();
            }
            uint d = dstart;
            for (QDate dt(year, m, dstart); ; dt = dt.addDays(1)) {
                appendDateTime(dt, tm, result);
                if (++d > dend) {
                    break;
                }
            }
        }
        done = true;
    }

    // Else: At least one of the week / yearday restrictions was given...
    // If we have a yearday (and of course a year), we know the exact date
    if (!done && yearday != 0) {
        // yearday < 0 means from end of year, so we'll need Jan 1 of the next year
        QDate d(year + ((yearday > 0) ? 0 : 1), 1, 1);
        d = d.addDays(yearday - ((yearday > 0) ? 1 : 0));
        appendDateTime(d, tm, result);
        done = true;
    }

    // Else: If we have a weeknumber, we have at most 7 possible dates, loop through them
    if (!done && weeknumber != 0) {
        QDate wst(DateHelper::getNthWeek(year, weeknumber, weekstart));
        if (weekday != 0) {
            wst = wst.addDays((7 + weekday - weekstart) % 7);
            appendDateTime(wst, tm, result);
        } else {
            for (int i = 0; i < 7; ++i) {
                appendDateTime(wst, tm, result);
                wst = wst.addDays(1);
            }
        }
        done = true;
    }

    // weekday is given
    if (!done && weekday != 0) {
        QDate dt(year, 1, 1);
        // If type == yearly and month is given, pos is still in month not year!
        // TODO_Recurrence: Correct handling of n-th  BYDAY...
        int maxloop = 53;
        bool inMonth = (type == RecurrenceRule::rMonthly) ||
                       (type == RecurrenceRule::rYearly && month > 0);
        if (inMonth && month > 0) {
            dt = QDate(year, month, 1);
            maxloop = 5;
        }
        if (weekdaynr < 0) {
            // From end of period (month, year) => relative to begin of next period
            if (inMonth) {
                dt = dt.addMonths(1);
            } else {
                dt = dt.addYears(1);
            }
        }
        int adj = (7 + weekday - dt.dayOfWeek()) % 7;
        dt = dt.addDays(adj);   // correct first weekday of the period

        if (weekdaynr > 0) {
            dt = dt.addDays((weekdaynr - 1) * 7);
            appendDateTime(dt, tm, result);
        } else if (weekdaynr < 0) {
            dt = dt.addDays(weekdaynr * 7);
            appendDateTime(dt, tm, result);
        } else {
            // loop through all possible weeks, non-matching will be filtered later
            for (int i = 0; i < maxloop; ++i) {
                appendDateTime(dt, tm, result);
                dt = dt.addDays(7);
            }
        }
    } // weekday != 0

    // Only use those times that really match all other constraints, too
    QList<KDateTime> valid;
    for (int i = 0, iend = result.count();  i < iend;  ++i) {
        if (matches(result[i], type)) {
            valid.append(result[i]);
        }
    }
    // Don't sort it here, would be unnecessary work. The results from all
    // constraints will be merged to one big list of the interval. Sort that one!
    return valid;
}

void Constraint::appendDateTime(const QDate &date, const QTime &time,
                                QList<KDateTime> &list) const
{
    KDateTime dt(date, time, timespec);
    if (dt.isValid()) {
        if (secondOccurrence) {
            dt.setSecondOccurrence(true);
        }
        list.append(dt);
    }
}

bool Constraint::increase(RecurrenceRule::PeriodType type, int freq)
{
    // convert the first day of the interval to KDateTime
    intervalDateTime(type);

    // Now add the intervals
    switch (type) {
    case RecurrenceRule::rSecondly:
        cachedDt = cachedDt.addSecs(freq);
        break;
    case RecurrenceRule::rMinutely:
        cachedDt = cachedDt.addSecs(60 * freq);
        break;
    case RecurrenceRule::rHourly:
        cachedDt = cachedDt.addSecs(3600 * freq);
        break;
    case RecurrenceRule::rDaily:
        cachedDt = cachedDt.addDays(freq);
        break;
    case RecurrenceRule::rWeekly:
        cachedDt = cachedDt.addDays(7 * freq);
        break;
    case RecurrenceRule::rMonthly:
        cachedDt = cachedDt.addMonths(freq);
        break;
    case RecurrenceRule::rYearly:
        cachedDt = cachedDt.addYears(freq);
        break;
    default:
        break;
    }
    // Convert back from KDateTime to the Constraint class
    readDateTime(cachedDt, type);
    useCachedDt = true;   // readDateTime() resets this

    return true;
}

// Set the constraint's value appropriate to 'type', to the value contained in a date/time.
bool Constraint::readDateTime(const KDateTime &dt, RecurrenceRule::PeriodType type)
{
    switch (type) {
        // Really fall through! Only weekly needs to be treated differently!
    case RecurrenceRule::rSecondly:
        second = dt.time().second();
    case RecurrenceRule::rMinutely:
        minute = dt.time().minute();
    case RecurrenceRule::rHourly:
        hour = dt.time().hour();
        secondOccurrence = dt.isSecondOccurrence();
    case RecurrenceRule::rDaily:
        day = dt.date().day();
    case RecurrenceRule::rMonthly:
        month = dt.date().month();
    case RecurrenceRule::rYearly:
        year = dt.date().year();
        break;
    case RecurrenceRule::rWeekly:
        // Determine start day of the current week, calculate the week number from that
        weeknumber = DateHelper::getWeekNumber(dt.date(), weekstart, &year);
        break;
    default:
        break;
    }
    useCachedDt = false;
    return true;
}
//@endcond

/**************************************************************************
 *                        RecurrenceRule::Private                         *
 **************************************************************************/

//@cond PRIVATE
class KCalCore::RecurrenceRule::Private
{
public:
    Private(RecurrenceRule *parent)
        : mParent(parent),
          mPeriod(rNone),
          mFrequency(0),
          mDuration(-1),
          mWeekStart(1),
          mIsReadOnly(false),
          mAllDay(false)
    {
        setDirty();
    }

    Private(RecurrenceRule *parent, const Private &p);

    Private &operator=(const Private &other);
    bool operator==(const Private &other) const;
    void clear();
    void setDirty();
    void buildConstraints();
    bool buildCache() const;
    Constraint getNextValidDateInterval(const KDateTime &preDate, PeriodType type) const;
    Constraint getPreviousValidDateInterval(const KDateTime &afterDate, PeriodType type) const;
    DateTimeList datesForInterval(const Constraint &interval, PeriodType type) const;

    RecurrenceRule *mParent;
    QString mRRule;            // RRULE string
    PeriodType mPeriod;
    KDateTime mDateStart;      // start of recurrence (but mDateStart is not an occurrence
    // unless it matches the rule)
    uint mFrequency;
    /** how often it recurs:
           < 0 means no end date,
           0 means an explicit end date,
           positive values give the number of occurrences */
    int mDuration;
    KDateTime mDateEnd;

    QList<int> mBySeconds;     // values: second 0-59
    QList<int> mByMinutes;     // values: minute 0-59
    QList<int> mByHours;       // values: hour 0-23

    QList<WDayPos> mByDays;   // n-th weekday of the month or year
    QList<int> mByMonthDays;   // values: day -31 to -1 and 1-31
    QList<int> mByYearDays;    // values: day -366 to -1 and 1-366
    QList<int> mByWeekNumbers; // values: week -53 to -1 and 1-53
    QList<int> mByMonths;      // values: month 1-12
    QList<int> mBySetPos;      // values: position -366 to -1 and 1-366
    short mWeekStart;               // first day of the week (1=Monday, 7=Sunday)

    Constraint::List mConstraints;
    QList<RuleObserver*> mObservers;

    // Cache for duration
    mutable DateTimeList mCachedDates;
    mutable KDateTime mCachedDateEnd;
    mutable KDateTime mCachedLastDate;   // when mCachedDateEnd invalid, last date checked
    mutable bool mCached;

    bool mIsReadOnly;
    bool mAllDay;
    bool mNoByRules;        // no BySeconds, ByMinutes, ... rules exist
    uint mTimedRepetition;  // repeats at a regular number of seconds interval, or 0
};

RecurrenceRule::Private::Private(RecurrenceRule *parent, const Private &p)
    : mParent(parent),
      mRRule(p.mRRule),
      mPeriod(p.mPeriod),
      mDateStart(p.mDateStart),
      mFrequency(p.mFrequency),
      mDuration(p.mDuration),
      mDateEnd(p.mDateEnd),

      mBySeconds(p.mBySeconds),
      mByMinutes(p.mByMinutes),
      mByHours(p.mByHours),
      mByDays(p.mByDays),
      mByMonthDays(p.mByMonthDays),
      mByYearDays(p.mByYearDays),
      mByWeekNumbers(p.mByWeekNumbers),
      mByMonths(p.mByMonths),
      mBySetPos(p.mBySetPos),
      mWeekStart(p.mWeekStart),

      mIsReadOnly(p.mIsReadOnly),
      mAllDay(p.mAllDay),
      mNoByRules(p.mNoByRules)
{
    setDirty();
}

RecurrenceRule::Private &RecurrenceRule::Private::operator=(const Private &p)
{
    // check for self assignment
    if (&p == this) {
        return *this;
    }

    mRRule = p.mRRule;
    mPeriod = p.mPeriod;
    mDateStart = p.mDateStart;
    mFrequency = p.mFrequency;
    mDuration = p.mDuration;
    mDateEnd = p.mDateEnd;

    mBySeconds = p.mBySeconds;
    mByMinutes = p.mByMinutes;
    mByHours = p.mByHours;
    mByDays = p.mByDays;
    mByMonthDays = p.mByMonthDays;
    mByYearDays = p.mByYearDays;
    mByWeekNumbers = p.mByWeekNumbers;
    mByMonths = p.mByMonths;
    mBySetPos = p.mBySetPos;
    mWeekStart = p.mWeekStart;

    mIsReadOnly = p.mIsReadOnly;
    mAllDay = p.mAllDay;
    mNoByRules = p.mNoByRules;

    setDirty();

    return *this;
}

bool RecurrenceRule::Private::operator==(const Private &r) const
{
    return
        mPeriod == r.mPeriod &&
        ((mDateStart == r.mDateStart) ||
         (!mDateStart.isValid() && !r.mDateStart.isValid())) &&
        mDuration == r.mDuration &&
        ((mDateEnd == r.mDateEnd) ||
         (!mDateEnd.isValid() && !r.mDateEnd.isValid())) &&
        mFrequency == r.mFrequency &&
        mIsReadOnly == r.mIsReadOnly &&
        mAllDay == r.mAllDay &&
        mBySeconds == r.mBySeconds &&
        mByMinutes == r.mByMinutes &&
        mByHours == r.mByHours &&
        mByDays == r.mByDays &&
        mByMonthDays == r.mByMonthDays &&
        mByYearDays == r.mByYearDays &&
        mByWeekNumbers == r.mByWeekNumbers &&
        mByMonths == r.mByMonths &&
        mBySetPos == r.mBySetPos &&
        mWeekStart == r.mWeekStart &&
        mNoByRules == r.mNoByRules;
}

void RecurrenceRule::Private::clear()
{
    if (mIsReadOnly) {
        return;
    }
    mPeriod = rNone;
    mBySeconds.clear();
    mByMinutes.clear();
    mByHours.clear();
    mByDays.clear();
    mByMonthDays.clear();
    mByYearDays.clear();
    mByWeekNumbers.clear();
    mByMonths.clear();
    mBySetPos.clear();
    mWeekStart = 1;
    mNoByRules = false;

    setDirty();
}

void RecurrenceRule::Private::setDirty()
{
    buildConstraints();
    mCached = false;
    mCachedDates.clear();
    for (int i = 0, iend = mObservers.count();  i < iend;  ++i) {
        if (mObservers[i]) {
            mObservers[i]->recurrenceChanged(mParent);
        }
    }
}
//@endcond

/**************************************************************************
 *                              RecurrenceRule                            *
 **************************************************************************/

RecurrenceRule::RecurrenceRule()
    : d(new Private(this))
{
}

RecurrenceRule::RecurrenceRule(const RecurrenceRule &r)
    : d(new Private(this, *r.d))
{
}

RecurrenceRule::~RecurrenceRule()
{
    delete d;
}

bool RecurrenceRule::operator==(const RecurrenceRule &r) const
{
    return *d == *r.d;
}

RecurrenceRule &RecurrenceRule::operator=(const RecurrenceRule &r)
{
    // check for self assignment
    if (&r == this) {
        return *this;
    }

    *d = *r.d;

    return *this;
}

void RecurrenceRule::addObserver(RuleObserver *observer)
{
    if (!d->mObservers.contains(observer)) {
        d->mObservers.append(observer);
    }
}

void RecurrenceRule::removeObserver(RuleObserver *observer)
{
    if (d->mObservers.contains(observer)) {
        d->mObservers.removeAll(observer);
    }
}

void RecurrenceRule::setRecurrenceType(PeriodType period)
{
    if (isReadOnly()) {
        return;
    }
    d->mPeriod = period;
    d->setDirty();
}

KDateTime RecurrenceRule::endDt(bool *result) const
{
    if (result) {
        *result = false;
    }
    if (d->mPeriod == rNone) {
        return KDateTime();
    }
    if (d->mDuration < 0) {
        return KDateTime();
    }
    if (d->mDuration == 0) {
        if (result) {
            *result = true;
        }
        return d->mDateEnd;
    }

    // N occurrences. Check if we have a full cache. If so, return the cached end date.
    if (!d->mCached) {
        // If not enough occurrences can be found (i.e. inconsistent constraints)
        if (!d->buildCache()) {
            return KDateTime();
        }
    }
    if (result) {
        *result = true;
    }
    return d->mCachedDateEnd;
}

void RecurrenceRule::setEndDt(const KDateTime &dateTime)
{
    if (isReadOnly()) {
        return;
    }
    d->mDateEnd = dateTime;
    d->mDuration = 0; // set to 0 because there is an end date/time
    d->setDirty();
}

void RecurrenceRule::setDuration(int duration)
{
    if (isReadOnly()) {
        return;
    }
    d->mDuration = duration;
    d->setDirty();
}

void RecurrenceRule::setAllDay(bool allDay)
{
    if (isReadOnly()) {
        return;
    }
    d->mAllDay = allDay;
    d->setDirty();
}

void RecurrenceRule::clear()
{
    d->clear();
}

void RecurrenceRule::setDirty()
{
    d->setDirty();
}

void RecurrenceRule::setStartDt(const KDateTime &start)
{
    if (isReadOnly()) {
        return;
    }
    d->mDateStart = start;
    d->setDirty();
}

void RecurrenceRule::setFrequency(int freq)
{
    if (isReadOnly() || freq <= 0) {
        return;
    }
    d->mFrequency = freq;
    d->setDirty();
}

void RecurrenceRule::setBySeconds(const QList<int> &bySeconds)
{
    if (isReadOnly()) {
        return;
    }
    d->mBySeconds = bySeconds;
    d->setDirty();
}

void RecurrenceRule::setByMinutes(const QList<int> &byMinutes)
{
    if (isReadOnly()) {
        return;
    }
    d->mByMinutes = byMinutes;
    d->setDirty();
}

void RecurrenceRule::setByHours(const QList<int> &byHours)
{
    if (isReadOnly()) {
        return;
    }
    d->mByHours = byHours;
    d->setDirty();
}

void RecurrenceRule::setByDays(const QList<WDayPos> &byDays)
{
    if (isReadOnly()) {
        return;
    }
    d->mByDays = byDays;
    d->setDirty();
}

void RecurrenceRule::setByMonthDays(const QList<int> &byMonthDays)
{
    if (isReadOnly()) {
        return;
    }
    d->mByMonthDays = byMonthDays;
    d->setDirty();
}

void RecurrenceRule::setByYearDays(const QList<int> &byYearDays)
{
    if (isReadOnly()) {
        return;
    }
    d->mByYearDays = byYearDays;
    d->setDirty();
}

void RecurrenceRule::setByWeekNumbers(const QList<int> &byWeekNumbers)
{
    if (isReadOnly()) {
        return;
    }
    d->mByWeekNumbers = byWeekNumbers;
    d->setDirty();
}

void RecurrenceRule::setByMonths(const QList<int> &byMonths)
{
    if (isReadOnly()) {
        return;
    }
    d->mByMonths = byMonths;
    d->setDirty();
}

void RecurrenceRule::setBySetPos(const QList<int> &bySetPos)
{
    if (isReadOnly()) {
        return;
    }
    d->mBySetPos = bySetPos;
    d->setDirty();
}

void RecurrenceRule::setWeekStart(short weekStart)
{
    if (isReadOnly()) {
        return;
    }
    d->mWeekStart = weekStart;
    d->setDirty();
}

void RecurrenceRule::shiftTimes(const KDateTime::Spec &oldSpec, const KDateTime::Spec &newSpec)
{
    d->mDateStart = d->mDateStart.toTimeSpec(oldSpec);
    d->mDateStart.setTimeSpec(newSpec);
    if (d->mDuration == 0) {
        d->mDateEnd = d->mDateEnd.toTimeSpec(oldSpec);
        d->mDateEnd.setTimeSpec(newSpec);
    }
    d->setDirty();
}

// Taken from recurrence.cpp
// int RecurrenceRule::maxIterations() const
// {
//   /* Find the maximum number of iterations which may be needed to reach the
//    * next actual occurrence of a monthly or yearly recurrence.
//    * More than one iteration may be needed if, for example, it's the 29th February,
//    * the 31st day of the month or the 5th Monday, and the month being checked is
//    * February or a 30-day month.
//    * The following recurrences may never occur:
//    * - For rMonthlyDay: if the frequency is a whole number of years.
//    * - For rMonthlyPos: if the frequency is an even whole number of years.
//    * - For rYearlyDay, rYearlyMonth: if the frequeny is a multiple of 4 years.
//    * - For rYearlyPos: if the frequency is an even number of years.
//    * The maximum number of iterations needed, assuming that it does actually occur,
//    * was found empirically.
//    */
//   switch (recurs) {
//     case rMonthlyDay:
//       return (rFreq % 12) ? 6 : 8;
//
//     case rMonthlyPos:
//       if (rFreq % 12 == 0) {
//         // Some of these frequencies may never occur
//         return (rFreq % 84 == 0) ? 364         // frequency = multiple of 7 years
//              : (rFreq % 48 == 0) ? 7           // frequency = multiple of 4 years
//              : (rFreq % 24 == 0) ? 14 : 28;    // frequency = multiple of 2 or 1 year
//       }
//       // All other frequencies will occur sometime
//       if (rFreq > 120)
//         return 364;    // frequencies of > 10 years will hit the date limit first
//       switch (rFreq) {
//         case 23:   return 50;
//         case 46:   return 38;
//         case 56:   return 138;
//         case 66:   return 36;
//         case 89:   return 54;
//         case 112:  return 253;
//         default:   return 25;       // most frequencies will need < 25 iterations
//       }
//
//     case rYearlyMonth:
//     case rYearlyDay:
//       return 8;          // only 29th Feb or day 366 will need more than one iteration
//
//     case rYearlyPos:
//       if (rFreq % 7 == 0)
//         return 364;    // frequencies of a multiple of 7 years will hit the date limit first
//       if (rFreq % 2 == 0) {
//         // Some of these frequencies may never occur
//         return (rFreq % 4 == 0) ? 7 : 14;    // frequency = even number of years
//       }
//       return 28;
//   }
//   return 1;
// }

//@cond PRIVATE
void RecurrenceRule::Private::buildConstraints()
{
    mTimedRepetition = 0;
    mNoByRules = mBySetPos.isEmpty();
    mConstraints.clear();
    Constraint con(mDateStart.timeSpec());
    if (mWeekStart > 0) {
        con.setWeekstart(mWeekStart);
    }
    mConstraints.append(con);

    int c, cend;
    int i, iend;
    Constraint::List tmp;

#define intConstraint( list, setElement ) \
  if ( !list.isEmpty() ) { \
    mNoByRules = false; \
    iend = list.count(); \
    if ( iend == 1 ) { \
      for ( c = 0, cend = mConstraints.count();  c < cend;  ++c ) { \
        mConstraints[c].setElement( list[0] ); \
      } \
    } else { \
      for ( c = 0, cend = mConstraints.count();  c < cend;  ++c ) { \
        for ( i = 0;  i < iend;  ++i ) { \
          con = mConstraints[c]; \
          con.setElement( list[i] ); \
          tmp.append( con ); \
        } \
      } \
      mConstraints = tmp; \
      tmp.clear(); \
    } \
  }

    intConstraint(mBySeconds, setSecond);
    intConstraint(mByMinutes, setMinute);
    intConstraint(mByHours, setHour);
    intConstraint(mByMonthDays, setDay);
    intConstraint(mByMonths, setMonth);
    intConstraint(mByYearDays, setYearday);
    intConstraint(mByWeekNumbers, setWeeknumber);
#undef intConstraint

    if (!mByDays.isEmpty()) {
        mNoByRules = false;
        for (c = 0, cend = mConstraints.count();  c < cend;  ++c) {
            for (i = 0, iend = mByDays.count();  i < iend;  ++i) {
                con = mConstraints[c];
                con.setWeekday(mByDays[i].day());
                con.setWeekdaynr(mByDays[i].pos());
                tmp.append(con);
            }
        }
        mConstraints = tmp;
        tmp.clear();
    }

#define fixConstraint( setElement, value ) \
  { \
    for ( c = 0, cend = mConstraints.count();  c < cend;  ++c ) { \
      mConstraints[c].setElement( value );                        \
    } \
  }
    // Now determine missing values from DTSTART. This can speed up things,
    // because we have more restrictions and save some loops.

    // TODO: Does RFC 2445 intend to restrict the weekday in all cases of weekly?
    if (mPeriod == rWeekly && mByDays.isEmpty()) {
        fixConstraint(setWeekday, mDateStart.date().dayOfWeek());
    }

    // Really fall through in the cases, because all smaller time intervals are
    // constrained from dtstart
    switch (mPeriod) {
    case rYearly:
        if (mByDays.isEmpty() && mByWeekNumbers.isEmpty() &&
                mByYearDays.isEmpty() && mByMonths.isEmpty()) {
            fixConstraint(setMonth, mDateStart.date().month());
        }
    case rMonthly:
        if (mByDays.isEmpty() && mByWeekNumbers.isEmpty() &&
                mByYearDays.isEmpty() && mByMonthDays.isEmpty()) {
            fixConstraint(setDay, mDateStart.date().day());
        }
    case rWeekly:
    case rDaily:
        if (mByHours.isEmpty()) {
            fixConstraint(setHour, mDateStart.time().hour());
        }
    case rHourly:
        if (mByMinutes.isEmpty()) {
            fixConstraint(setMinute, mDateStart.time().minute());
        }
    case rMinutely:
        if (mBySeconds.isEmpty()) {
            fixConstraint(setSecond, mDateStart.time().second());
        }
    case rSecondly:
    default:
        break;
    }
#undef fixConstraint

    if (mNoByRules) {
        switch (mPeriod) {
        case rHourly:
            mTimedRepetition = mFrequency * 3600;
            break;
        case rMinutely:
            mTimedRepetition = mFrequency * 60;
            break;
        case rSecondly:
            mTimedRepetition = mFrequency;
            break;
        default:
            break;
        }
    } else {
        for (c = 0, cend = mConstraints.count(); c < cend;) {
            if (mConstraints[c].isConsistent(mPeriod)) {
                ++c;
            } else {
                mConstraints.removeAt(c);
                --cend;
            }
        }
    }
}

// Build and cache a list of all occurrences.
// Only call buildCache() if mDuration > 0.
bool RecurrenceRule::Private::buildCache() const
{
    Q_ASSERT(mDuration > 0);
    // Build the list of all occurrences of this event (we need that to determine
    // the end date!)
    Constraint interval(getNextValidDateInterval(mDateStart, mPeriod));
    QDateTime next;

    DateTimeList dts = datesForInterval(interval, mPeriod);
    // Only use dates after the event has started (start date is only included
    // if it matches)
    int i = dts.findLT(mDateStart);
    if (i >= 0) {
        dts.erase(dts.begin(), dts.begin() + i + 1);
    }

    // some validity checks to avoid infinite loops (i.e. if we have
    // done this loop already 10000 times, bail out )
    for (int loopnr = 0; loopnr < LOOP_LIMIT && dts.count() < mDuration; ++loopnr) {
        interval.increase(mPeriod, mFrequency);
        // The returned date list is already sorted!
        dts += datesForInterval(interval, mPeriod);
    }
    if (dts.count() > mDuration) {
        // we have picked up more occurrences than necessary, remove them
        dts.erase(dts.begin() + mDuration, dts.end());
    }
    mCached = true;
    mCachedDates = dts;

// it = dts.begin();
// while ( it != dts.end() ) {
//   kDebug() << "            -=>" << dumpTime(*it);
//   ++it;
// }
    if (int(dts.count()) == mDuration) {
        mCachedDateEnd = dts.last();
        return true;
    } else {
        // The cached date list is incomplete
        mCachedDateEnd = KDateTime();
        mCachedLastDate = interval.intervalDateTime(mPeriod);
        return false;
    }
}
//@endcond

bool RecurrenceRule::dateMatchesRules(const KDateTime &kdt) const
{
    KDateTime dt = kdt.toTimeSpec(d->mDateStart.timeSpec());
    for (int i = 0, iend = d->mConstraints.count();  i < iend;  ++i) {
        if (d->mConstraints[i].matches(dt, recurrenceType())) {
            return true;
        }
    }
    return false;
}

bool RecurrenceRule::recursOn(const QDate &qd, const KDateTime::Spec &timeSpec) const
{
    int i, iend;

    if (!qd.isValid() || !d->mDateStart.isValid()) {
        // There can't be recurrences on invalid dates
        return false;
    }

    if (allDay()) {
        // It's a date-only rule, so it has no time specification.
        // Therefore ignore 'timeSpec'.
        if (qd < d->mDateStart.date()) {
            return false;
        }
        // Start date is only included if it really matches
        QDate endDate;
        if (d->mDuration >= 0) {
            endDate =  endDt().date();
            if (qd > endDate) {
                return false;
            }
        }

        // The date must be in an appropriate interval (getNextValidDateInterval),
        // Plus it must match at least one of the constraints
        bool match = false;
        for (i = 0, iend = d->mConstraints.count();  i < iend && !match;  ++i) {
            match = d->mConstraints[i].matches(qd, recurrenceType());
        }
        if (!match) {
            return false;
        }

        KDateTime start(qd, QTime(0, 0, 0), d->mDateStart.timeSpec());
        Constraint interval(d->getNextValidDateInterval(start, recurrenceType()));
        // Constraint::matches is quite efficient, so first check if it can occur at
        // all before we calculate all actual dates.
        if (!interval.matches(qd, recurrenceType())) {
            return false;
        }
        // We really need to obtain the list of dates in this interval, since
        // otherwise BYSETPOS will not work (i.e. the date will match the interval,
        // but BYSETPOS selects only one of these matching dates!
        KDateTime end = start.addDays(1);
        do {
            DateTimeList dts = d->datesForInterval(interval, recurrenceType());
            for (i = 0, iend = dts.count();  i < iend;  ++i) {
                if (dts[i].date() >= qd) {
                    return dts[i].date() == qd;
                }
            }
            interval.increase(recurrenceType(), frequency());
        } while (interval.intervalDateTime(recurrenceType()) < end);
        return false;
    }

    // It's a date-time rule, so we need to take the time specification into account.
    KDateTime start(qd, QTime(0, 0, 0), timeSpec);
    KDateTime end = start.addDays(1).toTimeSpec(d->mDateStart.timeSpec());
    start = start.toTimeSpec(d->mDateStart.timeSpec());
    if (end < d->mDateStart) {
        return false;
    }
    if (start < d->mDateStart) {
        start = d->mDateStart;
    }

    // Start date is only included if it really matches
    if (d->mDuration >= 0) {
        KDateTime endRecur = endDt();
        if (endRecur.isValid()) {
            if (start > endRecur) {
                return false;
            }
            if (end > endRecur) {
                end = endRecur;    // limit end-of-day time to end of recurrence rule
            }
        }
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints
        int n = static_cast<int>((d->mDateStart.secsTo_long(start) - 1) % d->mTimedRepetition);
        return start.addSecs(d->mTimedRepetition - n) < end;
    }

    // Find the start and end dates in the time spec for the rule
    QDate startDay = start.date();
    QDate endDay = end.addSecs(-1).date();
    int dayCount = startDay.daysTo(endDay) + 1;

    // The date must be in an appropriate interval (getNextValidDateInterval),
    // Plus it must match at least one of the constraints
    bool match = false;
    for (i = 0, iend = d->mConstraints.count();  i < iend && !match;  ++i) {
        match = d->mConstraints[i].matches(startDay, recurrenceType());
        for (int day = 1;  day < dayCount && !match;  ++day) {
            match = d->mConstraints[i].matches(startDay.addDays(day), recurrenceType());
        }
    }
    if (!match) {
        return false;
    }

    Constraint interval(d->getNextValidDateInterval(start, recurrenceType()));
    // Constraint::matches is quite efficient, so first check if it can occur at
    // all before we calculate all actual dates.
    match = false;
    Constraint intervalm = interval;
    do {
        match = intervalm.matches(startDay, recurrenceType());
        for (int day = 1;  day < dayCount && !match;  ++day) {
            match = intervalm.matches(startDay.addDays(day), recurrenceType());
        }
        if (match) {
            break;
        }
        intervalm.increase(recurrenceType(), frequency());
    } while (intervalm.intervalDateTime(recurrenceType()) < end);
    if (!match) {
        return false;
    }

    // We really need to obtain the list of dates in this interval, since
    // otherwise BYSETPOS will not work (i.e. the date will match the interval,
    // but BYSETPOS selects only one of these matching dates!
    do {
        DateTimeList dts = d->datesForInterval(interval, recurrenceType());
        int i = dts.findGE(start);
        if (i >= 0) {
            return dts[i] <= end;
        }
        interval.increase(recurrenceType(), frequency());
    } while (interval.intervalDateTime(recurrenceType()) < end);

    return false;
}

bool RecurrenceRule::recursAt(const KDateTime &kdt) const
{
    // Convert to the time spec used by this recurrence rule
    KDateTime dt(kdt.toTimeSpec(d->mDateStart.timeSpec()));

    if (allDay()) {
        return recursOn(dt.date(), dt.timeSpec());
    }
    if (dt < d->mDateStart) {
        return false;
    }
    // Start date is only included if it really matches
    if (d->mDuration >= 0 && dt > endDt()) {
        return false;
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints
        return !(d->mDateStart.secsTo_long(dt) % d->mTimedRepetition);
    }

    // The date must be in an appropriate interval (getNextValidDateInterval),
    // Plus it must match at least one of the constraints
    if (!dateMatchesRules(dt)) {
        return false;
    }
    // if it recurs every interval, speed things up...
//   if ( d->mFrequency == 1 && d->mBySetPos.isEmpty() && d->mByDays.isEmpty() ) return true;
    Constraint interval(d->getNextValidDateInterval(dt, recurrenceType()));
    // TODO_Recurrence: Does this work with BySetPos???
    if (interval.matches(dt, recurrenceType())) {
        return true;
    }
    return false;
}

TimeList RecurrenceRule::recurTimesOn(const QDate &date, const KDateTime::Spec &timeSpec) const
{
    TimeList lst;
    if (allDay()) {
        return lst;
    }
    KDateTime start(date, QTime(0, 0, 0), timeSpec);
    KDateTime end = start.addDays(1).addSecs(-1);
    DateTimeList dts = timesInInterval(start, end);     // returns between start and end inclusive
    for (int i = 0, iend = dts.count();  i < iend;  ++i) {
        lst += dts[i].toTimeSpec(timeSpec).time();
    }
    return lst;
}

/** Returns the number of recurrences up to and including the date/time specified. */
int RecurrenceRule::durationTo(const KDateTime &dt) const
{
    // Convert to the time spec used by this recurrence rule
    KDateTime toDate(dt.toTimeSpec(d->mDateStart.timeSpec()));
    // Easy cases:
    // either before start, or after all recurrences and we know their number
    if (toDate < d->mDateStart) {
        return 0;
    }
    // Start date is only included if it really matches
    if (d->mDuration > 0 && toDate >= endDt()) {
        return d->mDuration;
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints
        return static_cast<int>(d->mDateStart.secsTo_long(toDate) / d->mTimedRepetition);
    }

    return timesInInterval(d->mDateStart, toDate).count();
}

int RecurrenceRule::durationTo(const QDate &date) const
{
    return durationTo(KDateTime(date, QTime(23, 59, 59), d->mDateStart.timeSpec()));
}

KDateTime RecurrenceRule::getPreviousDate(const KDateTime &afterDate) const
{
    // Convert to the time spec used by this recurrence rule
    KDateTime toDate(afterDate.toTimeSpec(d->mDateStart.timeSpec()));

    // Invalid starting point, or beyond end of recurrence
    if (!toDate.isValid() || toDate < d->mDateStart) {
        return KDateTime();
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints
        KDateTime prev = toDate;
        if (d->mDuration >= 0 && endDt().isValid() && toDate > endDt()) {
            prev = endDt().addSecs(1).toTimeSpec(d->mDateStart.timeSpec());
        }
        int n = static_cast<int>((d->mDateStart.secsTo_long(prev) - 1) % d->mTimedRepetition);
        if (n < 0) {
            return KDateTime();  // before recurrence start
        }
        prev = prev.addSecs(-n - 1);
        return prev >= d->mDateStart ? prev : KDateTime();
    }

    // If we have a cache (duration given), use that
    if (d->mDuration > 0) {
        if (!d->mCached) {
            d->buildCache();
        }
        int i = d->mCachedDates.findLT(toDate);
        if (i >= 0) {
            return d->mCachedDates[i];
        }
        return KDateTime();
    }

    KDateTime prev = toDate;
    if (d->mDuration >= 0 && endDt().isValid() && toDate > endDt()) {
        prev = endDt().addSecs(1).toTimeSpec(d->mDateStart.timeSpec());
    }

    Constraint interval(d->getPreviousValidDateInterval(prev, recurrenceType()));
    DateTimeList dts = d->datesForInterval(interval, recurrenceType());
    int i = dts.findLT(prev);
    if (i >= 0) {
        return (dts[i] >= d->mDateStart) ? dts[i] : KDateTime();
    }

    // Previous interval. As soon as we find an occurrence, we're done.
    while (interval.intervalDateTime(recurrenceType()) > d->mDateStart) {
        interval.increase(recurrenceType(), -int(frequency()));
        // The returned date list is sorted
        DateTimeList dts = d->datesForInterval(interval, recurrenceType());
        // The list is sorted, so take the last one.
        if (!dts.isEmpty()) {
            prev = dts.last();
            if (prev.isValid() && prev >= d->mDateStart) {
                return prev;
            } else {
                return KDateTime();
            }
        }
    }
    return KDateTime();
}

KDateTime RecurrenceRule::getNextDate(const KDateTime &preDate) const
{
    // Convert to the time spec used by this recurrence rule
    KDateTime fromDate(preDate.toTimeSpec(d->mDateStart.timeSpec()));
    // Beyond end of recurrence
    if (d->mDuration >= 0 && endDt().isValid() && fromDate >= endDt()) {
        return KDateTime();
    }

    // Start date is only included if it really matches
    if (fromDate < d->mDateStart) {
        fromDate = d->mDateStart.addSecs(-1);
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints
        int n = static_cast<int>((d->mDateStart.secsTo_long(fromDate) + 1) % d->mTimedRepetition);
        KDateTime next = fromDate.addSecs(d->mTimedRepetition - n + 1);
        return d->mDuration < 0 || !endDt().isValid() || next <= endDt() ? next : KDateTime();
    }

    if (d->mDuration > 0) {
        if (!d->mCached) {
            d->buildCache();
        }
        int i = d->mCachedDates.findGT(fromDate);
        if (i >= 0) {
            return d->mCachedDates[i];
        }
    }

    KDateTime end = endDt();
    Constraint interval(d->getNextValidDateInterval(fromDate, recurrenceType()));
    DateTimeList dts = d->datesForInterval(interval, recurrenceType());
    int i = dts.findGT(fromDate);
    if (i >= 0) {
        return (d->mDuration < 0 || dts[i] <= end) ? dts[i] : KDateTime();
    }
    interval.increase(recurrenceType(), frequency());
    if (d->mDuration >= 0 && interval.intervalDateTime(recurrenceType()) > end) {
        return KDateTime();
    }

    // Increase the interval. The first occurrence that we find is the result (if
    // if's before the end date).
    // TODO: some validity checks to avoid infinite loops for contradictory constraints
    int loop = 0;
    do {
        DateTimeList dts = d->datesForInterval(interval, recurrenceType());
        if (dts.count() > 0) {
            KDateTime ret(dts[0]);
            if (d->mDuration >= 0 && ret > end) {
                return KDateTime();
            } else {
                return ret;
            }
        }
        interval.increase(recurrenceType(), frequency());
    } while (++loop < LOOP_LIMIT &&
             (d->mDuration < 0 || interval.intervalDateTime(recurrenceType()) < end));
    return KDateTime();
}

DateTimeList RecurrenceRule::timesInInterval(const KDateTime &dtStart,
        const KDateTime &dtEnd) const
{
    const KDateTime start = dtStart.toTimeSpec(d->mDateStart.timeSpec());
    const KDateTime end = dtEnd.toTimeSpec(d->mDateStart.timeSpec());
    DateTimeList result;
    if (end < d->mDateStart) {
        return result;    // before start of recurrence
    }
    KDateTime enddt = end;
    if (d->mDuration >= 0) {
        const KDateTime endRecur = endDt();
        if (endRecur.isValid()) {
            if (start > endRecur) {
                return result;    // beyond end of recurrence
            }
            if (end >= endRecur) {
                enddt = endRecur;    // limit end time to end of recurrence rule
            }
        }
    }

    if (d->mTimedRepetition) {
        // It's a simple sub-daily recurrence with no constraints

        //Seconds to add to interval start, to get first occurrence which is within interval
        qint64 offsetFromNextOccurrence;
        if (d->mDateStart < start) {
            offsetFromNextOccurrence =
                d->mTimedRepetition - (d->mDateStart.secsTo_long(start) % d->mTimedRepetition);
        } else {
            offsetFromNextOccurrence = -(d->mDateStart.secsTo_long(start) % d->mTimedRepetition);
        }
        KDateTime dt = start.addSecs(offsetFromNextOccurrence);
        if (dt <= enddt) {
            int numberOfOccurrencesWithinInterval =
                static_cast<int>(dt.secsTo_long(enddt) / d->mTimedRepetition) + 1;
            // limit n by a sane value else we can "explode".
            numberOfOccurrencesWithinInterval = qMin(numberOfOccurrencesWithinInterval, LOOP_LIMIT);
            for (int i = 0;
                    i < numberOfOccurrencesWithinInterval;
                    dt = dt.addSecs(d->mTimedRepetition), ++i) {
                result += dt;
            }
        }
        return result;
    }

    KDateTime st = start;
    bool done = false;
    if (d->mDuration > 0) {
        if (!d->mCached) {
            d->buildCache();
        }
        if (d->mCachedDateEnd.isValid() && start > d->mCachedDateEnd) {
            return result;    // beyond end of recurrence
        }
        int i = d->mCachedDates.findGE(start);
        if (i >= 0) {
            int iend = d->mCachedDates.findGT(enddt, i);
            if (iend < 0) {
                iend = d->mCachedDates.count();
            } else {
                done = true;
            }
            while (i < iend) {
                result += d->mCachedDates[i++];
            }
        }
        if (d->mCachedDateEnd.isValid()) {
            done = true;
        } else if (!result.isEmpty()) {
            result += KDateTime();    // indicate that the returned list is incomplete
            done = true;
        }
        if (done) {
            return result;
        }
        // We don't have any result yet, but we reached the end of the incomplete cache
        st = d->mCachedLastDate.addSecs(1);
    }

    Constraint interval(d->getNextValidDateInterval(st, recurrenceType()));
    int loop = 0;
    do {
        DateTimeList dts = d->datesForInterval(interval, recurrenceType());
        int i = 0;
        int iend = dts.count();
        if (loop == 0) {
            i = dts.findGE(st);
            if (i < 0) {
                i = iend;
            }
        }
        int j = dts.findGT(enddt, i);
        if (j >= 0) {
            iend = j;
            loop = LOOP_LIMIT;
        }
        while (i < iend) {
            result += dts[i++];
        }
        // Increase the interval.
        interval.increase(recurrenceType(), frequency());
    } while (++loop < LOOP_LIMIT &&
             interval.intervalDateTime(recurrenceType()) < end);
    return result;
}

//@cond PRIVATE
// Find the date/time of the occurrence at or before a date/time,
// for a given period type.
// Return a constraint whose value appropriate to 'type', is set to
// the value contained in the date/time.
Constraint RecurrenceRule::Private::getPreviousValidDateInterval(const KDateTime &dt,
        PeriodType type) const
{
    long periods = 0;
    KDateTime start = mDateStart;
    KDateTime nextValid(start);
    int modifier = 1;
    KDateTime toDate(dt.toTimeSpec(start.timeSpec()));
    // for super-daily recurrences, don't care about the time part

    // Find the #intervals since the dtstart and round to the next multiple of
    // the frequency
    switch (type) {
        // Really fall through for sub-daily, since the calculations only differ
        // by the factor 60 and 60*60! Same for weekly and daily (factor 7)
    case rHourly:
        modifier *= 60;
    case rMinutely:
        modifier *= 60;
    case rSecondly:
        periods = static_cast<int>(start.secsTo_long(toDate) / modifier);
        // round it down to the next lower multiple of frequency:
        if (mFrequency > 0) {
            periods = (periods / mFrequency) * mFrequency;
        }
        nextValid = start.addSecs(modifier * periods);
        break;
    case rWeekly:
        toDate = toDate.addDays(-(7 + toDate.date().dayOfWeek() - mWeekStart) % 7);
        start = start.addDays(-(7 + start.date().dayOfWeek() - mWeekStart) % 7);
        modifier *= 7;
    case rDaily:
        periods = start.daysTo(toDate) / modifier;
        // round it down to the next lower multiple of frequency:
        if (mFrequency > 0) {
            periods = (periods / mFrequency) * mFrequency;
        }
        nextValid = start.addDays(modifier * periods);
        break;
    case rMonthly:
    {
        periods = 12 * (toDate.date().year() - start.date().year()) +
                  (toDate.date().month() - start.date().month());
        // round it down to the next lower multiple of frequency:
        if (mFrequency > 0) {
            periods = (periods / mFrequency) * mFrequency;
        }
        // set the day to the first day of the month, so we don't have problems
        // with non-existent days like Feb 30 or April 31
        start.setDate(QDate(start.date().year(), start.date().month(), 1));
        nextValid.setDate(start.date().addMonths(periods));
        break;
    }
    case rYearly:
        periods = (toDate.date().year() - start.date().year());
        // round it down to the next lower multiple of frequency:
        if (mFrequency > 0) {
            periods = (periods / mFrequency) * mFrequency;
        }
        nextValid.setDate(start.date().addYears(periods));
        break;
    default:
        break;
    }

    return Constraint(nextValid, type, mWeekStart);
}

// Find the date/time of the next occurrence at or after a date/time,
// for a given period type.
// Return a constraint whose value appropriate to 'type', is set to the
// value contained in the date/time.
Constraint RecurrenceRule::Private::getNextValidDateInterval(const KDateTime &dt,
        PeriodType type) const
{
    // TODO: Simplify this!
    long periods = 0;
    KDateTime start = mDateStart;
    KDateTime nextValid(start);
    int modifier = 1;
    KDateTime toDate(dt.toTimeSpec(start.timeSpec()));
    // for super-daily recurrences, don't care about the time part

    // Find the #intervals since the dtstart and round to the next multiple of
    // the frequency
    switch (type) {
        // Really fall through for sub-daily, since the calculations only differ
        // by the factor 60 and 60*60! Same for weekly and daily (factor 7)
    case rHourly:
        modifier *= 60;
    case rMinutely:
        modifier *= 60;
    case rSecondly:
        periods = static_cast<int>(start.secsTo_long(toDate) / modifier);
        periods = qMax(0L, periods);
        if (periods > 0 && mFrequency > 0) {
            periods += (mFrequency - 1 - ((periods - 1) % mFrequency));
        }
        nextValid = start.addSecs(modifier * periods);
        break;
    case rWeekly:
        // correct both start date and current date to start of week
        toDate = toDate.addDays(-(7 + toDate.date().dayOfWeek() - mWeekStart) % 7);
        start = start.addDays(-(7 + start.date().dayOfWeek() - mWeekStart) % 7);
        modifier *= 7;
    case rDaily:
        periods = start.daysTo(toDate) / modifier;
        periods = qMax(0L, periods);
        if (periods > 0 && mFrequency > 0) {
            periods += (mFrequency - 1 - ((periods - 1) % mFrequency));
        }
        nextValid = start.addDays(modifier * periods);
        break;
    case rMonthly:
    {
        periods = 12 * (toDate.date().year() - start.date().year()) +
                  (toDate.date().month() - start.date().month());
        periods = qMax(0L, periods);
        if (periods > 0 && mFrequency > 0) {
            periods += (mFrequency - 1 - ((periods - 1) % mFrequency));
        }
        // set the day to the first day of the month, so we don't have problems
        // with non-existent days like Feb 30 or April 31
        start.setDate(QDate(start.date().year(), start.date().month(), 1));
        nextValid.setDate(start.date().addMonths(periods));
        break;
    }
    case rYearly:
        periods = (toDate.date().year() - start.date().year());
        periods = qMax(0L, periods);
        if (periods > 0 && mFrequency > 0) {
            periods += (mFrequency - 1 - ((periods - 1) % mFrequency));
        }
        nextValid.setDate(start.date().addYears(periods));
        break;
    default:
        break;
    }

    return Constraint(nextValid, type, mWeekStart);
}

DateTimeList RecurrenceRule::Private::datesForInterval(const Constraint &interval,
        PeriodType type) const
{
    /* -) Loop through constraints,
       -) merge interval with each constraint
       -) if merged constraint is not consistent => ignore that constraint
       -) if complete => add that one date to the date list
       -) Loop through all missing fields => For each add the resulting
    */
    DateTimeList lst;
    for (int i = 0, iend = mConstraints.count();  i < iend;  ++i) {
        Constraint merged(interval);
        if (merged.merge(mConstraints[i])) {
            // If the information is incomplete, we can't use this constraint
            if (merged.year > 0 && merged.hour >= 0 && merged.minute >= 0 && merged.second >= 0) {
                // We have a valid constraint, so get all datetimes that match it andd
                // append it to all date/times of this interval
                QList<KDateTime> lstnew = merged.dateTimes(type);
                lst += lstnew;
            }
        }
    }
    // Sort it so we can apply the BySetPos. Also some logic relies on this being sorted
    lst.sortUnique();

    /*if ( lst.isEmpty() ) {
      kDebug() << "         No Dates in Interval";
    } else {
      kDebug() << "         Dates:";
      for ( int i = 0, iend = lst.count();  i < iend;  ++i ) {
        kDebug()<< "              -)" << dumpTime(lst[i]);
      }
      kDebug() << "       ---------------------";
    }*/
    if (!mBySetPos.isEmpty()) {
        DateTimeList tmplst = lst;
        lst.clear();
        for (int i = 0, iend = mBySetPos.count();  i < iend;  ++i) {
            int pos = mBySetPos[i];
            if (pos > 0) {
                --pos;
            }
            if (pos < 0) {
                pos += tmplst.count();
            }
            if (pos >= 0 && pos < tmplst.count()) {
                lst.append(tmplst[pos]);
            }
        }
        lst.sortUnique();
    }

    return lst;
}
//@endcond

void RecurrenceRule::dump() const
{
#ifndef NDEBUG
    kDebug();
    if (!d->mRRule.isEmpty()) {
        kDebug() << "   RRULE=" << d->mRRule;
    }
    kDebug() << "   Read-Only:" << isReadOnly();

    kDebug() << "   Period type:" << int(recurrenceType()) << ", frequency:" << frequency();
    kDebug() << "   #occurrences:" << duration();
    kDebug() << "   start date:" << dumpTime(startDt())
             << ", end date:" << dumpTime(endDt());

#define dumpByIntList(list,label) \
  if ( !list.isEmpty() ) {\
    QStringList lst;\
    for ( int i = 0, iend = list.count();  i < iend;  ++i ) {\
      lst.append( QString::number( list[i] ) );\
    }\
    kDebug() << "  " << label << lst.join( QLatin1String(", ") );\
  }
    dumpByIntList(d->mBySeconds, QLatin1String("BySeconds:  "));
    dumpByIntList(d->mByMinutes, QLatin1String("ByMinutes:  "));
    dumpByIntList(d->mByHours, QLatin1String("ByHours:    "));
    if (!d->mByDays.isEmpty()) {
        QStringList lst;
        for (int i = 0, iend = d->mByDays.count();  i < iend;  ++i) {
            \
            lst.append((d->mByDays[i].pos() ? QString::number(d->mByDays[i].pos()) : QLatin1String("")) +
                       DateHelper::dayName(d->mByDays[i].day()));
        }
        kDebug() << "   ByDays:    " << lst.join(QLatin1String(", "));
    }
    dumpByIntList(d->mByMonthDays, QLatin1String("ByMonthDays:"));
    dumpByIntList(d->mByYearDays, QLatin1String("ByYearDays: "));
    dumpByIntList(d->mByWeekNumbers, QLatin1String("ByWeekNr:   "));
    dumpByIntList(d->mByMonths, QLatin1String("ByMonths:   "));
    dumpByIntList(d->mBySetPos, QLatin1String("BySetPos:   "));
#undef dumpByIntList

    kDebug() << "   Week start:" << DateHelper::dayName(d->mWeekStart);   //krazy:exclude=kdebug

    kDebug() << "   Constraints:";
    // dump constraints
    for (int i = 0, iend = d->mConstraints.count();  i < iend;  ++i) {
        d->mConstraints[i].dump();
    }
#endif
}

//@cond PRIVATE
void Constraint::dump() const
{
    kDebug() << "     ~> Y=" << year
             << ", M=" << month
             << ", D=" << day
             << ", H=" << hour
             << ", m=" << minute
             << ", S=" << second
             << ", wd=" << weekday
             << ",#wd=" << weekdaynr
             << ", #w=" << weeknumber
             << ", yd=" << yearday;
}
//@endcond

QString dumpTime(const KDateTime &dt)
{
#ifndef NDEBUG
    if (!dt.isValid()) {
        return QString();
    }
    QString result;
    if (dt.isDateOnly()) {
        result = dt.toString(QLatin1String("%a %Y-%m-%d %:Z"));
    } else {
        result = dt.toString(QLatin1String("%a %Y-%m-%d %H:%M:%S %:Z"));
        if (dt.isSecondOccurrence()) {
            result += QLatin1String(" (2nd)");
        }
    }
    if (dt.timeSpec() == KDateTime::Spec::ClockTime()) {
        result += QLatin1String("Clock");
    }
    return result;
#else
    Q_UNUSED(dt);
    return QString();
#endif
}

KDateTime RecurrenceRule::startDt() const
{
    return d->mDateStart;
}

RecurrenceRule::PeriodType RecurrenceRule::recurrenceType() const
{
    return d->mPeriod;
}

uint RecurrenceRule::frequency() const
{
    return d->mFrequency;
}

int RecurrenceRule::duration() const
{
    return d->mDuration;
}

QString RecurrenceRule::rrule() const
{
    return d->mRRule;
}

void RecurrenceRule::setRRule(const QString &rrule)
{
    d->mRRule = rrule;
}

bool RecurrenceRule::isReadOnly() const
{
    return d->mIsReadOnly;
}

void RecurrenceRule::setReadOnly(bool readOnly)
{
    d->mIsReadOnly = readOnly;
}

bool RecurrenceRule::recurs() const
{
    return d->mPeriod != rNone;
}

bool RecurrenceRule::allDay() const
{
    return d->mAllDay;
}

const QList<int> &RecurrenceRule::bySeconds() const
{
    return d->mBySeconds;
}

const QList<int> &RecurrenceRule::byMinutes() const
{
    return d->mByMinutes;
}

const QList<int> &RecurrenceRule::byHours() const
{
    return d->mByHours;
}

const QList<RecurrenceRule::WDayPos> &RecurrenceRule::byDays() const
{
    return d->mByDays;
}

const QList<int> &RecurrenceRule::byMonthDays() const
{
    return d->mByMonthDays;
}

const QList<int> &RecurrenceRule::byYearDays() const
{
    return d->mByYearDays;
}

const QList<int> &RecurrenceRule::byWeekNumbers() const
{
    return d->mByWeekNumbers;
}

const QList<int> &RecurrenceRule::byMonths() const
{
    return d->mByMonths;
}

const QList<int> &RecurrenceRule::bySetPos() const
{
    return d->mBySetPos;
}

short RecurrenceRule::weekStart() const
{
    return d->mWeekStart;
}

RecurrenceRule::RuleObserver::~RuleObserver()
{
}

RecurrenceRule::WDayPos::WDayPos(int ps, short dy)
    : mDay(dy), mPos(ps)
{
}

void RecurrenceRule::WDayPos::setDay(short dy)
{
    mDay = dy;
}

short RecurrenceRule::WDayPos::day() const
{
    return mDay;
}

void RecurrenceRule::WDayPos::setPos(int ps)
{
    mPos = ps;
}

int RecurrenceRule::WDayPos::pos() const
{
    return mPos;
}


QDataStream& operator<<(QDataStream &out, const Constraint &c)
{
    out << c.year << c.month << c.day << c.hour << c.minute << c.second
        << c.weekday << c.weekdaynr << c.weeknumber << c.yearday << c.weekstart
        << c.timespec << c.secondOccurrence;

    return out;
}

QDataStream& operator>>(QDataStream &in, Constraint &c)
{
    in >> c.year >> c.month >> c.day >> c.hour >> c.minute >> c.second
       >> c.weekday >> c.weekdaynr >> c.weeknumber >> c.yearday >> c.weekstart
       >> c.timespec >> c.secondOccurrence;
    return in;
}

KCALCORE_EXPORT QDataStream& KCalCore::operator<<(QDataStream &out, const KCalCore::RecurrenceRule::WDayPos &w)
{
    out << w.mDay << w.mPos;
    return out;
}

KCALCORE_EXPORT QDataStream& KCalCore::operator>>(QDataStream &in, KCalCore::RecurrenceRule::WDayPos &w)
{
    in >> w.mDay >> w.mPos;
    return in;
}

KCALCORE_EXPORT QDataStream& KCalCore::operator<<(QDataStream &out, const KCalCore::RecurrenceRule *r)
{
    if (!r)
        return out;

    RecurrenceRule::Private *d = r->d;
    out << d->mRRule << static_cast<quint32>(d->mPeriod) << d->mDateStart << d->mFrequency << d->mDuration << d->mDateEnd
        << d->mBySeconds << d->mByMinutes << d->mByHours << d->mByDays << d->mByMonthDays
        << d->mByYearDays << d->mByWeekNumbers << d->mByMonths << d->mBySetPos
        << d->mWeekStart << d->mConstraints << d->mAllDay << d->mNoByRules << d->mTimedRepetition
        << d->mIsReadOnly;

    return out;
}


KCALCORE_EXPORT QDataStream& KCalCore::operator>>(QDataStream &in, const KCalCore::RecurrenceRule *r)
{
    if (!r)
        return in;

    RecurrenceRule::Private *d = r->d;
    quint32 period;
    in >> d->mRRule >> period >> d->mDateStart >> d->mFrequency >> d->mDuration >> d->mDateEnd
       >> d->mBySeconds >> d->mByMinutes >> d->mByHours >> d->mByDays >> d->mByMonthDays
       >> d->mByYearDays >> d->mByWeekNumbers >> d->mByMonths >> d->mBySetPos
       >> d->mWeekStart >> d->mConstraints >> d->mAllDay >> d->mNoByRules >> d->mTimedRepetition
       >> d->mIsReadOnly;

    d->mPeriod = static_cast<RecurrenceRule::PeriodType>(period);

    return in;
}