/usr/share/perl5/Date/Manip/Recur.pm is in libdate-manip-perl 6.57-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 | package Date::Manip::Recur;
# Copyright (c) 1998-2016 Sullivan Beck. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
########################################################################
# Any routine that starts with an underscore (_) is NOT intended for
# public use. They are for internal use in the the Date::Manip
# modules and are subject to change without warning or notice.
#
# ABSOLUTELY NO USER SUPPORT IS OFFERED FOR THESE ROUTINES!
########################################################################
use Date::Manip::Obj;
@ISA = ('Date::Manip::Obj');
require 5.010000;
use warnings;
use strict;
use integer;
use utf8;
use IO::File;
#use re 'debug';
use Date::Manip::Base;
use Date::Manip::TZ;
our $VERSION;
$VERSION='6.57';
END { undef $VERSION; }
########################################################################
# BASE METHODS
########################################################################
sub is_recur {
return 1;
}
# Call this every time a new recur is put in to make sure everything is
# correctly initialized.
#
sub _init {
my($self) = @_;
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
$$self{'err'} = '';
$$self{'data'}{'freq'} = ''; # The frequency
$$self{'data'}{'flags'} = []; # Modifiers
$$self{'data'}{'base'} = undef; # The specified base date
$$self{'data'}{'BASE'} = undef; # The actual base date
$$self{'data'}{'start'} = undef; # Start and end date
$$self{'data'}{'end'} = undef;
$$self{'data'}{'unmod_range'} = 0; # If this is 1, the start/end range
# refer to the unmodified dates, not the
# final dates.
$$self{'data'}{'interval'} = []; # (Y, M, ...)
$$self{'data'}{'rtime'} = []; # ( [ VAL_OR_RANGE, VAL_OR_RANGE, ... ],
# [ VAL_OR_RANGE, VAL_OR_RANGE, ... ],
# ... )
$$self{'data'}{'slow'} = 0; # 1 if a range of the form 2--2 is
# included.
$$self{'data'}{'ev_per_d'} = 0; # The number of events per interval date.
$$self{'data'}{'delta'} = undef; # The offset based on the interval.
$$self{'data'}{'noint'} = 1; # 0 if an interval is present
# 1 if no interval is present and dates
# not done
# 2 if no interval is present and dates
# done
$$self{'data'}{'idate'} = {}; # Non-slow:
# { N => Nth interval date }
# Slow:
# { N => [Nth interval date,X,Y] }
# [X,Y] are the first/last event indices
# generated by this interval date.
$$self{'data'}{'dates'} = {}; # { N => Nth recurring event }
# N is relative to the base date and is
# not affected by start/end
$$self{'data'}{'curr'} = undef; # Iterator pointer
$$self{'data'}{'first'} = undef; # N : the first date in a range
$$self{'data'}{'last'} = undef; # N : the last date in a range
# Get the default start/end dates
my $range = $dmb->_config('recurrange');
if ($range eq 'none') {
$$self{'data'}{'start'} = undef;
$$self{'data'}{'end'} = undef;
} elsif ($range eq 'year') {
my $y = $dmt->_now('y',1);
my $start = $self->new_date();
my $end = $self->new_date();
$start->set('date',[$y, 1, 1,00,00,00]);
$end->set ('date',[$y,12,31,23,59,59]);
$$self{'data'}{'start'} = $start;
$$self{'data'}{'end'} = $end;
} elsif ($range eq 'month') {
my ($y,$m) = $dmt->_now('now',1);
my $dim = $dmb->days_in_month($y,$m);
my $start = $self->new_date();
my $end = $self->new_date();
$start->set('date',[$y,$m, 1,00,00,00]);
$end->set ('date',[$y,$m,$dim,23,59,59]);
$$self{'data'}{'start'} = $start;
$$self{'data'}{'end'} = $end;
} elsif ($range eq 'week') {
my($y,$m,$d) = $dmt->_now('now',1);
my $w;
($y,$w) = $dmb->week_of_year([$y,$m,$d]);
($y,$m,$d) = @{ $dmb->week_of_year($y,$w) };
my($yy,$mm,$dd)
= @{ $dmb->_calc_date_ymwd([$y,$m,$d], [0,0,0,6], 0) };
my $start = $self->new_date();
my $end = $self->new_date();
$start->set('date',[$y, $m, $d, 00,00,00]);
$end->set ('date',[$yy,$mm,$dd,23,59,59]);
$$self{'data'}{'start'} = $start;
$$self{'data'}{'end'} = $end;
} elsif ($range eq 'day') {
my($y,$m,$d) = $dmt->_now('now',1);
my $start = $self->new_date();
my $end = $self->new_date();
$start->set('date',[$y,$m,$d,00,00,00]);
$end->set ('date',[$y,$m,$d,23,59,59]);
$$self{'data'}{'start'} = $start;
$$self{'data'}{'end'} = $end;
} elsif ($range eq 'all') {
my $start = $self->new_date();
my $end = $self->new_date();
$start->set('date',[0001,02,01,00,00,00]);
$end->set ('date',[9999,11,30,23,59,59]);
$$self{'data'}{'start'} = $start;
$$self{'data'}{'end'} = $end;
}
}
# If $keep is 1, it will keep any existing base date and cached
# dates, but it will reset other things.
#
sub _init_dates {
my($self,$keep) = @_;
if (! $keep) {
$$self{'data'}{'base'} = undef;
$$self{'data'}{'BASE'} = undef;
$$self{'data'}{'idate'} = {};
$$self{'data'}{'dates'} = {};
}
$$self{'data'}{'curr'} = undef;
$$self{'data'}{'first'} = undef;
$$self{'data'}{'last'} = undef;
}
sub _init_args {
my($self) = @_;
my @args = @{ $$self{'args'} };
$self->parse(@args);
}
########################################################################
# METHODS
########################################################################
sub parse {
my($self,$string,@args) = @_;
$self->_init();
# Test if $string = FREQ
my $err = $self->frequency($string);
if (! $err) {
$string = '';
}
# Test if $string = "FREQ*..." and FREQ contains an '*'.
if ($err) {
$self->err(1);
$string =~ s/\s*\*\s*/\*/g;
if ($string =~ /^([^*]*\*[^*]*)\*/) {
# Everything up to the 2nd '*'
my $freq = $1;
$err = $self->frequency($freq);
if (! $err) {
$string =~ s/^\Q$freq\E\*//;
}
} else {
$err = 1;
}
}
# Test if $string = "FREQ*..." and FREQ does NOT contains an '*'.
if ($err) {
$self->err(1);
if ($string =~ s/^([^*]*)\*//) {
# Everything up to he 1st '*'
my $freq = $1;
$err = $self->frequency($freq);
if (! $err) {
$string =~ s/^\Q$freq\E\*//;
}
} else {
$err = 1;
}
}
if ($err) {
$$self{'err'} = "[parse] Invalid frequency string";
return 1;
}
# Handle MODIFIERS from string and arguments
my @string = split(/\*/,$string);
if (@string) {
my $tmp = shift(@string);
$err = $self->modifiers($tmp) if ($tmp);
return 1 if ($err);
}
if (@args) {
my $tmp = $args[0];
if ($tmp && ! ref($tmp)) {
$err = $self->modifiers($tmp);
shift(@args) if (! $err);
}
}
# Handle BASE
if (@string) {
my $tmp = shift(@string);
$err = $self->basedate($tmp) if (defined($tmp) && $tmp);
return 1 if ($err);
}
if (@args) {
my $tmp = shift(@args);
$err = $self->basedate($tmp) if (defined($tmp) && $tmp);
return 1 if ($err);
}
# Handle START, END, UNMOD
if (@string) {
my($start) = shift(@string);
my($end) = shift(@string);
my($unmod) = shift(@string);
$err = $self->start($start,$unmod) if (defined($start) && $start);
return 1 if ($err);
$err = $self->end($end) if (defined($end) && $end);
return 1 if ($err);
}
if (@args) {
my($start) = shift(@args);
my($end) = shift(@args);
my($unmod) = shift(@args);
$err = $self->start($start,$unmod) if (defined($start) && $start);
return 1 if ($err);
$err = $self->end($end) if (defined($end) && $end);
return 1 if ($err);
}
# Remaining arguments are invalid.
if (@string) {
$$self{'err'} = "[parse] String contains invalid elements";
return 1;
}
if (@args) {
$$self{'err'} = "[parse] Unknown arguments";
return 1;
}
return 0;
}
sub frequency {
my($self,$string) = @_;
return $$self{'data'}{'freq'} if (! defined $string);
$self->_init();
my (@int,@rtime);
PARSE: {
# Standard frequency notation
my $stdrx = $self->_rx('std');
if ($string =~ $stdrx) {
my($l,$r) = @+{qw(l r)};
if (defined($l)) {
$l =~ s/^\s*:/0:/;
$l =~ s/:\s*$/:0/;
$l =~ s/::/:0:/g;
@int = split(/:/,$l);
}
if (defined($r)) {
$r =~ s/^\s*:/0:/;
$r =~ s/:\s*$/:0/;
$r =~ s/::/:0:/g;
@rtime = split(/:/,$r);
}
last PARSE;
}
# Other frequency strings
# Strip out some words to ignore
my $ignrx = $self->_rx('ignore');
$string =~ s/$ignrx/ /g;
my $eachrx = $self->_rx('each');
my $each = 0;
if ($string =~ s/$eachrx/ /g) {
$each = 1;
}
$string =~ s/\s*$//;
if (! $string) {
$$self{'err'} = "[frequency] Invalid frequency string";
return 1;
}
my($l,$r);
my $err = $self->_parse_lang($string);
if ($err) {
$$self{'err'} = "[frequency] Invalid frequency string";
return 1;
}
return 0;
}
# If the interval consists only of zeros, the last entry is changed
# to 1.
if (@int) {
for my $i (@int) {
$i += 0;
}
TEST_INT: {
for my $i (@int) {
last TEST_INT if ($i);
}
$int[$#int] = 1;
}
}
# If @int contains 2 or 3 elements and ends in 0, move the trailing
# 0 to the start of @rtime.
#
# Y:M:0 * D:H:MN:S => Y:M * 0:D:H:MN:S
while (@int &&
($#int == 1 || $#int == 2) &&
($int[$#int] == 0)) {
pop(@int);
unshift(@rtime,0);
}
# We need to know what the valid values of M, W, and D are.
#
# Month can be:
# moy : 1 to 12 (month of the year)
#
# Week can be:
# woy : 1 to 53 or -1 to -53 (week of the year)
# wom : 1 to 5 or -1 to -5 (week of the month)
#
# Day can be:
# doy : 1 to 366 or -1 to -366 (day of the year)
# dom : 1 to 31 or -1 to -31 (day of the month)
# dow : 1 to 7 (day of the week)
#
# Other values must be zero or positive.
my($mtype,$wtype,$dtype) = ('','','');
my @f = (@int,@rtime);
my $m = $f[1];
my $w = $f[2];
my $d = $f[3];
if ($d && @int < 4) {
if ($w) {
$dtype = 'dow';
} elsif ($m) {
$dtype = 'dom';
} else {
$dtype = 'doy';
}
}
if ($w && @int < 3) {
if ($m) {
$wtype = 'wom';
} else {
$wtype = 'woy';
}
}
if ($m && @int < 2) {
$mtype = 'moy';
}
# Test the format of @rtime.
#
# Turn it to:
# @rtime = ( NUM|RANGE, NUM|RANGE, ...)
# where
# NUM is an integer
# RANGE is [NUM1,NUM2]
my $rfieldrx = $self->_rx('rfield');
my $rrangerx = $self->_rx('rrange');
my @type = qw(y m w d h mn s);
while ($#type > $#rtime) {
shift(@type);
}
foreach my $rfield (@rtime) {
my $type = shift(@type);
if ($rfield !~ $rfieldrx) {
$$self{'err'} = "[frequency] Invalid rtime string";
return 1;
}
my @rfield = split(/,/,$rfield);
my @val;
foreach my $vals (@rfield) {
if ($vals =~ $rrangerx) {
my ($num1,$num2) = ($1+0,$2+0);
my $err = $self->_frequency_values($num1,$type,$mtype,$wtype,$dtype);
return $err if ($err);
$err = $self->_frequency_values($num2,$type,$mtype,$wtype,$dtype);
return $err if ($err);
if ( ($num1 > 0 && $num2 > 0) ||
($num1 < 0 && $num2 < 0) ) {
if ($num1 > $num2) {
$$self{'err'} = "[frequency] Invalid rtime range string";
return 1;
}
push(@val,$num1..$num2);
} else {
push(@val,[$num1,$num2]);
}
} else {
$vals += 0;
my $err = $self->_frequency_values($vals,$type,$mtype,$wtype,$dtype);
return $err if ($err);
push(@val,$vals);
}
}
$rfield = [ @val ];
}
# Store it
$$self{'data'}{'interval'} = [ @int ];
$$self{'data'}{'rtime'} = [ @rtime ];
# Analyze the rtime to see if it's slow, and to get the number of
# events per interval date.
my $freq = join(':',@int);
my $slow = 0;
my $n = 1;
if (@rtime) {
$freq .= '*';
my (@tmp);
foreach my $rtime (@rtime) {
my @t2;
foreach my $tmp (@$rtime) {
if (ref($tmp)) {
my($a,$b) = @$tmp;
push(@t2,"$a-$b");
$slow = 1;
} else {
push(@t2,$tmp);
}
}
my $tmp = join(',',@t2);
push(@tmp,$tmp);
my $nn = @t2;
$n *= $nn;
}
$freq .= join(':',@tmp);
}
$$self{'data'}{'freq'} = $freq;
$$self{'data'}{'slow'} = $slow;
$$self{'data'}{'ev_per_d'} = $n if (! $slow);
if (@int) {
$$self{'data'}{'noint'} = 0;
while (@int < 7) {
push(@int,0);
}
my $delta = $self->new_delta();
$delta->set('delta',[@int]);
$$self{'data'}{'delta'} = $delta;
} else {
$$self{'data'}{'noint'} = 1;
}
return 0;
}
sub _frequency_values {
my($self,$num,$type,$mtype,$wtype,$dtype) = @_;
my $err;
if ($type eq 'm') {
if ($mtype eq 'moy') {
if ($num < 1) {
$$self{'err'} = "[frequency] Month of year must be 1-12 (zero/negative not allowed)";
return 1;
} elsif ($num > 12) {
$$self{'err'} = "[frequency] Month of year must be 1-12";
return 1;
}
}
return 0;
}
if ($type eq 'w') {
if ($wtype eq 'woy') {
if ($num == 0) {
$$self{'err'} = "[frequency] Week of year must be nonzero";
return 1;
} elsif ($num > 53 || $num < -53) {
$$self{'err'} = "[frequency] Week of year must be 1-53 or -1 to -53";
return 1;
}
} elsif ($wtype eq 'wom') {
if ($num == 0) {
$$self{'err'} = "[frequency] Week of month must be nonzero";
return 1;
} elsif ($num > 5 || $num < -5) {
$$self{'err'} = "[frequency] Week of month must be 1-5 or -1 to -5";
return 1;
}
}
return 0;
}
if ($type eq 'd') {
if ($dtype eq 'dow') {
if ($num < 1) {
$$self{'err'} = "[frequency] Day of week must be 1-7 (zero/negative not allowed)";
return 1;
} elsif ($num > 7) {
$$self{'err'} = "[frequency] Day of week must be 1-7";
return 1;
}
} elsif ($dtype eq 'dom') {
if ($num == 0) {
$$self{'err'} = "[frequency] Day of month must be nonzero";
return 1;
} elsif ($num > 31 || $num < -31) {
$$self{'err'} = "[frequency] Day of month must be 1-31 or -1 to -31";
return 1;
}
} elsif ($dtype eq 'doy') {
if ($num == 0) {
$$self{'err'} = "[frequency] Day of year must be nonzero";
return 1;
} elsif ($num > 366 || $num < -366) {
$$self{'err'} = "[frequency] Day of year must be 1-366 or -1 to -366";
return 1;
}
}
return 0;
}
if ($num < 0) {
$$self{'err'} = "[frequency] Negative values only allowed for day/week";
return 1;
}
return 0;
}
sub _parse_lang {
my($self,$string) = @_;
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
# Test the regular expression
my $rx = $self->_rx('every');
return 1 if ($string !~ $rx);
my($month,$week,$day,$last,$nth,$day_name,$day_abb,$mon_name,$mon_abb,$n,$y) =
@+{qw(month week day last nth day_name day_abb mon_name mon_abb n y)};
# Convert wordlist values to calendar values
my $dow;
if (defined($day_name) || defined($day_abb)) {
if (defined($day_name)) {
$dow = $$dmb{'data'}{'wordmatch'}{'day_name'}{lc($day_name)};
} else {
$dow = $$dmb{'data'}{'wordmatch'}{'day_abb'}{lc($day_abb)};
}
}
my $mmm;
if (defined($mon_name) || defined($mon_abb)) {
if (defined($mon_name)) {
$mmm = $$dmb{'data'}{'wordmatch'}{'month_name'}{lc($mon_name)};
} else {
$mmm = $$dmb{'data'}{'wordmatch'}{'month_abb'}{lc($mon_abb)};
}
}
if (defined($nth)) {
$nth = $$dmb{'data'}{'wordmatch'}{'nth'}{lc($nth)};
}
# Get the frequencies
my($freq);
if (defined($dow)) {
if (defined($mmm)) {
if (defined($last)) {
# last DoW in MMM [YY]
$freq = "1*$mmm:-1:$dow:0:0:0";
} elsif (defined($nth)) {
# Nth DoW in MMM [YY]
$freq = "1*$mmm:$nth:$dow:0:0:0";
} else {
# every DoW in MMM [YY]
$freq = "1*$mmm:1-5:$dow:0:0:0";
}
} else {
if (defined($last)) {
# last DoW in every month [in YY]
$freq = "0:1*-1:$dow:0:0:0";
} elsif (defined($nth)) {
# Nth DoW in every month [in YY]
$freq = "0:1*$nth:$dow:0:0:0";
} else {
# every DoW in every month [in YY]
$freq = "0:1*1-5:$dow:0:0:0";
}
}
} elsif (defined($day)) {
if (defined($month)) {
if (defined($nth)) {
# Nth day of every month [YY]
$freq = "0:1*0:$nth:0:0:0";
} elsif (defined($last)) {
# last day of every month [YY]
$freq = "0:1*0:-1:0:0:0";
} else {
# every day of every month [YY]
$freq = "0:0:0:1*0:0:0";
}
} else {
if (defined($nth)) {
# every Nth day [YY]
$freq = "0:0:0:$nth*0:0:0";
} elsif (defined($n)) {
# every N days [YY]
$freq = "0:0:0:$n*0:0:0";
} else {
# every day [YY]
$freq = "0:0:0:1*0:0:0";
}
}
}
# Get the range (if YY is included)
if (defined($y)) {
$y = $dmt->_fix_year($y);
my $start = "${y}010100:00:00";
my $end = "${y}123123:59:59";
return $self->parse($freq,undef,$start,$end);
}
return $self->frequency($freq)
}
sub _date {
my($self,$op,$date_or_string) = @_;
# Make sure the argument is a date
if (ref($date_or_string) eq 'Date::Manip::Date') {
$$self{'data'}{$op} = $date_or_string;
} elsif (ref($date_or_string)) {
$$self{'err'} = "[$op] Invalid date object";
return 1;
} else {
my $date = $self->new_date();
my $err = $date->parse($date_or_string);
if ($err) {
$$self{'err'} = "[$op] Invalid date string";
return 1;
}
$$self{'data'}{$op} = $date;
}
return 0;
}
sub start {
my($self,$start,$unmod) = @_;
return $$self{'data'}{'start'} if (! defined $start);
$self->_init_dates(1);
$$self{'data'}{'unmod_range'} = $unmod;
$self->_date('start',$start);
}
sub end {
my($self,$end) = @_;
return $$self{'data'}{'end'} if (! defined $end);
$self->_init_dates(1);
$self->_date('end',$end);
}
sub basedate {
my($self,$base) = @_;
return ($$self{'data'}{'base'},$$self{'data'}{'BASE'}) if (! defined $base);
$self->_init_dates();
$self->_date('base',$base);
}
sub modifiers {
my($self,@flags) = @_;
return @{ $$self{'data'}{'flags'} } if (! @flags);
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
if (@flags == 1) {
@flags = split(/,/,lc($flags[0]));
}
# Add these flags to the list
if (@flags && $flags[0] eq "+") {
shift(@flags);
my @tmp = @{ $$self{'data'}{'flags'} };
@flags = (@tmp,@flags) if (@tmp);
}
# Return an error if any modifier is unknown
foreach my $flag (@flags) {
next if ($flag =~ /^([pn][dt][1-7]|wd[1-7]|[fb][dw]\d+|cw[dnp]|[npd]wd|[in]bd|[in]w[1-7]|easter)$/);
$$self{'err'} = "[modifiers] Invalid modifier: $flag";
return 1;
}
$$self{'data'}{'flags'} = [ @flags ];
$self->_init_dates();
return 0;
}
sub nth {
my($self,$n) = @_;
$n = 0 if (! $n);
return ($$self{'data'}{'dates'}{$n},0)
if (exists $$self{'data'}{'dates'}{$n});
my ($err) = $self->_error();
return (undef,$err) if ($err);
return ($$self{'data'}{'dates'}{$n},0)
if (exists $$self{'data'}{'dates'}{$n});
# If there is no interval, then we've found every date that
# can be found.
if ($$self{'data'}{'noint'}) {
return (undef,0);
}
if ($$self{'data'}{'slow'}) {
my $nn = 0;
while (1) {
$self->_nth_interval($nn);
return ($$self{'data'}{'dates'}{$n},0)
if (exists $$self{'data'}{'dates'}{$n});
if ($n >= 0) {
$nn++;
} else {
$nn--;
}
}
} else {
my $nn;
if ($n >= 0) {
$nn = int($n/$$self{'data'}{'ev_per_d'});
} else {
$nn = int(($n+1)/$$self{'data'}{'ev_per_d'}) -1;
}
$self->_nth_interval($nn);
return ($$self{'data'}{'dates'}{$n},0);
}
}
sub next {
my($self) = @_;
my ($err) = $self->_error();
return (undef,$err) if ($err);
# If curr is not set, we have to get it.
if (! defined $$self{'data'}{'curr'}) {
CURR:
while (1) {
# If no interval then
# return base date
if ($$self{'data'}{'noint'}) {
$$self{'data'}{'curr'} = -1;
last CURR;
}
# If a range is defined
# find first event in range and return it
if (defined $$self{'data'}{'start'} &&
defined $$self{'data'}{'end'}) {
my $n = $self->_locate_n('first');
$$self{'data'}{'curr'} = $n-1;
} else {
$$self{'data'}{'curr'} = -1;
}
last CURR;
}
}
# With curr set, find the next defined one
while (1) {
$$self{'data'}{'curr'}++;
if ($$self{'data'}{'noint'}) {
return (undef,0)
if (! exists $$self{'data'}{'dates'}{$$self{'data'}{'curr'}});
}
my ($d,$e) = $self->nth($$self{'data'}{'curr'});
return (undef,$e) if ($e);
return ($d,0) if (defined $d);
}
}
sub prev {
my($self) = @_;
my ($err) = $self->_error();
return (undef,$err) if ($err);
# If curr is not set, we have to get it.
if (! defined $$self{'data'}{'curr'}) {
CURR:
while (1) {
# If no interval then
# return last one
if ($$self{'data'}{'noint'}) {
my @n = sort { $a <=> $b } keys %{ $$self{'data'}{'dates'} };
$$self{'data'}{'curr'} = pop(@n) + 1;
last CURR;
}
# If a range is defined
# find last event in range and return it
if (defined $$self{'data'}{'start'} &&
defined $$self{'data'}{'end'}) {
my $n = $self->_locate_n('last');
$$self{'data'}{'curr'} = $n+1;
} else {
$$self{'data'}{'curr'} = 0;
}
last CURR;
}
}
# With curr set, find the previous defined one
while (1) {
$$self{'data'}{'curr'}--;
if ($$self{'data'}{'noint'}) {
return (undef,0)
if (! exists $$self{'data'}{'dates'}{$$self{'data'}{'curr'}});
}
my ($d,$e) = $self->nth($$self{'data'}{'curr'});
return (undef,$e) if ($e);
return ($d,0) if (defined $d);
}
}
sub dates {
my($self,$start2,$end2,$unmod) = @_;
$self->err(1);
# If $start2 or $end2 are provided, make sure they are valid.
# If either are provided, make a note of it ($tmp_limits).
my $tmp_limits = 0;
$tmp_limits = 1 if ($start2 || $end2);
$unmod = 0 if (! $unmod);
# Check the recurrence for errors. If both $start2 and $end2 are
# provided, it's not necessary for a range to be in the recurrence.
my $range_required;
if (defined($start2) && defined($end2)) {
$range_required = 0;
} else {
$range_required = 1;
}
my($err);
($err,$start2,$end2) = $self->_error($range_required,$start2,$end2);
return () if ($err);
# If $start2 or $end2 were provided, back up the data that applies
# to the current date range, and store the new date range in it's place.
my ($old_start, $old_end, $old_first, $old_last, $old_unmod);
if ($tmp_limits) {
$old_start = $$self{'data'}{'start'};
$old_end = $$self{'data'}{'end'};
$old_first = $$self{'data'}{'first'};
$old_last = $$self{'data'}{'last'};
$old_unmod = $$self{'data'}{'unmod_range'};
$$self{'data'}{'start'} = $start2;
$$self{'data'}{'end'} = $end2;
$$self{'data'}{'first'} = undef;
$$self{'data'}{'last'} = undef;
$$self{'data'}{'unmod_range'} = $unmod;
}
# Get all of the dates
my($end,$first,$last,@dates);
$first = $self->_locate_n('first');
$last = $self->_locate_n('last');
if (defined($first) && defined($last)) {
for (my $n = $first; $n <= $last; $n++) {
my($date,$err) = $self->nth($n);
push(@dates,$date) if (defined $date);
}
}
# Restore the original date range values.
if ($tmp_limits) {
$$self{'data'}{'start'} = $old_start;
$$self{'data'}{'end'} = $old_end;
$$self{'data'}{'first'} = $old_first;
$$self{'data'}{'last'} = $old_last;
$$self{'data'}{'unmod_range'} = $old_unmod;
}
return @dates;
}
########################################################################
# MISC
########################################################################
# This checks a recurrence for errors and completeness prior to
# extracting a date or dates from it.
#
sub _error {
my($self,$range_required,$start2,$end2) = @_;
return ('Invalid recurrence') if ($self->err());
# All dates entered must be valid.
my($start,$end);
if (defined $start2) {
if (ref($start2) eq 'Date::Manip::Date') {
$start = $start2;
} elsif (! ref($start2)) {
$start = $self->new_date();
$start->parse($start2);
} else {
return ('Invalid start argument');
}
return ('Start invalid') if ($start->err());
} elsif (defined $$self{'data'}{'start'}) {
$start = $$self{'data'}{'start'};
return ('Start invalid') if ($start->err());
}
if (defined $end2) {
if (ref($end2) eq 'Date::Manip::Date') {
$end = $end2;
} elsif (! ref($end2)) {
$end = $self->new_date();
$end->parse($end2);
} else {
return ('Invalid end argument');
}
return ('End invalid') if ($end->err());
} elsif (defined $$self{'data'}{'end'}) {
$end = $$self{'data'}{'end'};
return ('End invalid') if ($end->err());
}
if (defined $$self{'data'}{'base'}) {
my $base = $$self{'data'}{'base'};
return ('Base invalid') if ($base->err());
}
# *Y:M:W:D:H:MN:S is complete.
if ($$self{'data'}{'noint'}) {
if ($$self{'data'}{'noint'} == 1) {
my @dates = $self->_apply_rtime_mods();
$$self{'data'}{'noint'} = 2;
my $n = 0;
foreach my $date (@dates) {
next if (! defined $date);
$$self{'data'}{'dates'}{$n++} = $date;
}
return (0,$start,$end) if ($n == 0);
if (defined $start && defined $end) {
my ($first,$last);
for (my $i=0; $i<$n; $i++) {
my $date = $$self{'data'}{'dates'}{$i};
if ($start->cmp($date) <= 0 &&
$end->cmp($date) >= 0) {
$first = $i;
last;
}
}
for (my $i=$n-1; $i>=0; $i--) {
my $date = $$self{'data'}{'dates'}{$i};
if ($start->cmp($date) <= 0 &&
$end->cmp($date) >= 0) {
$last = $i;
last;
}
}
$$self{'data'}{'first'} = $first;
$$self{'data'}{'last'} = $last;
} else {
$$self{'data'}{'first'} = 0;
$$self{'data'}{'last'} = $n-1;
}
}
return (0,$start,$end);
}
# If a range is entered, it must be valid. Also
# a range is required if $range_required is given.
if ($start && $end) {
return ('Range invalid') if ($start->cmp($end) == 1);
} elsif ($range_required) {
return ('Incomplete recurrence');
}
# Check that the base date is available.
$self->_actual_base($start);
if (defined $$self{'data'}{'BASE'}) {
my $base = $$self{'data'}{'BASE'};
return ('Base invalid') if ($base->err());
return (0,$start,$end);
}
return ('Incomplete recurrence');
}
# This determines the actual base date from a specified base date (or
# start date). If a base date cannot be set, then
# $$self{'data'}{'BASE'} is NOT defined.
#
sub _actual_base {
my($self,$start2) = @_;
# Is the actual base date already defined?
return if (defined $$self{'data'}{'BASE'});
# Use the specified base date or start date.
my $base = undef;
if (defined $$self{'data'}{'base'}) {
$base = $$self{'data'}{'base'};
} elsif (defined $start2) {
$base = $start2;
} elsif (defined $$self{'data'}{'start'}) {
$base = $$self{'data'}{'start'};
} else {
return;
}
# Determine the actual base date from the specified base date.
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
$dmt->_update_now(); # Update NOW
my @int = @{ $$self{'data'}{'interval'} };
my @rtime = @{ $$self{'data'}{'rtime'} };
my ($yf,$mf,$wf,$df,$hf,$mnf,$sf) = (@int,@rtime);
my ($y,$m,$d,$h,$mn,$s) = $base->value();
my $BASE = $self->new_date();
my $n = @int;
if ($n == 0) {
# *Y:M:W:D:H:MN:S
return;
} elsif ($n == 1) {
# Y*M:W:D:H:MN:S
$BASE->set('date',[$y,1,1,0,0,0]);
} elsif ($n == 2) {
# Y:M*W:D:H:MN:S
$BASE->set('date',[$y,$m,1,0,0,0]);
} elsif ($n == 3) {
# Y:M:W*D:H:MN:S
my($yy,$w) = $dmb->week_of_year([$y,$m,$d,$h,$mn,$s]);
my($ymd) = $dmb->week_of_year($yy,$w);
$BASE->set('date',[@$ymd,0,0,0]);
} elsif ($n == 4) {
# Y:M:W:D*H:MN:S
$BASE->set('date',[$y,$m,$d,0,0,0]);
} elsif ($n == 5) {
# Y:M:W:D:H*MN:S
$BASE->set('date',[$y,$m,$d,$h,0,0]);
} elsif ($n == 6) {
# Y:M:W:D:H:MN*S
$BASE->set('date',[$y,$m,$d,$h,$mn,0]);
} else {
# Y:M:W:D:H:MN:S
$BASE->set('date',[$y,$m,$d,$h,$mn,$s]);
}
$$self{'data'}{'BASE'} = $BASE;
}
sub _rx {
my($self,$rx) = @_;
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
return $$dmb{'data'}{'rx'}{'recur'}{$rx}
if (exists $$dmb{'data'}{'rx'}{'recur'}{$rx});
if ($rx eq 'std') {
my $l = '[0-9]*';
my $r = '[-,0-9]*';
my $stdrx = "(?<l>$l:$l:$l:$l:$l:$l:$l)(?<r>)|" .
"(?<l>$l:$l:$l:$l:$l:$l)\\*(?<r>$r)|" .
"(?<l>$l:$l:$l:$l:$l)\\*(?<r>$r:$r)|" .
"(?<l>$l:$l:$l:$l)\\*(?<r>$r:$r:$r)|" .
"(?<l>$l:$l:$l)\\*(?<r>$r:$r:$r:$r)|" .
"(?<l>$l:$l)\\*(?<r>$r:$r:$r:$r:$r)|" .
"(?<l>$l)\\*(?<r>$r:$r:$r:$r:$r:$r)|" .
"(?<l>)\\*(?<r>$r:$r:$r:$r:$r:$r:$r)";
$$dmb{'data'}{'rx'}{'recur'}{$rx} = qr/^\s*(?:$stdrx)\s*$/;
} elsif ($rx eq 'rfield' ||
$rx eq 'rnum' ||
$rx eq 'rrange') {
my $num = '[+-]?\d+';
my $range = "$num\-$num";
my $val = "(?:$range|$num)";
my $vals = "$val(?:,$val)*";
$$dmb{'data'}{'rx'}{'recur'}{'rfield'} = qr/^($vals)$/;
$$dmb{'data'}{'rx'}{'recur'}{'rnum'} = qr/^($num)$/;
$$dmb{'data'}{'rx'}{'recur'}{'rrange'} = qr/^($num)\-($num)$/;
} elsif ($rx eq 'each') {
my $each = $$dmb{'data'}{'rx'}{'each'};
my $eachrx = qr/(?:^|\s+)(?:$each)(\s+|$)/i;
$$dmb{'data'}{'rx'}{'recur'}{$rx} = $eachrx;
} elsif ($rx eq 'ignore') {
my $of = $$dmb{'data'}{'rx'}{'of'};
my $on = $$dmb{'data'}{'rx'}{'on'};
my $ignrx = qr/(?:^|\s+)(?:$on|$of)(\s+|$)/i;
$$dmb{'data'}{'rx'}{'recur'}{$rx} = $ignrx;
} elsif ($rx eq 'every') {
my $month = $$dmb{'data'}{'rx'}{'fields'}[2];
my $week = $$dmb{'data'}{'rx'}{'fields'}[3];
my $day = $$dmb{'data'}{'rx'}{'fields'}[4];
my $last = $$dmb{'data'}{'rx'}{'last'};
my $nth = $$dmb{'data'}{'rx'}{'nth'}[0];
my $nth_wom = $$dmb{'data'}{'rx'}{'nth_wom'}[0];
my $nth_dom = $$dmb{'data'}{'rx'}{'nth_dom'}[0];
my $day_abb = $$dmb{'data'}{'rx'}{'day_abb'}[0];
my $day_name = $$dmb{'data'}{'rx'}{'day_name'}[0];
my $mon_abb = $$dmb{'data'}{'rx'}{'month_abb'}[0];
my $mon_name = $$dmb{'data'}{'rx'}{'month_name'}[0];
my $beg = '(?:^|\s+)';
my $end = '(?:\s*$)';
$month = "$beg(?<month>$month)"; # months
$week = "$beg(?<week>$week)"; # weeks
$day = "$beg(?<day>$day)"; # days
$last = "$beg(?<last>$last)"; # last
$nth = "$beg(?<nth>$nth)"; # 1st,2nd,...
$nth_wom = "$beg(?<nth>$nth_wom)"; # 1st - 5th
$nth_dom = "$beg(?<nth>$nth_dom)"; # 1st - 31st
my $n = "$beg(?<n>\\d+)"; # 1,2,...
my $dow = "$beg(?:(?<day_name>$day_name)|(?<day_abb>$day_abb))"; # Sun|Sunday
my $mmm = "$beg(?:(?<mon_name>$mon_name)|(?<mon_abb>$mon_abb))"; # Jan|January
my $y = "(?:$beg(?:(?<y>\\d\\d\\d\\d)|(?<y>\\d\\d)))?";
my $freqrx =
"$nth_wom?$dow$mmm$y|" . # every DoW in MMM [YY]
"$last$dow$mmm$y|" . # Nth DoW in MMM [YY]
# last DoW in MMM [YY]
# day_name|day_abb
# mon_name|mon_abb
# last*|nth*
# y*
"$nth_wom?$dow$month$y|" . # every DoW of every month [YY]
"$last$dow$month$y|" . # Nth DoW of every month [YY]
# last DoW of every month [YY]
# day_name|day_abb
# last*|nth*
# y*
"$nth_dom?$day$month$y|" . # every day of every month [YY]
"$last$day$month$y|" . # Nth day of every month [YY]
# last day of every month [YY]
# day
# month
# nth*|last*
# y*
"$nth*$day$y|" . # every day [YY]
"$n$day$y"; # every Nth day [YY]
# every N days [YY]
# day
# nth*|n*
# y*
$freqrx = qr/^(?:$freqrx)\s*$/i;
$$dmb{'data'}{'rx'}{'recur'}{$rx} = $freqrx;
}
return $$dmb{'data'}{'rx'}{'recur'}{$rx};
}
# @dates = $self->_apply_rtime_mods();
#
# Should only be called if there is no interval (*Y:M:W:D:H:MN:S).
#
# It will use rtime/modifiers to get a list of all events
# specified by the recurrence. This only needs to be done once.
#
# @dates = $self->_apply_rtime_mods($date);
#
# For all other types of recurrences, this will take a single
# date and apply all rtime/modifiers to it to get a list of
# events.
#
sub _apply_rtime_mods {
my($self,$date) = @_;
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
my @int = @{ $$self{'data'}{'interval'} };
my @rtime = @{ $$self{'data'}{'rtime'} };
my $n = @int;
my ($yf,$mf,$wf,$df,$hf,$mnf,$sf) = (@int,@rtime);
my $m_empty = $self->_field_empty($mf);
my $w_empty = $self->_field_empty($wf);
my $d_empty = $self->_field_empty($df);
my ($err,$y,$m,$d,$h,$mn,$s,@y,@m,@w,@d,@h,@mn,@s,@doy,@woy,@dow,@n);
($y,$m,$d,$h,$mn,$s) = $date->value() if (defined $date);
my(@date);
if ($n <= 1) {
#
# *Y:M:W:D:H:MN:S
# Y*M:W:D:H:MN:S
#
if (@int == 0) {
($err,@y) = $self->_rtime_values('y',$yf);
return () if ($err);
} else {
@y = ($y);
}
if ( ($m_empty && $w_empty && $d_empty) ||
(! $m_empty && $w_empty) ) {
# *0:0:0:0 Jan 1 of the current year
# *1:0:0:0 Jan 1, 0001
# *0:2:0:0 Feb 1 of the current year
# *1:2:0:0 Feb 1, 0001
# *0:2:0:4 Feb 4th of the current year
# *1:2:0:4 Feb 4th, 0001
# 1*0:0:0 every year on Jan 1
# 1*2:0:0 every year on Feb 1
# 1*2:0:4 every year on Feb 4th
$mf = [1] if ($m_empty);
$df = [1] if ($d_empty);
($err,@m) = $self->_rtime_values('m',$mf);
return () if ($err);
foreach my $y (@y) {
foreach my $m (@m) {
($err,@d) = $self->_rtime_values('day_of_month',$df,$y,$m);
return () if ($err);
foreach my $d (@d) {
push(@date,[$y,$m,$d,0,0,0]);
}
}
}
} elsif ($m_empty) {
if ($w_empty) {
# *0:0:0:4 the 4th day of the current year
# *1:0:0:4 the 4th day of 0001
# 1*0:0:4 every year on the 4th day of the year
foreach my $y (@y) {
($err,@doy) = $self->_rtime_values('day_of_year',$df,$y);
return () if ($err);
foreach my $doy (@doy) {
my($yy,$mm,$dd) = @{ $dmb->day_of_year($y,$doy) };
push(@date,[$yy,$mm,$dd,0,0,0]);
}
}
} elsif ($d_empty) {
# *0:0:3:0 the first day of the 3rd week of the curr year
# *1:0:3:0 the first day of the 3rd week of 0001
# 1*0:3:0 every year on the first day of 3rd week of year
foreach my $y (@y) {
($err,@woy) = $self->_rtime_values('week_of_year',$wf,$y);
return () if ($err);
foreach my $woy (@woy) {
my ($yy,$mm,$dd) = @{ $dmb->week_of_year($y,$woy) };
push(@date,[$yy,$mm,$dd,0,0,0]);
}
}
} else {
# *1:0:3:4 in 0001 on the 3rd Thur of the year
# *0:0:3:4 on the 3rd Thur of the current year
# 1*0:3:4 every year on the 3rd Thur of the year
($err,@dow) = $self->_rtime_values('day_of_week',$df);
return () if ($err);
foreach my $y (@y) {
foreach my $dow (@dow) {
($err,@n) = $self->_rtime_values('dow_of_year',$wf,$y,$dow);
return () if ($err);
foreach my $n (@n) {
my $ymd = $dmb->nth_day_of_week($y,$n,$dow);
my($yy,$mm,$dd) = @$ymd;
push(@date,[$yy,$mm,$dd,0,0,0]);
}
}
}
}
} else {
# *1:2:3:4 in Feb 0001 on the 3rd Thur of the month
# *0:2:3:4 on the 3rd Thur of Feb in the curr year
# *1:2:3:0 the 3rd occurrence of FirstDay in Feb 0001
# *0:2:3:0 the 3rd occurrence of FirstDay in Feb of curr year
# 1*2:3:4 every year in Feb on the 3rd Thur
# 1*2:3:0 every year on the 3rd occurrence of FirstDay in Feb
($err,@m) = $self->_rtime_values('m',$mf);
return () if ($err);
if ($d_empty) {
@dow = ($dmb->_config('firstday'));
} else {
($err,@dow) = $self->_rtime_values('day_of_week',$df);
return () if ($err);
}
foreach my $y (@y) {
foreach my $m (@m) {
foreach my $dow (@dow) {
($err,@n) = $self->_rtime_values('dow_of_month',
$wf,$y,$m,$dow);
return () if ($err);
foreach my $n (@n) {
my $ymd = $dmb->nth_day_of_week($y,$n,$dow,$m);
my($yy,$mm,$dd) = @$ymd;
push(@date,[$yy,$mm,$dd,0,0,0]);
}
}
}
}
}
} elsif ($n == 2) {
#
# Y:M*W:D:H:MN:S
#
if ($w_empty) {
# 0:2*0:0 every 2 months on the first day of the month
# 0:2*0:4 every 2 months on the 4th day of the month
# 1:2*0:0 every 1 year, 2 months on the first day of the month
# 1:2*0:4 every 1 year, 2 months on the 4th day of the month
$df = [1] if ($d_empty);
($err,@d) = $self->_rtime_values('day_of_month',$df,$y,$m);
return () if ($err);
foreach my $d (@d) {
push(@date,[$y,$m,$d,0,0,0]);
}
} else {
# 0:2*3:0 every 2 months on the 3rd occurrence of FirstDay
# 0:2*3:4 every 2 months on the 3rd Thur of the month
# 1:2*3:0 every 1 year, 2 months on 3rd occurrence of FirstDay
# 1:2*3:4 every 1 year, 2 months on the 3rd Thur of the month
if ($d_empty) {
@dow = ($dmb->_config('firstday'));
} else {
($err,@dow) = $self->_rtime_values('day_of_week',$df);
return () if ($err);
}
foreach my $dow (@dow) {
($err,@n) = $self->_rtime_values('dow_of_month',
$wf,$y,$m,$dow);
return () if ($err);
foreach my $n (@n) {
my $ymd = $dmb->nth_day_of_week($y,$n,$dow,$m);
my($yy,$mm,$dd) = @$ymd;
push(@date,[$yy,$mm,$dd,0,0,0]);
}
}
}
} elsif ($n == 3) {
#
# Y:M:W*D:H:MN:S
#
# 0:0:3*0 every 3 weeks on FirstDay
# 0:0:3*4 every 3 weeks on Thur
# 0:2:3*0 every 2 months, 3 weeks on FirstDay
# 0:2:3*4 every 2 months, 3 weeks on Thur
# 1:0:3*0 every 1 year, 3 weeks on FirstDay
# 1:0:3*4 every 1 year, 3 weeks on Thur
# 1:2:3*0 every 1 year, 2 months, 3 weeks on FirstDay
# 1:2:3*4 every 1 year, 2 months, 3 weeks on Thur
my $fdow = $dmb->_config('firstday');
if ($d_empty) {
@dow = ($fdow);
} else {
($err,@dow) = $self->_rtime_values('day_of_week',$df);
return () if ($err);
}
my($mm,$dd);
my($yy,$ww) = $dmb->week_of_year([$y,$m,$d]);
($yy,$mm,$dd) = @{ $dmb->week_of_year($yy,$ww) };
foreach my $dow (@dow) {
$dow += 7 if ($dow < $fdow);
my($yyy,$mmm,$ddd) = @{ $dmb->calc_date_days([$yy,$mm,$dd],$dow-$fdow) };
push(@date,[$yyy,$mmm,$ddd,0,0,0]);
}
} elsif ($n == 4) {
#
# Y:M:W:D*H:MN:S
#
push(@date,[$y,$m,$d,0,0,0]);
} elsif ($n == 5) {
#
# Y:M:W:D:H*MN:S
#
push(@date,[$y,$m,$d,$h,0,0]);
} elsif ($n == 6) {
#
# Y:M:W:D:H:MN*S
#
push(@date,[$y,$m,$d,$h,$mn,0]);
} elsif ($n == 7) {
#
# Y:M:W:D:H:MN:S
#
push(@date,[$y,$m,$d,$h,$mn,$s]);
}
#
# Handle the H/MN/S portion.
#
# Do hours
if ($n <= 4 ) {
($err,@h) = $self->_rtime_values('h',$hf);
return () if ($err);
$self->_field_add_values(\@date,3,@h);
}
# Do minutes
if ($n <= 5) {
($err,@mn) = $self->_rtime_values('mn',$mnf);
return () if ($err);
$self->_field_add_values(\@date,4,@mn);
}
# Do seconds
if ($n <= 6) {
($err,@s) = $self->_rtime_values('s',$sf);
return () if ($err);
$self->_field_add_values(\@date,5,@s);
}
# Sort the dates... just to be sure.
@date = sort { $dmb->cmp($a,$b) } @date if (@date);
#
# Apply modifiers
#
my @flags = @{ $$self{'data'}{'flags'} };
if (@flags) {
my $obj = $self->new_date();
my @keep;
foreach my $date (@date) {
my ($y,$m,$d,$h,$mn,$s) = @$date;
my $keep = 1;
MODIFIER:
foreach my $flag (@flags) {
my(@wd,$today);
if ($flag =~ /^([pn])([dt])([1-7])$/) {
my($forw,$today,$dow) = ($1,$2,$3);
$forw = ($forw eq 'p' ? 0 : 1);
$today = ($today eq 'd' ? 0 : 1);
($y,$m,$d,$h,$mn,$s) =
@{ $obj->__next_prev([$y,$m,$d,$h,$mn,$s],$forw,$dow,$today) };
} elsif ($flag =~ /^([fb])([dw])(\d+)$/) {
my($prev,$business,$n) = ($1,$2,$3);
$prev = ($prev eq 'b' ? 1 : 0);
$business = ($business eq 'w' ? 1 : 0);
if ($business) {
($y,$m,$d,$h,$mn,$s) =
@{ $obj->__nextprev_business_day($prev,$n,0,[$y,$m,$d,$h,$mn,$s]) };
} else {
($y,$m,$d) = @{ $dmb->calc_date_days([$y,$m,$d],$n,$prev) };
}
} elsif ($flag eq 'ibd' ||
$flag eq 'nbd') {
my $bd = $obj->__is_business_day([$y,$m,$d,$h,$mn,$s],0);
if ( ($flag eq 'ibd' && ! $bd) ||
($flag eq 'nbd' && $bd) ) {
$keep = 0;
last MODIFIER;
}
} elsif ($flag =~ /^([in])w([1-7])$/) {
my($is,$dow) = ($1,$2);
$is = ($is eq 'i' ? 1 : 0);
my $currdow = $dmb->day_of_week([$y,$m,$d]); # Current dow
if ( ($is && $dow != $currdow) ||
(! $is && $dow == $currdow) ) {
$keep = 0;
last MODIFIER;
}
} elsif ($flag =~ /^wd([1-7])$/) {
my $dow = $1; # Dow wanted
my $currdow = $dmb->day_of_week([$y,$m,$d]); # Current dow
if ($dow != $currdow) {
my($yy,$ww) = $dmb->week_of_year([$y,$m,$d]); # What week is this
my $tmp = $dmb->week_of_year($yy,$ww); # First day of week
($y,$m,$d) = @$tmp;
$currdow = $dmb->_config('firstday');
if ($dow > $currdow) {
$tmp = $dmb->calc_date_days([$y,$m,$d],$dow-$currdow);
($y,$m,$d) = @$tmp;
} elsif ($dow < $currdow) {
$tmp = $dmb->calc_date_days([$y,$m,$d],$dow-$currdow+7);
($y,$m,$d) = @$tmp;
}
}
} elsif ($flag eq 'nwd') {
if (! $obj->__is_business_day([$y,$m,$d,$h,$mn,$s],0)) {
($y,$m,$d,$h,$mn,$s) =
@{ $obj->__nextprev_business_day(0,0,0,[$y,$m,$d,$h,$mn,$s]) };
}
} elsif ($flag eq 'pwd') {
if (! $obj->__is_business_day([$y,$m,$d,$h,$mn,$s],0)) {
($y,$m,$d,$h,$mn,$s) =
@{ $obj->__nextprev_business_day(1,1,0,[$y,$m,$d,$h,$mn,$s]) };
}
} elsif ($flag eq 'easter') {
($m,$d) = $self->_easter($y);
} elsif ($flag eq 'dwd' &&
$obj->__is_business_day([$y,$m,$d,$h,$mn,$s],0)) {
# nothing
} else {
if ($flag eq 'cwd' || $flag eq 'dwd') {
if ($dmb->_config('tomorrowfirst')) {
@wd = ([$y,$m,$d,$h,$mn,$s],+1, [$y,$m,$d,$h,$mn,$s],-1);
} else {
@wd = ([$y,$m,$d,$h,$mn,$s],-1, [$y,$m,$d,$h,$mn,$s],+1);
}
} elsif ($flag eq 'cwn') {
@wd = ([$y,$m,$d,$h,$mn,$s],+1, [$y,$m,$d,$h,$mn,$s],-1);
$today = 0;
} elsif ($flag eq 'cwp') {
@wd = ([$y,$m,$d,$h,$mn,$s],-1, [$y,$m,$d,$h,$mn,$s],+1);
$today = 0;
}
while (1) {
my(@d,$off);
# Test in the first direction
@d = @{ $wd[0] };
$off = $wd[1];
@d = @{ $dmb->calc_date_days(\@d,$off) };
if ($obj->__is_business_day(\@d,0)) {
($y,$m,$d,$h,$mn,$s) = @d;
last;
}
$wd[0] = [@d];
# Test in the other direction
@d = @{ $wd[2] };
$off = $wd[3];
@d = @{ $dmb->calc_date_days(\@d,$off) };
if ($obj->__is_business_day(\@d,0)) {
($y,$m,$d,$h,$mn,$s) = @d;
last;
}
$wd[2] = [@d];
}
}
}
if ($keep) {
push(@keep,[$y,$m,$d,$h,$mn,$s]);
}
}
@date = @keep;
}
#
# Convert the dates to objects.
#
my(@ret);
foreach my $date (@date) {
my @d = @$date;
my $obj = $self->new_date();
$obj->set('date',\@d);
if ($obj->err()) {
push(@ret,undef);
} else {
push(@ret,$obj);
}
}
return @ret;
}
# This calculates the Nth interval date (0 is the base date) and then
# calculates the recurring events produced by it.
#
sub _nth_interval {
my($self,$n) = @_;
return if (exists $$self{'data'}{'idate'}{$n});
my $base = $$self{'data'}{'BASE'};
my $date;
# Get the interval date.
if ($n == 0) {
$date = $base;
} else {
my @delta = $$self{'data'}{'delta'}->value;
my $absn = abs($n);
@delta = map { $absn*$_ } @delta;
my $delta = $self->new_delta;
$delta->set('delta',[@delta]);
$date = $base->calc($delta, ($n>0 ? 0 : 2));
}
# For 'slow' recursion, we need to make sure we've got
# the n-1 or n+1 interval as appropriate.
if ($$self{'data'}{'slow'}) {
if ($n > 0) {
$self->_nth_interval($n-1);
} elsif ($n < 0) {
$self->_nth_interval($n+1);
}
}
# Get the list of events associated with this interval date.
my @date = $self->_apply_rtime_mods($date);
# Determine the index of the earliest event associated with
# this interval date.
#
# Events are numbered [$n0...$n1]
my($n0,$n1);
if ($$self{'data'}{'slow'}) {
if (! @date) {
$n0 = undef;
$n1 = undef;
} elsif ($n == 0) {
$n0 = 0;
$n1 = $#date;
} elsif ($n > 0) {
foreach (my $i = $n-1; $i >= 0; $i--) {
next if (! defined $$self{'data'}{'idate'}{$i}[2]);
$n0 = $$self{'data'}{'idate'}{$i}[2] + 1;
last;
}
$n0 = 0 if (! defined $n0);
$n1 = $n0 + $#date;
} else {
foreach (my $i = $n+1; $i <= 0; $i++) {
next if (! defined $$self{'data'}{'idate'}{$i}[1]);
$n1 = $$self{'data'}{'idate'}{$i}[1] - 1;
last;
}
$n1 = -1 if (! defined $n1);
$n0 = $n1 - $#date;
}
} else {
# ev_per_d = 3
# idate = 0 1 2
# events = 0 1 2 3 4 5 6 7 8
# ev_per_d = 3
# idate = -1 -2 -3
# events = -3 -2 -1 -6 -5 -4 -9 -8 -7
$n0 = $n * $$self{'data'}{'ev_per_d'};
$n1 = $n0 + $$self{'data'}{'ev_per_d'} - 1;
}
# Store the dates.
for (my $i=0; $i<=$#date; $i++) {
$$self{'data'}{'dates'}{$n0+$i} = $date[$i];
}
# Store the idate.
if ($$self{'data'}{'slow'}) {
$$self{'data'}{'idate'}{$n} = [$date,$n0,$n1];
} else {
$$self{'data'}{'idate'}{$n} = $date;
}
}
# This locates the first/last event in the range and returns $n. It
# returns undef if there is no date in the range.
#
sub _locate_n {
my($self,$op) = @_;
return $$self{'data'}{$op} if (defined $$self{'data'}{$op} ||
$$self{'data'}{'noint'} == 2);
my $start = $$self{'data'}{'start'};
my $end = $$self{'data'}{'end'};
my $unmod = $$self{'data'}{'unmod_range'};
# Given interval date Idate(n) produces event dates: Date(f)..Date(l)
#
# If we're looking at unmodified dates:
# Find smallest n such that:
# Idate(n) >= start
# first=f
# Then find largest n such that:
# Idate(n) <= end
# last=l
# Otherwise
# Find smallest n such that
# Date(y) >= start
# first=z (smallest z)
# Where x <= z <= y and
# Date(z) >= start
# Then find largest n such that
# Date(x) <= end
# last=z (largest z)
# Where x <= z <= y and
# Date(z) <= end
my($first_int,$last_int,$first,$last);
if ($$self{'data'}{'slow'}) {
#
# For a 'slow' recurrence, we have to start at 0 and work forwards
# or backwards.
#
# Move backwards until we're completely before start
$first_int = 0;
if ($unmod) {
while (1) {
$self->_nth_interval($first_int);
my $date = $$self{'data'}{'idate'}{$first_int}[0];
last if (defined $date && $date->cmp($start) < 0);
$first_int--;
}
} else {
while (1) {
$self->_nth_interval($first_int);
my $ptr = $$self{'data'}{'idate'}{$first_int}[2];
if (defined $ptr) {
my $date = $$self{'data'}{'dates'}{$ptr};
last if (defined $date && $date->cmp($start) < 0);
}
$first_int--;
}
}
# Then move forwards until we're after start
# i.e. Date(y) >= start for modified dates
if ($unmod) {
while (1) {
$self->_nth_interval($first_int);
my $date = $$self{'data'}{'idate'}{$first_int}[0];
last if (defined $date && $date->cmp($start) >= 0);
$first_int++;
}
$first = $$self{'data'}{'idate'}{$first_int}[1];
} else {
while (1) {
$self->_nth_interval($first_int);
my $ptr = $$self{'data'}{'idate'}{$first_int}[2];
if (defined $ptr) {
my $date = $$self{'data'}{'dates'}{$ptr};
last if (defined $date && $date->cmp($start) >= 0);
}
$first_int++;
}
foreach my $i ($$self{'data'}{'idate'}{$first_int}[1] ..
$$self{'data'}{'idate'}{$first_int}[2]) {
my $date = $$self{'data'}{'dates'}{$i};
if (defined $date && $date->cmp($start) >= 0) {
$first = $i;
last;
}
}
}
# Then move forwards until we're after end
# i.e. Date(x) > end for modified dates
$last_int = $first_int;
if ($unmod) {
while (1) {
$self->_nth_interval($last_int);
my $date = $$self{'data'}{'idate'}{$last_int}[0];
last if (defined $date && $date->cmp($end) > 0);
$last_int++;
}
$last_int--;
for (my $i=$$self{'data'}{'idate'}{$last_int}[2];
$i >= $$self{'data'}{'idate'}{$last_int}[1]; $i--) {
my $date = $$self{'data'}{'dates'}{$i};
if (defined $date) {
$last = $i;
last;
}
}
} else {
while (1) {
$self->_nth_interval($last_int);
my $ptr = $$self{'data'}{'idate'}{$last_int}[1];
if (defined $ptr) {
my $date = $$self{'data'}{'dates'}{$ptr};
last if (defined $date && $date->cmp($end) > 0);
}
$last_int++;
}
$last_int--;
$last = undef;
my $i = $first;
while (1) {
last if (! exists $$self{'data'}{'dates'}{$i});
my $date = $$self{'data'}{'dates'}{$i};
next if (! defined $date);
last if ($date->cmp($end) > 0);
$last = $i;
$i++;
}
}
return undef if (! defined $last ||
$last < $first);
$$self{'data'}{'first'} = $first;
$$self{'data'}{'last'} = $last;
return $$self{'data'}{$op}
}
#
# For a normal recurrence, we can estimate which interval date we're
# interested in and then move forward/backward from it.
#
# Calculate the interval date index ($nn) based on the length of
# the delta.
#
# For the Nth interval, the dates produced are:
# N*EV_PER_DAY to (N+1)EV_PER_DAY-1
#
my $base = $$self{'data'}{'BASE'};
my $delta = $$self{'data'}{'delta'};
# $len = 0 is when a recur contains no delta (i.e. *Y:M:W:D:H:Mn:S)
my $len = ($delta ? $delta->printf('%sys') : 0);
my $targ = ($op eq 'first' ? $start : $end);
my $diff = $base->calc($targ);
my $tot = $diff->printf('%sys');
my $nn = ($len ? int($tot/$len) : 1);
my $ev = $$self{'data'}{'ev_per_d'};
# Move backwards until we're completely before start
$first_int = $nn;
if ($unmod) {
while (1) {
$self->_nth_interval($first_int);
my $date = $$self{'data'}{'idate'}{$first_int};
last if (defined $date && $date->cmp($start) < 0);
$first_int--;
}
} else {
LOOP:
while (1) {
$self->_nth_interval($first_int);
for (my $i=($first_int+1)*$ev - 1; $i >= $first_int*$ev; $i--) {
next if (! exists $$self{'data'}{'dates'}{$i});
my $date = $$self{'data'}{'dates'}{$i};
last LOOP if ($date->cmp($start) < 0);
}
$first_int--;
}
}
# Then move forwards until we're after start
# i.e. Date(y) >= start for modified dates
if ($unmod) {
while (1) {
$self->_nth_interval($first_int);
my $date = $$self{'data'}{'idate'}{$first_int};
last if (defined $date && $date->cmp($start) >= 0);
$first_int++;
}
} else {
LOOP:
while (1) {
$self->_nth_interval($first_int);
for (my $i=($first_int+1)*$ev - 1; $i >= $first_int*$ev; $i--) {
next if (! exists $$self{'data'}{'dates'}{$i});
my $date = $$self{'data'}{'dates'}{$i};
last LOOP if ($date->cmp($start) >= 0);
}
$first_int++;
}
}
$first = $first_int*$ev;
# Then move forwards until we're after end
# i.e. Date(y) > end for modified dates
$last_int = $first_int;
if ($unmod) {
while (1) {
$self->_nth_interval($last_int);
my $date = $$self{'data'}{'idate'}{$last_int};
last if (defined $date && $date->cmp($end) > 0);
$last_int++;
}
$last_int--;
} else {
LOOP:
while (1) {
$self->_nth_interval($last_int);
for (my $i=($last_int+1)*$ev - 1; $i >= $last_int*$ev; $i--) {
next if (! exists $$self{'data'}{'dates'}{$i});
my $date = $$self{'data'}{'dates'}{$i};
last LOOP if ($date->cmp($end) >= 0);
}
$last_int++;
}
}
$last = ($last_int+1)*$ev - 1;
# Now get the actual first/last dates
if ($unmod) {
while (1) {
last if (exists $$self{'data'}{'dates'}{$first} &&
defined $$self{'data'}{'dates'}{$first});
$first++;
return undef if ($first > $last);
}
while (1) {
last if (exists $$self{'data'}{'dates'}{$last} &&
defined $$self{'data'}{'dates'}{$last});
$last--;
}
} else {
while (1) {
last if (exists $$self{'data'}{'dates'}{$first} &&
defined $$self{'data'}{'dates'}{$first} &&
$$self{'data'}{'dates'}{$first}->cmp($start) >= 0);
$first++;
return undef if ($first > $last);
}
while (1) {
last if (exists $$self{'data'}{'dates'}{$last} &&
defined $$self{'data'}{'dates'}{$last} &&
$$self{'data'}{'dates'}{$last}->cmp($end) <= 0);
$last--;
}
}
return undef if (! defined $last ||
$last < $first);
$$self{'data'}{'first'} = $first;
$$self{'data'}{'last'} = $last;
return $$self{'data'}{$op}
}
# This returns the date easter occurs on for a given year as ($month,$day).
# This is from the Calendar FAQ.
#
sub _easter {
my($self,$y) = @_;
my($c) = $y/100;
my($g) = $y % 19;
my($k) = ($c-17)/25;
my($i) = ($c - $c/4 - ($c-$k)/3 + 19*$g + 15) % 30;
$i = $i - ($i/28)*(1 - ($i/28)*(29/($i+1))*((21-$g)/11));
my($j) = ($y + $y/4 + $i + 2 - $c + $c/4) % 7;
my($l) = $i-$j;
my($m) = 3 + ($l+40)/44;
my($d) = $l + 28 - 31*($m/4);
return ($m,$d);
}
# This returns 1 if a field is empty.
#
sub _field_empty {
my($self,$val) = @_;
if (ref($val)) {
my @tmp = @$val;
return 1 if ($#tmp == -1 ||
($#tmp == 0 && ! ref($tmp[0]) && ! $tmp[0]));
return 0;
} else {
return $val;
}
}
# This returns a list of values that appear in a field in the rtime.
#
# $val is a listref, with each element being a value or a range.
#
# Usage:
# _rtime_values('y' ,$y);
# _rtime_values('m' ,$m);
# _rtime_values('week_of_year' ,$w ,$y);
# _rtime_values('dow_of_year' ,$w ,$y,$dow);
# _rtime_values('dow_of_month' ,$w ,$y,$m,$dow);
# _rtime_values('day_of_year' ,$d ,$y);
# _rtime_values('day_of_month' ,$d ,$y,$m);
# _rtime_values('day_of_week' ,$d);
# _rtime_values('h' ,$h);
# _rtime_values('mn' ,$mn);
# _rtime_values('s' ,$s);
#
# Returns ($err,@vals)
#
sub _rtime_values {
my($self,$type,$val,@args) = @_;
my $dmt = $$self{'tz'};
my $dmb = $$dmt{'base'};
if ($type eq 'h') {
@args = (0,0,23,23);
} elsif ($type eq 'mn') {
@args = (0,0,59,59);
} elsif ($type eq 's') {
@args = (0,0,59,59);
} elsif ($type eq 'y') {
my $curry = $dmt->_now('y',1);
foreach my $y (@$val) {
$y = $curry if (! ref($y) && $y==0);
}
@args = (0,1,9999,9999);
} elsif ($type eq 'm') {
@args = (0,1,12,12);
} elsif ($type eq 'week_of_year') {
my($y) = @args;
my $wiy = $dmb->weeks_in_year($y);
@args = (1,1,$wiy,53);
} elsif ($type eq 'dow_of_year') {
my($y,$dow) = @args;
# Get the 1st occurrence of $dow
my $d0 = 1;
my $dow0 = $dmb->day_of_week([$y,1,$d0]);
if ($dow > $dow0) {
$d0 += ($dow-$dow0);
} elsif ($dow < $dow0) {
$d0 += 7-($dow0-$dow);
}
# Get the last occurrence of $dow
my $d1 = 31;
my $dow1 = $dmb->day_of_week([$y,12,$d1]);
if ($dow1 > $dow) {
$d1 -= ($dow1-$dow);
} elsif ($dow1 < $dow) {
$d1 -= 7-($dow-$dow1);
}
# Find out the number of occurrenced of $dow
my $doy1 = $dmb->day_of_year([$y,12,$d1]);
my $n = ($doy1 - $d0)/7 + 1;
# Get the list of @w
@args = (1,1,$n,53);
} elsif ($type eq 'dow_of_month') {
my($y,$m,$dow) = @args;
# Get the 1st occurrence of $dow in the month
my $d0 = 1;
my $dow0 = $dmb->day_of_week([$y,$m,$d0]);
if ($dow > $dow0) {
$d0 += ($dow-$dow0);
} elsif ($dow < $dow0) {
$d0 += 7-($dow0-$dow);
}
# Get the last occurrence of $dow
my $d1 = $dmb->days_in_month($y,$m);
my $dow1 = $dmb->day_of_week([$y,$m,$d1]);
if ($dow1 > $dow) {
$d1 -= ($dow1-$dow);
} elsif ($dow1 < $dow) {
$d1 -= 7-($dow-$dow1);
}
# Find out the number of occurrenced of $dow
my $n = ($d1 - $d0)/7 + 1;
# Get the list of @w
@args = (1,1,$n,5);
} elsif ($type eq 'day_of_year') {
my($y) = @args;
my $diy = $dmb->days_in_year($y);
@args = (1,1,$diy,366);
} elsif ($type eq 'day_of_month') {
my($y,$m) = @args;
my $dim = $dmb->days_in_month($y,$m);
@args = (1,1,$dim,31);
} elsif ($type eq 'day_of_week') {
@args = (0,1,7,7);
}
my($err,@vals) = $self->__rtime_values($val,@args);
if ($err) {
$$self{'err'} = "[dates] $err [$type]";
return (1);
}
return(0,@vals);
}
# This returns the raw values for a list.
#
# If $allowneg is 0, only positive numbers are allowed, and they must be
# in the range [$min,$absmax]. If $allowneg is 1, positive numbers in the
# range [$min,$absmax] and negative numbers in the range [-$absmax,-$min]
# are allowed. An error occurs if a value falls outside the range.
#
# Only values in the range of [$min,$max] are actually kept. This allows
# a recurrence for day_of_month to be 1-31 and not fail for a month that
# has fewer than 31 days. Any value outside the [$min,$max] are silently
# discarded.
#
# Returns:
# ($err,@vals)
#
sub __rtime_values {
my($self,$vals,$allowneg,$min,$max,$absmax) = @_;
my(@ret);
foreach my $val (@$vals) {
if (ref($val)) {
my($val1,$val2) = @$val;
if ($allowneg) {
return ('Value outside range')
if ( ($val1 >= 0 && ($val1 < $min || $val1 > $absmax) ) ||
($val2 >= 0 && ($val2 < $min || $val2 > $absmax) ) );
return ('Negative value outside range')
if ( ($val1 <= 0 && ($val1 < -$absmax || $val1 > -$min) ) ||
($val2 <= 0 && ($val2 < -$absmax || $val2 > -$min) ) );
} else {
return ('Value outside range')
if ( ($val1 < $min || $val1 > $absmax) ||
($val2 < $min || $val2 > $absmax) );
}
return ('Range values reversed')
if ( ($val1 <= 0 && $val2 <= 0 && $val1 > $val2) ||
($val1 >= 0 && $val2 >= 0 && $val1 > $val2) );
# Use $max instead of $absmax when converting negative numbers to
# positive ones.
$val1 = $max + $val1 + 1 if ($val1 < 0); # day -10
$val2 = $max + $val2 + 1 if ($val2 < 0);
$val1 = $min if ($val1 < $min); # day -31 in a 30 day month
$val2 = $max if ($val2 > $max);
next if ($val1 > $val2);
push(@ret,$val1..$val2);
} else {
if ($allowneg) {
return ('Value outside range')
if ($val >= 0 && ($val < $min || $val > $absmax));
return ('Negative value outside range')
if ($val <= 0 && ($val < -$absmax || $val > -$min));
} else {
return ('Value outside range')
if ($val < $min || $val > $absmax);
}
# Use $max instead of $absmax when converting negative numbers to
# positive ones.
my $ret;
if ($val < 0 ) {
$ret = $max + $val + 1;
} else {
$ret = $val;
}
next if ($ret > $max || $ret < $min);
push(@ret,$ret);
}
}
return ('',@ret);
}
# This takes a list of dates (each a listref of [y,m,d,h,mn,s]) and replaces
# the Nth field with all of the possible values passed in, creating a new
# list with all the dates.
#
sub _field_add_values {
my($self,$datesref,$n,@val) = @_;
my @dates = @$datesref;
my @tmp;
foreach my $date (@dates) {
my @d = @$date;
foreach my $val (@val) {
$d[$n] = $val;
push(@tmp,[@d]);
}
}
@$datesref = @tmp;
}
1;
# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# cperl-indent-level: 3
# cperl-continued-statement-offset: 2
# cperl-continued-brace-offset: 0
# cperl-brace-offset: 0
# cperl-brace-imaginary-offset: 0
# cperl-label-offset: 0
# End:
|