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 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/dtrule.h"
#include "unicode/tzrule.h"
#include "unicode/rbtz.h"
#include "unicode/simpletz.h"
#include "unicode/tzrule.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/strenum.h"
#include "unicode/ucal.h"
#include "unicode/unistr.h"
#include "unicode/ustring.h"
#include "unicode/tztrans.h"
#include "unicode/vtzone.h"
#include "tzrulets.h"
#include "zrule.h"
#include "ztrans.h"
#include "vzone.h"
#include "cmemory.h"
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
#define HOUR (60*60*1000)
static const char *const TESTZIDS[] = {
"AGT",
"America/New_York",
"America/Los_Angeles",
"America/Indiana/Indianapolis",
"America/Havana",
"Europe/Lisbon",
"Europe/Paris",
"Asia/Tokyo",
"Asia/Sakhalin",
"Africa/Cairo",
"Africa/Windhoek",
"Australia/Sydney",
"Etc/GMT+8"
};
static UBool hasEquivalentTransitions(/*const*/ BasicTimeZone& tz1, /*const*/BasicTimeZone& tz2,
UDate start, UDate end,
UBool ignoreDstAmount, int32_t maxTransitionTimeDelta,
UErrorCode& status);
class TestZIDEnumeration : public StringEnumeration {
public:
TestZIDEnumeration(UBool all = false);
~TestZIDEnumeration();
virtual int32_t count(UErrorCode& /*status*/) const override {
return len;
}
virtual const UnicodeString *snext(UErrorCode& status) override;
virtual void reset(UErrorCode& status) override;
static inline UClassID getStaticClassID() {
return (UClassID)&fgClassID;
}
virtual UClassID getDynamicClassID() const override {
return getStaticClassID();
}
private:
static const char fgClassID;
int32_t idx;
int32_t len;
StringEnumeration *tzenum;
};
const char TestZIDEnumeration::fgClassID = 0;
TestZIDEnumeration::TestZIDEnumeration(UBool all)
: idx(0) {
UErrorCode status = U_ZERO_ERROR;
if (all) {
tzenum = TimeZone::createEnumeration(status);
len = tzenum->count(status);
} else {
tzenum = nullptr;
len = UPRV_LENGTHOF(TESTZIDS);
}
}
TestZIDEnumeration::~TestZIDEnumeration() {
delete tzenum;
}
const UnicodeString*
TestZIDEnumeration::snext(UErrorCode& status) {
if (tzenum != nullptr) {
return tzenum->snext(status);
} else if (U_SUCCESS(status) && idx < len) {
unistr = UnicodeString(TESTZIDS[idx++], "");
return &unistr;
}
return nullptr;
}
void
TestZIDEnumeration::reset(UErrorCode& status) {
if (tzenum != nullptr) {
tzenum->reset(status);
} else {
idx = 0;
}
}
void TimeZoneRuleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) {
logln("TestSuite TestTimeZoneRule");
}
switch (index) {
CASE(0, TestSimpleRuleBasedTimeZone);
CASE(1, TestHistoricalRuleBasedTimeZone);
CASE(2, TestOlsonTransition);
CASE(3, TestRBTZTransition);
CASE(4, TestHasEquivalentTransitions);
CASE(5, TestVTimeZoneRoundTrip);
CASE(6, TestVTimeZoneRoundTripPartial);
CASE(7, TestVTimeZoneSimpleWrite);
CASE(8, TestVTimeZoneHeaderProps);
CASE(9, TestGetSimpleRules);
CASE(10, TestTimeZoneRuleCoverage);
CASE(11, TestSimpleTimeZoneCoverage);
CASE(12, TestVTimeZoneCoverage);
CASE(13, TestVTimeZoneParse);
CASE(14, TestT6216);
CASE(15, TestT6669);
CASE(16, TestVTimeZoneWrapper);
CASE(17, TestT8943);
default: name = ""; break;
}
}
/*
* Compare SimpleTimeZone with equivalent RBTZ
*/
void
TimeZoneRuleTest::TestSimpleRuleBasedTimeZone() {
UErrorCode status = U_ZERO_ERROR;
SimpleTimeZone stz(-1*HOUR, "TestSTZ",
UCAL_SEPTEMBER, -30, -UCAL_SATURDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
UCAL_FEBRUARY, 2, UCAL_SUNDAY, 1*HOUR, SimpleTimeZone::WALL_TIME,
1*HOUR, status);
if (U_FAILURE(status)) {
errln("FAIL: Couldn't create SimpleTimezone.");
}
DateTimeRule *dtr;
AnnualTimeZoneRule *atzr;
int32_t STARTYEAR = 2000;
InitialTimeZoneRule *ir = new InitialTimeZoneRule(
"RBTZ_Initial", // Initial time Name
-1*HOUR, // Raw offset
1*HOUR); // DST saving amount
// Original rules
RuleBasedTimeZone *rbtz1 = new RuleBasedTimeZone("RBTZ1", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, false,
1*HOUR, DateTimeRule::WALL_TIME); // SUN<=30 in September, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_DST1",
-1*HOUR /*rawOffset*/, 1*HOUR /*dstSavings*/, dtr,
STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
1*HOUR, DateTimeRule::WALL_TIME); // 2nd Sunday in February, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_STD1",
-1*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr,
STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1-2.");
}
rbtz1->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 1.");
}
// Equivalent, but different date rule type
RuleBasedTimeZone *rbtz2 = new RuleBasedTimeZone("RBTZ2", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, -1, UCAL_SATURDAY,
1*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in September at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_DST2", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz2->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 8, UCAL_SUNDAY, true,
1*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in February, at 1AM wall time
atzr = new AnnualTimeZoneRule("RBTZ_STD2", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz2->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2-2.");
}
rbtz2->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 2");
}
// Equivalent, but different time rule type
RuleBasedTimeZone *rbtz3 = new RuleBasedTimeZone("RBTZ3", ir->clone());
dtr = new DateTimeRule(UCAL_SEPTEMBER, 30, UCAL_SATURDAY, false,
2*HOUR, DateTimeRule::UTC_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST3", -1*HOUR, 1*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz3->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3-1.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 2, UCAL_SUNDAY,
0*HOUR, DateTimeRule::STANDARD_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD3", -1*HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz3->addTransitionRule(atzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3-2.");
}
rbtz3->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ 3");
}
// Check equivalency for 10 years
UDate start = getUTCMillis(STARTYEAR, UCAL_JANUARY, 1);
UDate until = getUTCMillis(STARTYEAR + 10, UCAL_JANUARY, 1);
if (!(stz.hasEquivalentTransitions(*rbtz1, start, until, true, status))) {
errln("FAIL: rbtz1 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
if (!(stz.hasEquivalentTransitions(*rbtz2, start, until, true, status))) {
errln("FAIL: rbtz2 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
if (!(stz.hasEquivalentTransitions(*rbtz3, start, until, true, status))) {
errln("FAIL: rbtz3 must be equivalent to the SimpleTimeZone in the time range.");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions");
}
// hasSameRules
if (rbtz1->hasSameRules(*rbtz2)) {
errln("FAIL: rbtz1 and rbtz2 have different rules, but returned true.");
}
if (rbtz1->hasSameRules(*rbtz3)) {
errln("FAIL: rbtz1 and rbtz3 have different rules, but returned true.");
}
RuleBasedTimeZone *rbtz1c = rbtz1->clone();
if (!rbtz1->hasSameRules(*rbtz1c)) {
errln("FAIL: Cloned RuleBasedTimeZone must have the same rules with the original.");
}
// getOffset
int32_t era, year, month, dayOfMonth, dayOfWeek, millisInDay;
UDate time;
int32_t offset, dstSavings;
UBool dst;
GregorianCalendar *cal = new GregorianCalendar(status);
if (U_FAILURE(status)) {
dataerrln("FAIL: Could not create a Gregorian calendar instance.: %s", u_errorName(status));
delete rbtz1;
delete rbtz2;
delete rbtz3;
delete rbtz1c;
return;
}
cal->setTimeZone(*rbtz1);
cal->clear();
// Jan 1, 1000 BC
cal->set(UCAL_ERA, GregorianCalendar::BC);
cal->set(1000, UCAL_JANUARY, 1);
era = cal->get(UCAL_ERA, status);
year = cal->get(UCAL_YEAR, status);
month = cal->get(UCAL_MONTH, status);
dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
time = cal->getTime(status);
if (U_FAILURE(status)) {
errln("FAIL: Could not get calendar field values.");
}
offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed.");
}
if (offset != 0) {
errln(UnicodeString("FAIL: Invalid time zone offset: ") + offset + " /expected: 0");
}
dst = rbtz1->inDaylightTime(time, status);
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed.");
}
if (!dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1->getOffset(time, true, offset, dstSavings, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone raw offset: ") + offset + " /expected: -3600000");
}
if (dstSavings != 3600000) {
errln(UnicodeString("FAIL: Invalid DST amount: ") + dstSavings + " /expected: 3600000");
}
// July 1, 2000, AD
cal->set(UCAL_ERA, GregorianCalendar::AD);
cal->set(2000, UCAL_JULY, 1);
era = cal->get(UCAL_ERA, status);
year = cal->get(UCAL_YEAR, status);
month = cal->get(UCAL_MONTH, status);
dayOfMonth = cal->get(UCAL_DAY_OF_MONTH, status);
dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
millisInDay = cal->get(UCAL_MILLISECONDS_IN_DAY, status);
time = cal->getTime(status);
if (U_FAILURE(status)) {
errln("FAIL: Could not get calendar field values.");
}
offset = rbtz1->getOffset(era, year, month, dayOfMonth, dayOfWeek, millisInDay, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone offset: ") + offset + " /expected: -3600000");
}
dst = rbtz1->inDaylightTime(time, status);
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed.");
}
if (dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1->getOffset(time, true, offset, dstSavings, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed.");
}
if (offset != -3600000) {
errln(UnicodeString("FAIL: Invalid time zone raw offset: ") + offset + " /expected: -3600000");
}
if (dstSavings != 0) {
errln(UnicodeString("FAIL: Invalid DST amount: ") + dstSavings + " /expected: 0");
}
// getRawOffset
offset = rbtz1->getRawOffset();
if (offset != -1*HOUR) {
errln(UnicodeString("FAIL: Invalid time zone raw offset returned by getRawOffset: ")
+ offset + " /expected: -3600000");
}
// operator=/==/!=
RuleBasedTimeZone rbtz0("RBTZ1", ir->clone());
if (rbtz0 == *rbtz1 || !(rbtz0 != *rbtz1)) {
errln("FAIL: RuleBasedTimeZone rbtz0 is not equal to rbtz1, but got wrong result");
}
rbtz0 = *rbtz1;
if (rbtz0 != *rbtz1 || !(rbtz0 == *rbtz1)) {
errln("FAIL: RuleBasedTimeZone rbtz0 is equal to rbtz1, but got wrong result");
}
// setRawOffset
const int32_t RAW = -10*HOUR;
rbtz0.setRawOffset(RAW);
if (rbtz0.getRawOffset() != RAW) {
logln("setRawOffset is implemented in RuleBasedTimeZone");
}
// useDaylightTime
if (!rbtz1->useDaylightTime()) {
errln("FAIL: useDaylightTime returned false");
}
// Try to add 3rd final rule
dtr = new DateTimeRule(UCAL_OCTOBER, 15, 1*HOUR, DateTimeRule::WALL_TIME);
atzr = new AnnualTimeZoneRule("3RD_ATZ", -1*HOUR, 2*HOUR, dtr, STARTYEAR, AnnualTimeZoneRule::MAX_YEAR);
rbtz1->addTransitionRule(atzr, status);
if (U_SUCCESS(status)) {
errln("FAIL: 3rd final rule must be rejected");
}
// Try to add an initial rule
InitialTimeZoneRule *ir1 = new InitialTimeZoneRule("Test Initial", 2*HOUR, 0);
rbtz1->addTransitionRule(ir1, status);
if (U_SUCCESS(status)) {
errln("FAIL: InitialTimeZoneRule must be rejected");
}
delete ir;
delete rbtz1;
delete rbtz2;
delete rbtz3;
delete rbtz1c;
delete cal;
}
/*
* Test equivalency between OlsonTimeZone and custom RBTZ representing the
* equivalent rules in a certain time range
*/
void
TimeZoneRuleTest::TestHistoricalRuleBasedTimeZone() {
UErrorCode status = U_ZERO_ERROR;
// Compare to America/New_York with equivalent RBTZ
BasicTimeZone *ny = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/New_York"));
//RBTZ
InitialTimeZoneRule *ir = new InitialTimeZoneRule("EST", -5*HOUR, 0);
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone("EST5EDT", ir);
DateTimeRule *dtr;
AnnualTimeZoneRule *tzr;
// Standard time
dtr = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in October, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR /*rawOffset*/, 0 /*dstSavings*/, dtr, 1967, 2006);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 1.");
}
dtr = new DateTimeRule(UCAL_NOVEMBER, 1, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in November, at 2AM wall time
tzr = new AnnualTimeZoneRule("EST", -5*HOUR, 0, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 2.");
}
// Daylight saving time
dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1967, 1973);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 3.");
}
dtr = new DateTimeRule(UCAL_JANUARY, 6,
2*HOUR, DateTimeRule::WALL_TIME); // January 6, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1974, 1974);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 4.");
}
dtr = new DateTimeRule(UCAL_FEBRUARY, 23,
2*HOUR, DateTimeRule::WALL_TIME); // February 23, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1975, 1975);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 5.");
}
dtr = new DateTimeRule(UCAL_APRIL, -1, UCAL_SUNDAY,
2*HOUR, DateTimeRule::WALL_TIME); // Last Sunday in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1976, 1986);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 6.");
}
dtr = new DateTimeRule(UCAL_APRIL, 1, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=1 in April, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 1987, 2006);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
}
dtr = new DateTimeRule(UCAL_MARCH, 8, UCAL_SUNDAY,
true, 2*HOUR, DateTimeRule::WALL_TIME); // SUN>=8 in March, at 2AM wall time
tzr = new AnnualTimeZoneRule("EDT", -5*HOUR, 1*HOUR, dtr, 2007, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(tzr, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add AnnualTimeZoneRule 7.");
}
rbtz->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete RBTZ.");
}
// hasEquivalentTransitions
UDate jan1_1950 = getUTCMillis(1950, UCAL_JANUARY, 1);
UDate jan1_1967 = getUTCMillis(1971, UCAL_JANUARY, 1);
UDate jan1_2010 = getUTCMillis(2010, UCAL_JANUARY, 1);
if (!ny->hasEquivalentTransitions(*rbtz, jan1_1967, jan1_2010, true, status)) {
dataerrln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1967-2010");
}
if (ny->hasEquivalentTransitions(*rbtz, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for ny/rbtz 1950-2010");
}
// Same with above, but calling RBTZ#hasEquivalentTransitions against OlsonTimeZone
if (!rbtz->hasEquivalentTransitions(*ny, jan1_1967, jan1_2010, true, status)) {
dataerrln("FAIL: The RBTZ must be equivalent to America/New_York between 1967 and 2010 ");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1967-2010");
}
if (rbtz->hasEquivalentTransitions(*ny, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: The RBTZ must not be equivalent to America/New_York between 1950 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/ny 1950-2010");
}
// TimeZone APIs
if (ny->hasSameRules(*rbtz) || rbtz->hasSameRules(*ny)) {
errln("FAIL: hasSameRules must return false");
}
RuleBasedTimeZone *rbtzc = rbtz->clone();
if (!rbtz->hasSameRules(*rbtzc) || !rbtz->hasEquivalentTransitions(*rbtzc, jan1_1950, jan1_2010, true, status)) {
errln("FAIL: hasSameRules/hasEquivalentTransitions must return true for cloned RBTZs");
}
if (U_FAILURE(status)) {
errln("FAIL: error returned from hasEquivalentTransitions for rbtz/rbtzc 1950-2010");
}
UDate times[] = {
getUTCMillis(2006, UCAL_MARCH, 15),
getUTCMillis(2006, UCAL_NOVEMBER, 1),
getUTCMillis(2007, UCAL_MARCH, 15),
getUTCMillis(2007, UCAL_NOVEMBER, 1),
getUTCMillis(2008, UCAL_MARCH, 15),
getUTCMillis(2008, UCAL_NOVEMBER, 1),
0
};
int32_t offset1, dst1;
int32_t offset2, dst2;
for (int i = 0; times[i] != 0; i++) {
// Check getOffset - must return the same results for these time data
rbtz->getOffset(times[i], false, offset1, dst1, status);
if (U_FAILURE(status)) {
errln("FAIL: rbtz->getOffset failed");
}
ny->getOffset(times[i], false, offset2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: ny->getOffset failed");
}
if (offset1 != offset2 || dst1 != dst2) {
dataerrln("FAIL: Incompatible time zone offset/dstSavings for ny and rbtz");
}
// Check inDaylightTime
if (rbtz->inDaylightTime(times[i], status) != ny->inDaylightTime(times[i], status)) {
dataerrln("FAIL: Incompatible daylight saving time for ny and rbtz");
}
if (U_FAILURE(status)) {
errln("FAIL: inDaylightTime failed");
}
}
delete ny;
delete rbtz;
delete rbtzc;
}
/*
* Check if transitions returned by getNextTransition/getPreviousTransition
* are actual time transitions.
*/
void
TimeZoneRuleTest::TestOlsonTransition() {
const int32_t TESTYEARS[][2] = {
{1895, 1905}, // including int32 minimum second
{1965, 1975}, // including the epoch
{1995, 2015}, // practical year range
{0,0}
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
for (int32_t i = 0; TESTYEARS[i][0] != 0 || TESTYEARS[i][1] != 0; i++) {
UDate lo = getUTCMillis(TESTYEARS[i][0], UCAL_JANUARY, 1);
UDate hi = getUTCMillis(TESTYEARS[i][1], UCAL_JANUARY, 1);
verifyTransitions(*tz, lo, hi);
}
delete tz;
}
}
/*
* Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
* transitions.
*/
void
TimeZoneRuleTest::TestRBTZTransition() {
const int32_t STARTYEARS[] = {
1900,
1960,
1990,
2010,
0
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
int32_t ruleCount = tz->countTransitionRules(status);
const InitialTimeZoneRule *initial;
const TimeZoneRule **trsrules = new const TimeZoneRule*[ruleCount];
tz->getTimeZoneRules(initial, trsrules, ruleCount, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to get the TimeZoneRules from time zone ") + *tzid);
}
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(*tzid, initial->clone());
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to get the transition rule count from time zone ") + *tzid);
}
for (int32_t i = 0; i < ruleCount; i++) {
rbtz->addTransitionRule(trsrules[i]->clone(), status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: failed to add a transition rule at index ") + i + " to the RBTZ for " + *tzid);
}
}
rbtz->complete(status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: complete() failed for the RBTZ for ") + *tzid);
}
for (int32_t idx = 0; STARTYEARS[idx] != 0; idx++) {
UDate start = getUTCMillis(STARTYEARS[idx], UCAL_JANUARY, 1);
UDate until = getUTCMillis(STARTYEARS[idx] + 20, UCAL_JANUARY, 1);
// Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
// Ascending
compareTransitionsAscending(*tz, *rbtz, start, until, false);
// Ascending/inclusive
compareTransitionsAscending(*tz, *rbtz, start + 1, until, true);
// Descending
compareTransitionsDescending(*tz, *rbtz, start, until, false);
// Descending/inclusive
compareTransitionsDescending(*tz, *rbtz, start + 1, until, true);
}
delete [] trsrules;
delete rbtz;
delete tz;
}
}
void
TimeZoneRuleTest::TestHasEquivalentTransitions() {
// America/New_York and America/Indiana/Indianapolis are equivalent
// since 2006
UErrorCode status = U_ZERO_ERROR;
BasicTimeZone *newyork = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/New_York"));
BasicTimeZone *indianapolis = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/Indiana/Indianapolis"));
BasicTimeZone *gmt_5 = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("Etc/GMT+5"));
UDate jan1_1971 = getUTCMillis(1971, UCAL_JANUARY, 1);
UDate jan1_2005 = getUTCMillis(2005, UCAL_JANUARY, 1);
UDate jan1_2006 = getUTCMillis(2006, UCAL_JANUARY, 1);
UDate jan1_2007 = getUTCMillis(2007, UCAL_JANUARY, 1);
UDate jan1_2011 = getUTCMillis(2010, UCAL_JANUARY, 1);
if (newyork->hasEquivalentTransitions(*indianapolis, jan1_2005, jan1_2011, true, status)) {
dataerrln("FAIL: New_York is not equivalent to Indianapolis between 2005 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (!newyork->hasEquivalentTransitions(*indianapolis, jan1_2006, jan1_2011, true, status)) {
errln("FAIL: New_York is equivalent to Indianapolis between 2006 and 2010");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (!indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2006, true, status)) {
errln("FAIL: Indianapolis is equivalent to GMT+5 between 1971 and 2005");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
if (indianapolis->hasEquivalentTransitions(*gmt_5, jan1_1971, jan1_2007, true, status)) {
dataerrln("FAIL: Indianapolis is not equivalent to GMT+5 between 1971 and 2006");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
// Cloned TimeZone
BasicTimeZone *newyork2 = newyork->clone();
if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, false, status)) {
errln("FAIL: Cloned TimeZone must have the same transitions");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
}
if (!newyork->hasEquivalentTransitions(*newyork2, jan1_1971, jan1_2011, true, status)) {
errln("FAIL: Cloned TimeZone must have the same transitions");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/newyork2");
}
// America/New_York and America/Los_Angeles has same DST start rules, but
// raw offsets are different
BasicTimeZone *losangeles = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone("America/Los_Angeles"));
if (newyork->hasEquivalentTransitions(*losangeles, jan1_2006, jan1_2011, true, status)) {
dataerrln("FAIL: New_York is not equivalent to Los Angeles, but returned true");
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition for newyork/losangeles");
}
delete newyork;
delete newyork2;
delete indianapolis;
delete gmt_5;
delete losangeles;
}
/*
* Write out time zone rules of OlsonTimeZone into VTIMEZONE format, create a new
* VTimeZone from the VTIMEZONE data, then compare transitions
*/
void
TimeZoneRuleTest::TestVTimeZoneRoundTrip() {
UDate startTime = getUTCMillis(1850, UCAL_JANUARY, 1);
UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
vtz_org->setTZURL("http://source.icu-project.org/timezone");
vtz_org->setLastModified(Calendar::getNow());
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
// Write out VTIMEZONE data
vtz_org->write(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + " into VTIMEZONE format.");
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while reading VTIMEZONE data for ") + *tzid);
} else {
// Write out VTIMEZONE one more time
UnicodeString vtzdata1;
vtz_new->write(vtzdata1, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + "(vtz_new) into VTIMEZONE format.");
} else {
// Make sure VTIMEZONE data is exactly same with the first one
if (vtzdata != vtzdata1) {
errln(UnicodeString("FAIL: different VTIMEZONE data after round trip for ") + *tzid);
}
}
// Check equivalency after the first transition.
// The DST information before the first transition might be lost
// because there is no good way to represent the initial time with
// VTIMEZONE.
int32_t raw1, raw2, dst1, dst2;
tz->getOffset(startTime, false, raw1, dst1, status);
vtz_new->getOffset(startTime, false, raw2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from getOffset");
} else {
if (raw1 + dst1 != raw2 + dst2) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding at "
+ dateToString(startTime));
}
TimeZoneTransition trans;
UBool avail = tz->getNextTransition(startTime, false, trans);
if (avail) {
if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
endTime, true, status)) {
int32_t maxDelta = 1000;
if (!hasEquivalentTransitions(*vtz_new, *tz, trans.getTime() + maxDelta,
endTime, true, maxDelta, status)) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding.");
} else {
logln("VTimeZone for " + *tzid +
" differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
}
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
}
}
}
if (vtz_new != nullptr) {
delete vtz_new;
vtz_new = nullptr;
}
}
delete tz;
delete vtz_org;
}
}
/*
* Write out time zone rules of OlsonTimeZone after a cutover date into VTIMEZONE format,
* create a new VTimeZone from the VTIMEZONE data, then compare transitions
*/
void
TimeZoneRuleTest::TestVTimeZoneRoundTripPartial() {
const int32_t STARTYEARS[] = {
1900,
1950,
2020,
0
};
UDate endTime = getUTCMillis(2050, UCAL_JANUARY, 1);
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
for (int32_t i = 0; STARTYEARS[i] != 0; i++) {
// Write out VTIMEZONE
UDate startTime = getUTCMillis(STARTYEARS[i], UCAL_JANUARY, 1);
vtz_org->write(startTime, vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing time zone rules for ") +
*tzid + " into VTIMEZONE format since " + dateToString(startTime));
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while reading VTIMEZONE data for ") + *tzid
+ " since " + dateToString(startTime));
} else {
// Check equivalency after the first transition.
// The DST information before the first transition might be lost
// because there is no good way to represent the initial time with
// VTIMEZONE.
int32_t raw1, raw2, dst1, dst2;
tz->getOffset(startTime, false, raw1, dst1, status);
vtz_new->getOffset(startTime, false, raw2, dst2, status);
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from getOffset");
} else {
if (raw1 + dst1 != raw2 + dst2) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding at "
+ dateToString(startTime));
}
TimeZoneTransition trans;
UBool avail = tz->getNextTransition(startTime, false, trans);
if (avail) {
if (!vtz_new->hasEquivalentTransitions(*tz, trans.getTime(),
endTime, true, status)) {
int32_t maxDelta = 1000;
if (!hasEquivalentTransitions(*vtz_new, *tz, trans.getTime() + maxDelta,
endTime, true, maxDelta, status)) {
errln("FAIL: VTimeZone for " + *tzid +
" is not equivalent to its OlsonTimeZone corresponding.");
} else {
logln("VTimeZone for " + *tzid +
" differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
}
}
if (U_FAILURE(status)) {
errln("FAIL: error status is returned from hasEquivalentTransition");
}
}
}
}
}
if (vtz_new != nullptr) {
delete vtz_new;
vtz_new = nullptr;
}
}
delete tz;
delete vtz_org;
}
}
/*
* Write out simple time zone rules from an OlsonTimeZone at various time into VTIMEZONE
* format and create a new VTimeZone from the VTIMEZONE data, then make sure the raw offset
* and DST savings are same in these two time zones.
*/
void
TimeZoneRuleTest::TestVTimeZoneSimpleWrite() {
const int32_t TESTDATES[][3] = {
{2006, UCAL_JANUARY, 1},
{2006, UCAL_MARCH, 15},
{2006, UCAL_MARCH, 31},
{2006, UCAL_OCTOBER, 25},
{2006, UCAL_NOVEMBER, 1},
{2006, UCAL_NOVEMBER, 5},
{2007, UCAL_JANUARY, 1},
{0, 0, 0}
};
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
VTimeZone *vtz_org = VTimeZone::createVTimeZoneByID(*tzid);
VTimeZone *vtz_new = nullptr;
UnicodeString vtzdata;
for (int32_t i = 0; TESTDATES[i][0] != 0; i++) {
// Write out VTIMEZONE
UDate time = getUTCMillis(TESTDATES[i][0], TESTDATES[i][1], TESTDATES[i][2]);
vtz_org->writeSimple(time, vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while writing simple time zone rules for ") +
*tzid + " into VTIMEZONE format at " + dateToString(time));
} else {
// Read VTIMEZONE data
vtz_new = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: error returned while reading simple VTIMEZONE data for ") + *tzid
+ " at " + dateToString(time));
} else {
// Check equivalency
int32_t raw0, dst0;
int32_t raw1, dst1;
vtz_org->getOffset(time, false, raw0, dst0, status);
vtz_new->getOffset(time, false, raw1, dst1, status);
if (U_SUCCESS(status)) {
if (raw0 != raw1 || dst0 != dst1) {
errln("FAIL: VTimeZone writeSimple for " + *tzid + " at "
+ dateToString(time) + " failed to the round trip.");
}
} else {
errln("FAIL: getOffset returns error status");
}
}
}
if (vtz_new != nullptr) {
delete vtz_new;
vtz_new = nullptr;
}
}
delete vtz_org;
}
}
/*
* Write out time zone rules of OlsonTimeZone into VTIMEZONE format with RFC2445 header TZURL and
* LAST-MODIFIED, create a new VTimeZone from the VTIMEZONE data to see if the headers are preserved.
*/
void
TimeZoneRuleTest::TestVTimeZoneHeaderProps() {
const UnicodeString TESTURL1("http://source.icu-project.org");
const UnicodeString TESTURL2("http://www.ibm.com");
UErrorCode status = U_ZERO_ERROR;
UnicodeString tzurl;
UDate lmod;
UDate lastmod = getUTCMillis(2007, UCAL_JUNE, 1);
VTimeZone *vtz = VTimeZone::createVTimeZoneByID("America/Chicago");
vtz->setTZURL(TESTURL1);
vtz->setLastModified(lastmod);
// Roundtrip conversion
UnicodeString vtzdata;
vtz->write(vtzdata, status);
VTimeZone *newvtz1 = nullptr;
if (U_FAILURE(status)) {
errln("FAIL: error returned while writing VTIMEZONE data 1");
return;
}
// Create a new one
newvtz1 = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln("FAIL: error returned while loading VTIMEZONE data 1");
} else {
// Check if TZURL and LAST-MODIFIED properties are preserved
newvtz1->getTZURL(tzurl);
if (tzurl != TESTURL1) {
errln("FAIL: TZURL 1 was not preserved");
}
vtz->getLastModified(lmod);
if (lastmod != lmod) {
errln("FAIL: LAST-MODIFIED was not preserved");
}
}
if (U_SUCCESS(status)) {
// Set different tzurl
newvtz1->setTZURL(TESTURL2);
// Second roundtrip, with a cutover
newvtz1->write(vtzdata, status);
if (U_FAILURE(status)) {
errln("FAIL: error returned while writing VTIMEZONE data 2");
} else {
VTimeZone *newvtz2 = VTimeZone::createVTimeZone(vtzdata, status);
if (U_FAILURE(status)) {
errln("FAIL: error returned while loading VTIMEZONE data 2");
} else {
// Check if TZURL and LAST-MODIFIED properties are preserved
newvtz2->getTZURL(tzurl);
if (tzurl != TESTURL2) {
errln("FAIL: TZURL was not preserved in the second roundtrip");
}
vtz->getLastModified(lmod);
if (lastmod != lmod) {
errln("FAIL: LAST-MODIFIED was not preserved in the second roundtrip");
}
}
delete newvtz2;
}
}
delete newvtz1;
delete vtz;
}
/*
* Extract simple rules from an OlsonTimeZone and make sure the rule format matches
* the expected format.
*/
void
TimeZoneRuleTest::TestGetSimpleRules() {
UDate testTimes[] = {
getUTCMillis(1970, UCAL_JANUARY, 1),
getUTCMillis(2000, UCAL_MARCH, 31),
getUTCMillis(2005, UCAL_JULY, 1),
getUTCMillis(2010, UCAL_NOVEMBER, 1),
};
int32_t numTimes = UPRV_LENGTHOF(testTimes);
UErrorCode status = U_ZERO_ERROR;
TestZIDEnumeration tzenum(!quick);
InitialTimeZoneRule *initial;
AnnualTimeZoneRule *std, *dst;
for (int32_t i = 0; i < numTimes ; i++) {
while (true) {
const UnicodeString *tzid = tzenum.snext(status);
if (tzid == nullptr) {
break;
}
if (U_FAILURE(status)) {
errln("FAIL: error returned while enumerating timezone IDs.");
break;
}
BasicTimeZone *tz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(*tzid));
initial = nullptr;
std = dst = nullptr;
tz->getSimpleRulesNear(testTimes[i], initial, std, dst, status);
if (U_FAILURE(status)) {
errln("FAIL: getSimpleRules failed.");
break;
}
if (initial == nullptr) {
errln("FAIL: initial rule must not be nullptr");
break;
} else if (!((std == nullptr && dst == nullptr) || (std != nullptr && dst != nullptr))) {
errln("FAIL: invalid std/dst pair.");
break;
}
if (std != nullptr) {
const DateTimeRule *dtr = std->getRule();
if (dtr->getDateRuleType() != DateTimeRule::DOW) {
errln("FAIL: simple std rull must use DateTimeRule::DOW as date rule.");
break;
}
if (dtr->getTimeRuleType() != DateTimeRule::WALL_TIME) {
errln("FAIL: simple std rull must use DateTimeRule::WALL_TIME as time rule.");
break;
}
dtr = dst->getRule();
if (dtr->getDateRuleType() != DateTimeRule::DOW) {
errln("FAIL: simple dst rull must use DateTimeRule::DOW as date rule.");
break;
}
if (dtr->getTimeRuleType() != DateTimeRule::WALL_TIME) {
errln("FAIL: simple dst rull must use DateTimeRule::WALL_TIME as time rule.");
break;
}
}
// Create an RBTZ from the rules and compare the offsets at the date
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(*tzid, initial);
if (std != nullptr) {
rbtz->addTransitionRule(std, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add std rule.");
}
rbtz->addTransitionRule(dst, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't add dst rule.");
}
}
rbtz->complete(status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't complete rbtz for " + *tzid);
}
int32_t raw0, dst0, raw1, dst1;
tz->getOffset(testTimes[i], false, raw0, dst0, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't get offsets from tz for " + *tzid);
}
rbtz->getOffset(testTimes[i], false, raw1, dst1, status);
if (U_FAILURE(status)) {
errln("FAIL: couldn't get offsets from rbtz for " + *tzid);
}
if (raw0 != raw1 || dst0 != dst1) {
errln("FAIL: rbtz created by simple rule does not match the original tz for tzid " + *tzid);
}
delete rbtz;
delete tz;
}
}
}
/*
* API coverage tests for TimeZoneRule
*/
void
TimeZoneRuleTest::TestTimeZoneRuleCoverage() {
UDate time1 = getUTCMillis(2005, UCAL_JULY, 4);
UDate time2 = getUTCMillis(2015, UCAL_JULY, 4);
UDate time3 = getUTCMillis(1950, UCAL_JULY, 4);
DateTimeRule *dtr1 = new DateTimeRule(UCAL_FEBRUARY, 29, UCAL_SUNDAY, false,
3*HOUR, DateTimeRule::WALL_TIME); // Last Sunday on or before Feb 29, at 3 AM, wall time
DateTimeRule *dtr2 = new DateTimeRule(UCAL_MARCH, 11, 2*HOUR,
DateTimeRule::STANDARD_TIME); // Mar 11, at 2 AM, standard time
DateTimeRule *dtr3 = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SATURDAY,
6*HOUR, DateTimeRule::UTC_TIME); //Last Saturday in Oct, at 6 AM, UTC
DateTimeRule *dtr4 = new DateTimeRule(UCAL_MARCH, 8, UCAL_SUNDAY, true,
2*HOUR, DateTimeRule::WALL_TIME); // First Sunday on or after Mar 8, at 2 AM, wall time
AnnualTimeZoneRule *a1 = new AnnualTimeZoneRule("a1", -3*HOUR, 1*HOUR, *dtr1,
2000, AnnualTimeZoneRule::MAX_YEAR);
AnnualTimeZoneRule *a2 = new AnnualTimeZoneRule("a2", -3*HOUR, 1*HOUR, *dtr1,
2000, AnnualTimeZoneRule::MAX_YEAR);
AnnualTimeZoneRule *a3 = new AnnualTimeZoneRule("a3", -3*HOUR, 1*HOUR, *dtr1,
2000, 2010);
InitialTimeZoneRule *i1 = new InitialTimeZoneRule("i1", -3*HOUR, 0);
InitialTimeZoneRule *i2 = new InitialTimeZoneRule("i2", -3*HOUR, 0);
InitialTimeZoneRule *i3 = new InitialTimeZoneRule("i3", -3*HOUR, 1*HOUR);
UDate trtimes1[] = {0.0};
UDate trtimes2[] = {0.0, 10000000.0};
TimeArrayTimeZoneRule *t1 = new TimeArrayTimeZoneRule("t1", -3*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
TimeArrayTimeZoneRule *t2 = new TimeArrayTimeZoneRule("t2", -3*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
TimeArrayTimeZoneRule *t3 = new TimeArrayTimeZoneRule("t3", -3*HOUR, 0, trtimes2, 2, DateTimeRule::UTC_TIME);
TimeArrayTimeZoneRule *t4 = new TimeArrayTimeZoneRule("t4", -3*HOUR, 0, trtimes1, 1, DateTimeRule::STANDARD_TIME);
TimeArrayTimeZoneRule *t5 = new TimeArrayTimeZoneRule("t5", -4*HOUR, 1*HOUR, trtimes1, 1, DateTimeRule::WALL_TIME);
// DateTimeRule::operator=/clone
DateTimeRule dtr0(UCAL_MAY, 31, 2*HOUR, DateTimeRule::WALL_TIME);
if (dtr0 == *dtr1 || !(dtr0 != *dtr1)) {
errln("FAIL: DateTimeRule dtr0 is not equal to dtr1, but got wrong result");
}
dtr0 = *dtr1;
if (dtr0 != *dtr1 || !(dtr0 == *dtr1)) {
errln("FAIL: DateTimeRule dtr0 is equal to dtr1, but got wrong result");
}
DateTimeRule *dtr0c = dtr0.clone();
if (*dtr0c != *dtr1 || !(*dtr0c == *dtr1)) {
errln("FAIL: DateTimeRule dtr0c is equal to dtr1, but got wrong result");
}
delete dtr0c;
// AnnualTimeZonerule::operator=/clone
AnnualTimeZoneRule a0("a0", 5*HOUR, 1*HOUR, *dtr1, 1990, AnnualTimeZoneRule::MAX_YEAR);
if (a0 == *a1 || !(a0 != *a1)) {
errln("FAIL: AnnualTimeZoneRule a0 is not equal to a1, but got wrong result");
}
a0 = *a1;
if (a0 != *a1 || !(a0 == *a1)) {
errln("FAIL: AnnualTimeZoneRule a0 is equal to a1, but got wrong result");
}
AnnualTimeZoneRule *a0c = a0.clone();
if (*a0c != *a1 || !(*a0c == *a1)) {
errln("FAIL: AnnualTimeZoneRule a0c is equal to a1, but got wrong result");
}
delete a0c;
// AnnualTimeZoneRule::getRule
if (*(a1->getRule()) != *(a2->getRule())) {
errln("FAIL: The same DateTimeRule must be returned from AnnualTimeZoneRule a1 and a2");
}
// AnnualTimeZoneRule::getStartYear
int32_t startYear = a1->getStartYear();
if (startYear != 2000) {
errln(UnicodeString("FAIL: The start year of AnnualTimeZoneRule a1 must be 2000 - returned: ") + startYear);
}
// AnnualTimeZoneRule::getEndYear
int32_t endYear = a1->getEndYear();
if (endYear != AnnualTimeZoneRule::MAX_YEAR) {
errln(UnicodeString("FAIL: The start year of AnnualTimeZoneRule a1 must be MAX_YEAR - returned: ") + endYear);
}
endYear = a3->getEndYear();
if (endYear != 2010) {
errln(UnicodeString("FAIL: The start year of AnnualTimeZoneRule a3 must be 2010 - returned: ") + endYear);
}
// AnnualTimeZone::getStartInYear
UBool b1, b2;
UDate d1, d2;
b1 = a1->getStartInYear(2005, -3*HOUR, 0, d1);
b2 = a3->getStartInYear(2005, -3*HOUR, 0, d2);
if (!b1 || !b2 || d1 != d2) {
errln("FAIL: AnnualTimeZoneRule::getStartInYear did not work as expected");
}
b2 = a3->getStartInYear(2015, -3*HOUR, 0, d2);
if (b2) {
errln("FAIL: AnnualTimeZoneRule::getStartInYear returned true for 2015 which is out of rule range");
}
// AnnualTimeZone::getFirstStart
b1 = a1->getFirstStart(-3*HOUR, 0, d1);
b2 = a1->getFirstStart(-4*HOUR, 1*HOUR, d2);
if (!b1 || !b2 || d1 != d2) {
errln("FAIL: The same start time should be returned by getFirstStart");
}
// AnnualTimeZone::getFinalStart
b1 = a1->getFinalStart(-3*HOUR, 0, d1);
if (b1) {
errln("FAIL: getFinalStart returned true for a1");
}
b1 = a1->getStartInYear(2010, -3*HOUR, 0, d1);
b2 = a3->getFinalStart(-3*HOUR, 0, d2);
if (!b1 || !b2 || d1 != d2) {
errln("FAIL: Bad date is returned by getFinalStart");
}
// AnnualTimeZone::getNextStart / getPreviousStart
b1 = a1->getNextStart(time1, -3*HOUR, 0, false, d1);
if (!b1) {
errln("FAIL: getNextStart returned false for ai");
} else {
b2 = a1->getPreviousStart(d1, -3*HOUR, 0, true, d2);
if (!b2 || d1 != d2) {
errln("FAIL: Bad Date is returned by getPreviousStart");
}
}
b1 = a3->getNextStart(time2, -3*HOUR, 0, false, d1);
if (b1) {
dataerrln("FAIL: getNextStart must return false when no start time is available after the base time");
}
b1 = a3->getFinalStart(-3*HOUR, 0, d1);
b2 = a3->getPreviousStart(time2, -3*HOUR, 0, false, d2);
if (!b1 || !b2 || d1 != d2) {
dataerrln("FAIL: getPreviousStart does not match with getFinalStart after the end year");
}
// AnnualTimeZone::isEquavalentTo
if (!a1->isEquivalentTo(*a2)) {
errln("FAIL: AnnualTimeZoneRule a1 is equivalent to a2, but returned false");
}
if (a1->isEquivalentTo(*a3)) {
errln("FAIL: AnnualTimeZoneRule a1 is not equivalent to a3, but returned true");
}
if (!a1->isEquivalentTo(*a1)) {
errln("FAIL: AnnualTimeZoneRule a1 is equivalent to itself, but returned false");
}
if (a1->isEquivalentTo(*t1)) {
errln("FAIL: AnnualTimeZoneRule is not equivalent to TimeArrayTimeZoneRule, but returned true");
}
// InitialTimezoneRule::operator=/clone
InitialTimeZoneRule i0("i0", 10*HOUR, 0);
if (i0 == *i1 || !(i0 != *i1)) {
errln("FAIL: InitialTimeZoneRule i0 is not equal to i1, but got wrong result");
}
i0 = *i1;
if (i0 != *i1 || !(i0 == *i1)) {
errln("FAIL: InitialTimeZoneRule i0 is equal to i1, but got wrong result");
}
InitialTimeZoneRule *i0c = i0.clone();
if (*i0c != *i1 || !(*i0c == *i1)) {
errln("FAIL: InitialTimeZoneRule i0c is equal to i1, but got wrong result");
}
delete i0c;
// InitialTimeZoneRule::isEquivalentRule
if (!i1->isEquivalentTo(*i2)) {
errln("FAIL: InitialTimeZoneRule i1 is equivalent to i2, but returned false");
}
if (i1->isEquivalentTo(*i3)) {
errln("FAIL: InitialTimeZoneRule i1 is not equivalent to i3, but returned true");
}
if (i1->isEquivalentTo(*a1)) {
errln("FAIL: An InitialTimeZoneRule is not equivalent to an AnnualTimeZoneRule, but returned true");
}
// InitialTimeZoneRule::getFirstStart/getFinalStart/getNextStart/getPreviousStart
b1 = i1->getFirstStart(0, 0, d1);
if (b1) {
errln("FAIL: InitialTimeZone::getFirstStart returned true");
}
b1 = i1->getFinalStart(0, 0, d1);
if (b1) {
errln("FAIL: InitialTimeZone::getFinalStart returned true");
}
b1 = i1->getNextStart(time1, 0, 0, false, d1);
if (b1) {
errln("FAIL: InitialTimeZone::getNextStart returned true");
}
b1 = i1->getPreviousStart(time1, 0, 0, false, d1);
if (b1) {
errln("FAIL: InitialTimeZone::getPreviousStart returned true");
}
// TimeArrayTimeZoneRule::operator=/clone
TimeArrayTimeZoneRule t0("t0", 4*HOUR, 0, trtimes1, 1, DateTimeRule::UTC_TIME);
if (t0 == *t1 || !(t0 != *t1)) {
errln("FAIL: TimeArrayTimeZoneRule t0 is not equal to t1, but got wrong result");
}
t0 = *t1;
if (t0 != *t1 || !(t0 == *t1)) {
errln("FAIL: TimeArrayTimeZoneRule t0 is equal to t1, but got wrong result");
}
TimeArrayTimeZoneRule *t0c = t0.clone();
if (*t0c != *t1 || !(*t0c == *t1)) {
errln("FAIL: TimeArrayTimeZoneRule t0c is equal to t1, but got wrong result");
}
delete t0c;
// TimeArrayTimeZoneRule::countStartTimes
if (t1->countStartTimes() != 1) {
errln("FAIL: Bad start time count is returned by TimeArrayTimeZoneRule::countStartTimes");
}
// TimeArrayTimeZoneRule::getStartTimeAt
b1 = t1->getStartTimeAt(-1, d1);
if (b1) {
errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned true for index -1");
}
b1 = t1->getStartTimeAt(0, d1);
if (!b1 || d1 != trtimes1[0]) {
errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned incorrect result for index 0");
}
b1 = t1->getStartTimeAt(1, d1);
if (b1) {
errln("FAIL: TimeArrayTimeZoneRule::getStartTimeAt returned true for index 1");
}
// TimeArrayTimeZoneRule::getTimeType
if (t1->getTimeType() != DateTimeRule::UTC_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t1 uses UTC_TIME, but different type is returned");
}
if (t4->getTimeType() != DateTimeRule::STANDARD_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t4 uses STANDARD_TIME, but different type is returned");
}
if (t5->getTimeType() != DateTimeRule::WALL_TIME) {
errln("FAIL: TimeArrayTimeZoneRule t5 uses WALL_TIME, but different type is returned");
}
// TimeArrayTimeZoneRule::getFirstStart/getFinalStart
b1 = t1->getFirstStart(0, 0, d1);
if (!b1 || d1 != trtimes1[0]) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t1");
}
b1 = t1->getFinalStart(0, 0, d1);
if (!b1 || d1 != trtimes1[0]) {
errln("FAIL: Bad final start time returned from TimeArrayTimeZoneRule t1");
}
b1 = t4->getFirstStart(-4*HOUR, 1*HOUR, d1);
if (!b1 || d1 != (trtimes1[0] + 4*HOUR)) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t4");
}
b1 = t5->getFirstStart(-4*HOUR, 1*HOUR, d1);
if (!b1 || d1 != (trtimes1[0] + 3*HOUR)) {
errln("FAIL: Bad first start time returned from TimeArrayTimeZoneRule t5");
}
// TimeArrayTimeZoneRule::getNextStart/getPreviousStart
b1 = t3->getNextStart(time1, -3*HOUR, 1*HOUR, false, d1);
if (b1) {
dataerrln("FAIL: getNextStart returned true after the final transition for t3");
}
b1 = t3->getPreviousStart(time1, -3*HOUR, 1*HOUR, false, d1);
if (!b1 || d1 != trtimes2[1]) {
dataerrln("FAIL: Bad start time returned by getPreviousStart for t3");
} else {
b2 = t3->getPreviousStart(d1, -3*HOUR, 1*HOUR, false, d2);
if (!b2 || d2 != trtimes2[0]) {
errln("FAIL: Bad start time returned by getPreviousStart for t3");
}
}
b1 = t3->getPreviousStart(time3, -3*HOUR, 1*HOUR, false, d1); //time3 - year 1950, no result expected
if (b1) {
errln("FAIL: getPreviousStart returned true before the first transition for t3");
}
// TimeArrayTimeZoneRule::isEquivalentTo
if (!t1->isEquivalentTo(*t2)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is equivalent to t2, but returned false");
}
if (t1->isEquivalentTo(*t3)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t3, but returned true");
}
if (t1->isEquivalentTo(*t4)) {
errln("FAIL: TimeArrayTimeZoneRule t1 is not equivalent to t4, but returned true");
}
if (t1->isEquivalentTo(*a1)) {
errln("FAIL: TimeArrayTimeZoneRule is not equivalent to AnnualTimeZoneRule, but returned true");
}
delete dtr1;
delete dtr2;
delete dtr3;
delete dtr4;
delete a1;
delete a2;
delete a3;
delete i1;
delete i2;
delete i3;
delete t1;
delete t2;
delete t3;
delete t4;
delete t5;
}
/*
* API coverage test for BasicTimeZone APIs in SimpleTimeZone
*/
void
TimeZoneRuleTest::TestSimpleTimeZoneCoverage() {
UDate time1 = getUTCMillis(1990, UCAL_JUNE, 1);
UDate time2 = getUTCMillis(2000, UCAL_JUNE, 1);
TimeZoneTransition tzt1, tzt2;
UBool avail1, avail2;
UErrorCode status = U_ZERO_ERROR;
const TimeZoneRule *trrules[2];
const InitialTimeZoneRule *ir = nullptr;
int32_t numTzRules;
// BasicTimeZone API implementation in SimpleTimeZone
SimpleTimeZone *stz1 = new SimpleTimeZone(-5*HOUR, "GMT-5");
avail1 = stz1->getNextTransition(time1, false, tzt1);
if (avail1) {
errln("FAIL: No transition must be returned by getNextTransition for SimpleTimeZone with no DST rule");
}
avail1 = stz1->getPreviousTransition(time1, false, tzt1);
if (avail1) {
errln("FAIL: No transition must be returned by getPreviousTransition for SimpleTimeZone with no DST rule");
}
numTzRules = stz1->countTransitionRules(status);
if (U_FAILURE(status)) {
errln("FAIL: countTransitionRules failed");
}
if (numTzRules != 0) {
errln(UnicodeString("FAIL: countTransitionRules returned ") + numTzRules);
}
numTzRules = 2;
stz1->getTimeZoneRules(ir, trrules, numTzRules, status);
if (U_FAILURE(status)) {
errln("FAIL: getTimeZoneRules failed");
}
if (numTzRules != 0) {
errln("FAIL: Incorrect transition rule count");
}
if (ir == nullptr || ir->getRawOffset() != stz1->getRawOffset()) {
errln("FAIL: Bad initial time zone rule");
}
// Set DST rule
stz1->setStartRule(UCAL_MARCH, 11, 2*HOUR, status); // March 11
stz1->setEndRule(UCAL_NOVEMBER, 1, UCAL_SUNDAY, 2*HOUR, status); // First Sunday in November
if (U_FAILURE(status)) {
errln("FAIL: Failed to set DST rules in a SimpleTimeZone");
}
avail1 = stz1->getNextTransition(time1, false, tzt1);
if (!avail1) {
errln("FAIL: Non-null transition must be returned by getNextTransition for SimpleTimeZone with a DST rule");
}
avail1 = stz1->getPreviousTransition(time1, false, tzt1);
if (!avail1) {
errln("FAIL: Non-null transition must be returned by getPreviousTransition for SimpleTimeZone with a DST rule");
}
numTzRules = stz1->countTransitionRules(status);
if (U_FAILURE(status)) {
errln("FAIL: countTransitionRules failed");
}
if (numTzRules != 2) {
errln(UnicodeString("FAIL: countTransitionRules returned ") + numTzRules);
}
numTzRules = 2;
trrules[0] = nullptr;
trrules[1] = nullptr;
stz1->getTimeZoneRules(ir, trrules, numTzRules, status);
if (U_FAILURE(status)) {
errln("FAIL: getTimeZoneRules failed");
}
if (numTzRules != 2) {
errln("FAIL: Incorrect transition rule count");
}
if (ir == nullptr || ir->getRawOffset() != stz1->getRawOffset()) {
errln("FAIL: Bad initial time zone rule");
}
if (trrules[0] == nullptr || trrules[0]->getRawOffset() != stz1->getRawOffset()) {
errln("FAIL: Bad transition rule 0");
}
if (trrules[1] == nullptr || trrules[1]->getRawOffset() != stz1->getRawOffset()) {
errln("FAIL: Bad transition rule 1");
}
// Set DST start year
stz1->setStartYear(2007);
avail1 = stz1->getPreviousTransition(time1, false, tzt1);
if (avail1) {
errln("FAIL: No transition must be returned before 1990");
}
avail1 = stz1->getNextTransition(time1, false, tzt1); // transition after 1990-06-01
avail2 = stz1->getNextTransition(time2, false, tzt2); // transition after 2000-06-01
if (!avail1 || !avail2 || tzt1 != tzt2) {
errln("FAIL: Bad transition returned by SimpleTimeZone::getNextTransition");
}
delete stz1;
}
/*
* API coverage test for VTimeZone
*/
void
TimeZoneRuleTest::TestVTimeZoneCoverage() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString TZID("Europe/Moscow");
BasicTimeZone *otz = dynamic_cast<BasicTimeZone*>(TimeZone::createTimeZone(TZID));
VTimeZone *vtz = VTimeZone::createVTimeZoneByID(TZID);
// getOffset(era, year, month, day, dayOfWeek, milliseconds, ec)
int32_t offset1 = otz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed for otz");
}
int32_t offset2 = vtz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(7 args) failed for vtz");
}
if (offset1 != offset2) {
errln("FAIL: getOffset(7 args) returned different results in VTimeZone and OlsonTimeZone");
}
// getOffset(era, year, month, day, dayOfWeek, milliseconds, monthLength, ec)
offset1 = otz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, 31, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(8 args) failed for otz");
}
offset2 = vtz->getOffset(GregorianCalendar::AD, 2007, UCAL_JULY, 1, UCAL_SUNDAY, 0, 31, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(8 args) failed for vtz");
}
if (offset1 != offset2) {
errln("FAIL: getOffset(8 args) returned different results in VTimeZone and OlsonTimeZone");
}
// getOffset(date, local, rawOffset, dstOffset, ec)
UDate t = Calendar::getNow();
int32_t rawOffset1, dstSavings1;
int32_t rawOffset2, dstSavings2;
otz->getOffset(t, false, rawOffset1, dstSavings1, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed for otz");
}
vtz->getOffset(t, false, rawOffset2, dstSavings2, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset(5 args) failed for vtz");
}
if (rawOffset1 != rawOffset2 || dstSavings1 != dstSavings2) {
errln("FAIL: getOffset(long,boolean,int[]) returned different results in VTimeZone and OlsonTimeZone");
}
// getRawOffset
if (otz->getRawOffset() != vtz->getRawOffset()) {
errln("FAIL: getRawOffset returned different results in VTimeZone and OlsonTimeZone");
}
// inDaylightTime
UBool inDst1, inDst2;
inDst1 = otz->inDaylightTime(t, status);
if (U_FAILURE(status)) {
dataerrln("FAIL: inDaylightTime failed for otz: %s", u_errorName(status));
}
inDst2 = vtz->inDaylightTime(t, status);
if (U_FAILURE(status)) {
dataerrln("FAIL: inDaylightTime failed for vtz: %s", u_errorName(status));
}
if (inDst1 != inDst2) {
errln("FAIL: inDaylightTime returned different results in VTimeZone and OlsonTimeZone");
}
// useDaylightTime
if (otz->useDaylightTime() != vtz->useDaylightTime()) {
errln("FAIL: useDaylightTime returned different results in VTimeZone and OlsonTimeZone");
}
// setRawOffset
const int32_t RAW = -10*HOUR;
VTimeZone *tmpvtz = vtz->clone();
tmpvtz->setRawOffset(RAW);
if (tmpvtz->getRawOffset() != RAW) {
logln("setRawOffset is implemented in VTimeZone");
}
// hasSameRules
UBool bSame = otz->hasSameRules(*vtz);
logln(UnicodeString("OlsonTimeZone::hasSameRules(VTimeZone) should return false always for now - actual: ") + bSame);
// getTZURL/setTZURL
UnicodeString TZURL("http://icu-project.org/timezone");
UnicodeString url;
if (vtz->getTZURL(url)) {
errln("FAIL: getTZURL returned true");
}
vtz->setTZURL(TZURL);
if (!vtz->getTZURL(url) || url != TZURL) {
errln("FAIL: URL returned by getTZURL does not match the one set by setTZURL");
}
// getLastModified/setLastModified
UDate lastmod;
if (vtz->getLastModified(lastmod)) {
errln("FAIL: getLastModified returned true");
}
vtz->setLastModified(t);
if (!vtz->getLastModified(lastmod) || lastmod != t) {
errln("FAIL: Date returned by getLastModified does not match the one set by setLastModified");
}
// getNextTransition/getPreviousTransition
UDate base = getUTCMillis(2007, UCAL_JULY, 1);
TimeZoneTransition tzt1, tzt2;
UBool btr1 = otz->getNextTransition(base, true, tzt1);
UBool btr2 = vtz->getNextTransition(base, true, tzt2);
if (!btr1 || !btr2 || tzt1 != tzt2) {
dataerrln("FAIL: getNextTransition returned different results in VTimeZone and OlsonTimeZone");
}
btr1 = otz->getPreviousTransition(base, false, tzt1);
btr2 = vtz->getPreviousTransition(base, false, tzt2);
if (!btr1 || !btr2 || tzt1 != tzt2) {
dataerrln("FAIL: getPreviousTransition returned different results in VTimeZone and OlsonTimeZone");
}
// TimeZoneTransition constructor/clone
TimeZoneTransition *tzt1c = tzt1.clone();
if (*tzt1c != tzt1 || !(*tzt1c == tzt1)) {
errln("FAIL: TimeZoneTransition tzt1c is equal to tzt1, but got wrong result");
}
delete tzt1c;
TimeZoneTransition tzt3(tzt1);
if (tzt3 != tzt1 || !(tzt3 == tzt1)) {
errln("FAIL: TimeZoneTransition tzt3 is equal to tzt1, but got wrong result");
}
// hasEquivalentTransitions
UDate time1 = getUTCMillis(1950, UCAL_JANUARY, 1);
UDate time2 = getUTCMillis(2020, UCAL_JANUARY, 1);
UBool equiv = vtz->hasEquivalentTransitions(*otz, time1, time2, false, status);
if (U_FAILURE(status)) {
dataerrln("FAIL: hasEquivalentTransitions failed for vtz/otz: %s", u_errorName(status));
}
if (!equiv) {
dataerrln("FAIL: hasEquivalentTransitions returned false for the same time zone");
}
// operator=/operator==/operator!=
VTimeZone *vtz1 = VTimeZone::createVTimeZoneByID("America/Los_Angeles");
if (*vtz1 == *vtz || !(*vtz1 != *vtz)) {
errln("FAIL: VTimeZone vtz1 is not equal to vtz, but got wrong result");
}
*vtz1 = *vtz;
if (*vtz1 != *vtz || !(*vtz1 == *vtz)) {
errln("FAIL: VTimeZone vtz1 is equal to vtz, but got wrong result");
}
// Creation from BasicTimeZone
//
status = U_ZERO_ERROR;
VTimeZone *vtzFromBasic = nullptr;
SimpleTimeZone *simpleTZ = new SimpleTimeZone(28800000, "Asia/Singapore");
simpleTZ->setStartYear(1970);
simpleTZ->setStartRule(0, // month
1, // day of week
0, // time
status);
simpleTZ->setEndRule(1, 1, 0, status);
if (U_FAILURE(status)) {
errln("File %s, line %d, failed with status = %s", __FILE__, __LINE__, u_errorName(status));
goto end_basic_tz_test;
}
vtzFromBasic = VTimeZone::createVTimeZoneFromBasicTimeZone(*simpleTZ, status);
if (U_FAILURE(status) || vtzFromBasic == nullptr) {
dataerrln("File %s, line %d, failed with status = %s", __FILE__, __LINE__, u_errorName(status));
goto end_basic_tz_test;
}
// delete the source time zone, to make sure there are no dependencies on it.
delete simpleTZ;
// Create another simple time zone w the same rules, and check that it is the
// same as the test VTimeZone created above.
{
SimpleTimeZone simpleTZ2(28800000, "Asia/Singapore");
simpleTZ2.setStartYear(1970);
simpleTZ2.setStartRule(0, // month
1, // day of week
0, // time
status);
simpleTZ2.setEndRule(1, 1, 0, status);
if (U_FAILURE(status)) {
errln("File %s, line %d, failed with status = %s", __FILE__, __LINE__, u_errorName(status));
goto end_basic_tz_test;
}
if (vtzFromBasic->hasSameRules(simpleTZ2) == false) {
errln("File %s, line %d, failed hasSameRules() ", __FILE__, __LINE__);
goto end_basic_tz_test;
}
}
end_basic_tz_test:
delete vtzFromBasic;
delete otz;
delete vtz;
delete tmpvtz;
delete vtz1;
}
void
TimeZoneRuleTest::TestVTimeZoneParse() {
UErrorCode status = U_ZERO_ERROR;
// Trying to create VTimeZone from empty data
UnicodeString emptyData;
VTimeZone *empty = VTimeZone::createVTimeZone(emptyData, status);
if (U_SUCCESS(status) || empty != nullptr) {
delete empty;
errln("FAIL: Non-null VTimeZone is returned for empty VTIMEZONE data");
}
status = U_ZERO_ERROR;
// Create VTimeZone for Asia/Tokyo
UnicodeString asiaTokyoID("Asia/Tokyo");
static const char16_t asiaTokyo[] = {
/* "BEGIN:VTIMEZONE\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
/* "TZID:Asia\x0D\x0A" */
0x54,0x5A,0x49,0x44,0x3A,0x41,0x73,0x69,0x61,0x0D,0x0A,
/* "\x09/Tokyo\x0D\x0A" */
0x09,0x2F,0x54,0x6F,0x6B,0x79,0x6F,0x0D,0x0A,
/* "BEGIN:STANDARD\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
/* "TZOFFSETFROM:+0900\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2B,0x30,0x39,0x30,0x30,0x0D,0x0A,
/* "TZOFFSETTO:+0900\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2B,0x30,0x39,0x30,0x30,0x0D,0x0A,
/* "TZNAME:JST\x0D\x0A" */
0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x4A,0x53,0x54,0x0D,0x0A,
/* "DTSTART:19700101\x0D\x0A" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x31,0x39,0x37,0x30,0x30,0x31,0x30,0x31,0x0D,0x0A,
/* " T000000\x0D\x0A" */
0x20,0x54,0x30,0x30,0x30,0x30,0x30,0x30,0x0D,0x0A,
/* "END:STANDARD\x0D\x0A" */
0x45,0x4E,0x44,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
/* "END:VTIMEZONE" */
0x45,0x4E,0x44,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,
0
};
VTimeZone *tokyo = VTimeZone::createVTimeZone(asiaTokyo, status);
if (U_FAILURE(status) || tokyo == nullptr) {
errln("FAIL: Failed to create a VTimeZone tokyo");
} else {
// Check ID
UnicodeString tzid;
tokyo->getID(tzid);
if (tzid != asiaTokyoID) {
errln(UnicodeString("FAIL: Invalid TZID: ") + tzid);
}
// Make sure offsets are correct
int32_t rawOffset, dstSavings;
tokyo->getOffset(Calendar::getNow(), false, rawOffset, dstSavings, status);
if (U_FAILURE(status)) {
errln("FAIL: getOffset failed for tokyo");
}
if (rawOffset != 9*HOUR || dstSavings != 0) {
errln("FAIL: Bad offsets returned by a VTimeZone created for Tokyo");
}
}
delete tokyo;
// Create VTimeZone from VTIMEZONE data
static const char16_t fooData[] = {
/* "BEGIN:VCALENDAR\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x43,0x41,0x4C,0x45,0x4E,0x44,0x41,0x52,0x0D,0x0A,
/* "BEGIN:VTIMEZONE\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
/* "TZID:FOO\x0D\x0A" */
0x54,0x5A,0x49,0x44,0x3A,0x46,0x4F,0x4F,0x0D,0x0A,
/* "BEGIN:STANDARD\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
/* "TZOFFSETFROM:-0700\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2D,0x30,0x37,0x30,0x30,0x0D,0x0A,
/* "TZOFFSETTO:-0800\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2D,0x30,0x38,0x30,0x30,0x0D,0x0A,
/* "TZNAME:FST\x0D\x0A" */
0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x46,0x53,0x54,0x0D,0x0A,
/* "DTSTART:20071010T010000\x0D\x0A" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x32,0x30,0x30,0x37,0x31,0x30,0x31,0x30,0x54,0x30,0x31,0x30,0x30,0x30,0x30,0x0D,0x0A,
/* "RRULE:FREQ=YEARLY;BYDAY=WE;BYMONTHDAY=10,11,12,13,14,15,16;BYMONTH=10\x0D\x0A" */
0x52,0x52,0x55,0x4C,0x45,0x3A,0x46,0x52,0x45,0x51,0x3D,0x59,0x45,0x41,0x52,0x4C,0x59,0x3B,0x42,0x59,0x44,0x41,0x59,0x3D,0x57,0x45,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x44,0x41,0x59,0x3D,0x31,0x30,0x2C,0x31,0x31,0x2C,0x31,0x32,0x2C,0x31,0x33,0x2C,0x31,0x34,0x2C,0x31,0x35,0x2C,0x31,0x36,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x3D,0x31,0x30,0x0D,0x0A,
/* "END:STANDARD\x0D\x0A" */
0x45,0x4E,0x44,0x3A,0x53,0x54,0x41,0x4E,0x44,0x41,0x52,0x44,0x0D,0x0A,
/* "BEGIN:DAYLIGHT\x0D\x0A" */
0x42,0x45,0x47,0x49,0x4E,0x3A,0x44,0x41,0x59,0x4C,0x49,0x47,0x48,0x54,0x0D,0x0A,
/* "TZOFFSETFROM:-0800\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4F,0x4D,0x3A,0x2D,0x30,0x38,0x30,0x30,0x0D,0x0A,
/* "TZOFFSETTO:-0700\x0D\x0A" */
0x54,0x5A,0x4F,0x46,0x46,0x53,0x45,0x54,0x54,0x4F,0x3A,0x2D,0x30,0x37,0x30,0x30,0x0D,0x0A,
/* "TZNAME:FDT\x0D\x0A" */
0x54,0x5A,0x4E,0x41,0x4D,0x45,0x3A,0x46,0x44,0x54,0x0D,0x0A,
/* "DTSTART:20070415T010000\x0D\x0A" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3A,0x32,0x30,0x30,0x37,0x30,0x34,0x31,0x35,0x54,0x30,0x31,0x30,0x30,0x30,0x30,0x0D,0x0A,
/* "RRULE:FREQ=YEARLY;BYMONTHDAY=15;BYMONTH=4\x0D\x0A" */
0x52,0x52,0x55,0x4C,0x45,0x3A,0x46,0x52,0x45,0x51,0x3D,0x59,0x45,0x41,0x52,0x4C,0x59,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x44,0x41,0x59,0x3D,0x31,0x35,0x3B,0x42,0x59,0x4D,0x4F,0x4E,0x54,0x48,0x3D,0x34,0x0D,0x0A,
/* "END:DAYLIGHT\x0D\x0A" */
0x45,0x4E,0x44,0x3A,0x44,0x41,0x59,0x4C,0x49,0x47,0x48,0x54,0x0D,0x0A,
/* "END:VTIMEZONE\x0D\x0A" */
0x45,0x4E,0x44,0x3A,0x56,0x54,0x49,0x4D,0x45,0x5A,0x4F,0x4E,0x45,0x0D,0x0A,
/* "END:VCALENDAR" */
0x45,0x4E,0x44,0x3A,0x56,0x43,0x41,0x4C,0x45,0x4E,0x44,0x41,0x52,
0
};
VTimeZone *foo = VTimeZone::createVTimeZone(fooData, status);
if (U_FAILURE(status) || foo == nullptr) {
errln("FAIL: Failed to create a VTimeZone foo");
} else {
// Write VTIMEZONE data
UnicodeString fooData2;
foo->write(getUTCMillis(2005, UCAL_JANUARY, 1), fooData2, status);
if (U_FAILURE(status)) {
errln("FAIL: Failed to write VTIMEZONE data for foo");
}
logln(fooData2);
}
delete foo;
}
void
TimeZoneRuleTest::TestT6216() {
// Test case in #6216
static const char16_t tokyoTZ[] = {
/* "BEGIN:VCALENDAR\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
/* "VERSION:2.0\r\n" */
0x56,0x45,0x52,0x53,0x49,0x4f,0x4e,0x3a,0x32,0x2e,0x30,0x0d,0x0a,
/* "PRODID:-//PYVOBJECT//NONSGML Version 1//EN\r\n" */
0x50,0x52,0x4f,0x44,0x49,0x44,0x3a,0x2d,0x2f,0x2f,0x50,0x59,0x56,0x4f,0x42,0x4a,0x45,0x43,0x54,0x2f,0x2f,0x4e,0x4f,0x4e,0x53,0x47,0x4d,0x4c,0x20,0x56,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x31,0x2f,0x2f,0x45,0x4e,0x0d,0x0a,
/* "BEGIN:VTIMEZONE\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "TZID:Asia/Tokyo\r\n" */
0x54,0x5a,0x49,0x44,0x3a,0x41,0x73,0x69,0x61,0x2f,0x54,0x6f,0x6b,0x79,0x6f,0x0d,0x0a,
/* "BEGIN:STANDARD\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "DTSTART:20000101T000000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x32,0x30,0x30,0x30,0x30,0x31,0x30,0x31,0x54,0x30,0x30,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYMONTH=1\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x31,0x0d,0x0a,
/* "TZNAME:Asia/Tokyo\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x41,0x73,0x69,0x61,0x2f,0x54,0x6f,0x6b,0x79,0x6f,0x0d,0x0a,
/* "TZOFFSETFROM:+0900\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2b,0x30,0x39,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:+0900\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2b,0x30,0x39,0x30,0x30,0x0d,0x0a,
/* "END:STANDARD\r\n" */
0x45,0x4e,0x44,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "END:VTIMEZONE\r\n" */
0x45,0x4e,0x44,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "END:VCALENDAR" */
0x45,0x4e,0x44,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
0
};
// Single final rule, overlapping with another
static const char16_t finalOverlap[] = {
/* "BEGIN:VCALENDAR\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
/* "BEGIN:VTIMEZONE\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "TZID:FinalOverlap\r\n" */
0x54,0x5a,0x49,0x44,0x3a,0x46,0x69,0x6e,0x61,0x6c,0x4f,0x76,0x65,0x72,0x6c,0x61,0x70,0x0d,0x0a,
/* "BEGIN:STANDARD\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "TZOFFSETFROM:-0200\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2d,0x30,0x32,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:-0300\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2d,0x30,0x33,0x30,0x30,0x0d,0x0a,
/* "TZNAME:STD\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x53,0x54,0x44,0x0d,0x0a,
/* "DTSTART:20001029T020000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x32,0x30,0x30,0x30,0x31,0x30,0x32,0x39,0x54,0x30,0x32,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x44,0x41,0x59,0x3d,0x2d,0x31,0x53,0x55,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x31,0x30,0x0d,0x0a,
/* "END:STANDARD\r\n" */
0x45,0x4e,0x44,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "BEGIN:DAYLIGHT\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x44,0x41,0x59,0x4c,0x49,0x47,0x48,0x54,0x0d,0x0a,
/* "TZOFFSETFROM:-0300\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2d,0x30,0x33,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:-0200\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2d,0x30,0x32,0x30,0x30,0x0d,0x0a,
/* "TZNAME:DST\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x44,0x53,0x54,0x0d,0x0a,
/* "DTSTART:19990404T020000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x31,0x39,0x39,0x39,0x30,0x34,0x30,0x34,0x54,0x30,0x32,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x44,0x41,0x59,0x3d,0x31,0x53,0x55,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x34,0x3b,0x55,0x4e,0x54,0x49,0x4c,0x3d,0x32,0x30,0x30,0x35,0x30,0x34,0x30,0x33,0x54,0x30,0x34,0x30,0x30,0x30,0x30,0x5a,0x0d,0x0a,
/* "END:DAYLIGHT\r\n" */
0x45,0x4e,0x44,0x3a,0x44,0x41,0x59,0x4c,0x49,0x47,0x48,0x54,0x0d,0x0a,
/* "END:VTIMEZONE\r\n" */
0x45,0x4e,0x44,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "END:VCALENDAR" */
0x45,0x4e,0x44,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
0
};
// Single final rule, no overlapping with another
static const char16_t finalNonOverlap[] = {
/* "BEGIN:VCALENDAR\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
/* "BEGIN:VTIMEZONE\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "TZID:FinalNonOverlap\r\n" */
0x54,0x5a,0x49,0x44,0x3a,0x46,0x69,0x6e,0x61,0x6c,0x4e,0x6f,0x6e,0x4f,0x76,0x65,0x72,0x6c,0x61,0x70,0x0d,0x0a,
/* "BEGIN:STANDARD\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "TZOFFSETFROM:-0200\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2d,0x30,0x32,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:-0300\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2d,0x30,0x33,0x30,0x30,0x0d,0x0a,
/* "TZNAME:STD\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x53,0x54,0x44,0x0d,0x0a,
/* "DTSTART:20001029T020000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x32,0x30,0x30,0x30,0x31,0x30,0x32,0x39,0x54,0x30,0x32,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10;UNTIL=20041031T040000Z\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x44,0x41,0x59,0x3d,0x2d,0x31,0x53,0x55,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x31,0x30,0x3b,0x55,0x4e,0x54,0x49,0x4c,0x3d,0x32,0x30,0x30,0x34,0x31,0x30,0x33,0x31,0x54,0x30,0x34,0x30,0x30,0x30,0x30,0x5a,0x0d,0x0a,
/* "END:STANDARD\r\n" */
0x45,0x4e,0x44,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "BEGIN:DAYLIGHT\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x44,0x41,0x59,0x4c,0x49,0x47,0x48,0x54,0x0d,0x0a,
/* "TZOFFSETFROM:-0300\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2d,0x30,0x33,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:-0200\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2d,0x30,0x32,0x30,0x30,0x0d,0x0a,
/* "TZNAME:DST\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x44,0x53,0x54,0x0d,0x0a,
/* "DTSTART:19990404T020000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x31,0x39,0x39,0x39,0x30,0x34,0x30,0x34,0x54,0x30,0x32,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x44,0x41,0x59,0x3d,0x31,0x53,0x55,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x34,0x3b,0x55,0x4e,0x54,0x49,0x4c,0x3d,0x32,0x30,0x30,0x35,0x30,0x34,0x30,0x33,0x54,0x30,0x34,0x30,0x30,0x30,0x30,0x5a,0x0d,0x0a,
/* "END:DAYLIGHT\r\n" */
0x45,0x4e,0x44,0x3a,0x44,0x41,0x59,0x4c,0x49,0x47,0x48,0x54,0x0d,0x0a,
/* "BEGIN:STANDARD\r\n" */
0x42,0x45,0x47,0x49,0x4e,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "TZOFFSETFROM:-0200\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x46,0x52,0x4f,0x4d,0x3a,0x2d,0x30,0x32,0x30,0x30,0x0d,0x0a,
/* "TZOFFSETTO:-0300\r\n" */
0x54,0x5a,0x4f,0x46,0x46,0x53,0x45,0x54,0x54,0x4f,0x3a,0x2d,0x30,0x33,0x30,0x30,0x0d,0x0a,
/* "TZNAME:STDFINAL\r\n" */
0x54,0x5a,0x4e,0x41,0x4d,0x45,0x3a,0x53,0x54,0x44,0x46,0x49,0x4e,0x41,0x4c,0x0d,0x0a,
/* "DTSTART:20071028T020000\r\n" */
0x44,0x54,0x53,0x54,0x41,0x52,0x54,0x3a,0x32,0x30,0x30,0x37,0x31,0x30,0x32,0x38,0x54,0x30,0x32,0x30,0x30,0x30,0x30,0x0d,0x0a,
/* "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" */
0x52,0x52,0x55,0x4c,0x45,0x3a,0x46,0x52,0x45,0x51,0x3d,0x59,0x45,0x41,0x52,0x4c,0x59,0x3b,0x42,0x59,0x44,0x41,0x59,0x3d,0x2d,0x31,0x53,0x55,0x3b,0x42,0x59,0x4d,0x4f,0x4e,0x54,0x48,0x3d,0x31,0x30,0x0d,0x0a,
/* "END:STANDARD\r\n" */
0x45,0x4e,0x44,0x3a,0x53,0x54,0x41,0x4e,0x44,0x41,0x52,0x44,0x0d,0x0a,
/* "END:VTIMEZONE\r\n" */
0x45,0x4e,0x44,0x3a,0x56,0x54,0x49,0x4d,0x45,0x5a,0x4f,0x4e,0x45,0x0d,0x0a,
/* "END:VCALENDAR" */
0x45,0x4e,0x44,0x3a,0x56,0x43,0x41,0x4c,0x45,0x4e,0x44,0x41,0x52,0x0d,0x0a,
0
};
static const int32_t TestDates[][3] = {
{1995, UCAL_JANUARY, 1},
{1995, UCAL_JULY, 1},
{2000, UCAL_JANUARY, 1},
{2000, UCAL_JULY, 1},
{2005, UCAL_JANUARY, 1},
{2005, UCAL_JULY, 1},
{2010, UCAL_JANUARY, 1},
{2010, UCAL_JULY, 1},
{0, 0, 0}
};
/*static*/ const UnicodeString TestZones[] = {
UnicodeString(tokyoTZ),
UnicodeString(finalOverlap),
UnicodeString(finalNonOverlap),
UnicodeString()
};
int32_t Expected[][8] = {
// JAN90 JUL90 JAN00 JUL00 JAN05 JUL05 JAN10 JUL10
{ 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000},
{-10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000},
{-10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000}
};
int32_t i, j;
// Get test times
UDate times[UPRV_LENGTHOF(TestDates)];
int32_t numTimes;
UErrorCode status = U_ZERO_ERROR;
TimeZone *utc = TimeZone::createTimeZone("Etc/GMT");
GregorianCalendar cal(utc, status);
if (U_FAILURE(status)) {
dataerrln("FAIL: Failed to create a GregorianCalendar: %s", u_errorName(status));
return;
}
for (i = 0; TestDates[i][2] != 0; i++) {
cal.clear();
cal.set(TestDates[i][0], TestDates[i][1], TestDates[i][2]);
times[i] = cal.getTime(status);
if (U_FAILURE(status)) {
errln("FAIL: getTime failed");
return;
}
}
numTimes = i;
// Test offset
for (i = 0; !TestZones[i].isEmpty(); i++) {
VTimeZone *vtz = VTimeZone::createVTimeZone(TestZones[i], status);
if (U_FAILURE(status)) {
errln("FAIL: failed to create VTimeZone");
continue;
}
for (j = 0; j < numTimes; j++) {
int32_t raw, dst;
status = U_ZERO_ERROR;
vtz->getOffset(times[j], false, raw, dst, status);
if (U_FAILURE(status)) {
errln(UnicodeString("FAIL: getOffset failed for time zone ") + i + " at " + times[j]);
}
int32_t offset = raw + dst;
if (offset != Expected[i][j]) {
errln(UnicodeString("FAIL: Invalid offset at time(") + times[j] + "):" + offset + " Expected:" + Expected[i][j]);
}
}
delete vtz;
}
}
void
TimeZoneRuleTest::TestT6669() {
UErrorCode status = U_ZERO_ERROR;
SimpleTimeZone stz(0, "CustomID", UCAL_JANUARY, 1, UCAL_SUNDAY, 0, UCAL_JULY, 1, UCAL_SUNDAY, 0, status);
if (U_FAILURE(status)) {
errln("FAIL: Failed to create a SimpleTimeZone");
return;
}
UDate t = 1230681600000.0; //2008-12-31T00:00:00
UDate expectedNext = 1231027200000.0; //2009-01-04T00:00:00
UDate expectedPrev = 1215298800000.0; //2008-07-06T00:00:00
TimeZoneTransition tzt;
UBool avail = stz.getNextTransition(t, false, tzt);
if (!avail) {
errln("FAIL: No transition returned by getNextTransition.");
} else if (tzt.getTime() != expectedNext) {
errln(UnicodeString("FAIL: Wrong transition time returned by getNextTransition - ")
+ tzt.getTime() + " Expected: " + expectedNext);
}
avail = stz.getPreviousTransition(t, true, tzt);
if (!avail) {
errln("FAIL: No transition returned by getPreviousTransition.");
} else if (tzt.getTime() != expectedPrev) {
errln(UnicodeString("FAIL: Wrong transition time returned by getPreviousTransition - ")
+ tzt.getTime() + " Expected: " + expectedPrev);
}
}
void
TimeZoneRuleTest::TestVTimeZoneWrapper() {
#if 0
// local variables
UBool b;
char16_t * data = nullptr;
int32_t length = 0;
int32_t i;
UDate result;
UDate base = 1231027200000.0; //2009-01-04T00:00:00
UErrorCode status;
const char *name = "Test Initial";
char16_t uname[20];
UClassID cid1;
UClassID cid2;
ZRule * r;
IZRule* ir1;
IZRule* ir2;
ZTrans* zt1;
ZTrans* zt2;
VZone* v1;
VZone* v2;
uprv_memset(uname, 0, sizeof(uname));
u_uastrcpy(uname, name);
// create rules
ir1 = izrule_open(uname, 13, 2*HOUR, 0);
ir2 = izrule_clone(ir1);
// test equality
b = izrule_equals(ir1, ir2);
b = izrule_isEquivalentTo(ir1, ir2);
// test accessors
izrule_getName(ir1, data, length);
i = izrule_getRawOffset(ir1);
i = izrule_getDSTSavings(ir1);
b = izrule_getFirstStart(ir1, 2*HOUR, 0, result);
b = izrule_getFinalStart(ir1, 2*HOUR, 0, result);
b = izrule_getNextStart(ir1, base , 2*HOUR, 0, true, result);
b = izrule_getPreviousStart(ir1, base, 2*HOUR, 0, true, result);
// test class ids
cid1 = izrule_getStaticClassID(ir1);
cid2 = izrule_getDynamicClassID(ir1);
// test transitions
zt1 = ztrans_open(base, ir1, ir2);
zt2 = ztrans_clone(zt1);
zt2 = ztrans_openEmpty();
// test equality
b = ztrans_equals(zt1, zt2);
// test accessors
result = ztrans_getTime(zt1);
ztrans_setTime(zt1, result);
r = (ZRule*)ztrans_getFrom(zt1);
ztrans_setFrom(zt1, (void*)ir1);
ztrans_adoptFrom(zt1, (void*)ir1);
r = (ZRule*)ztrans_getTo(zt1);
ztrans_setTo(zt1, (void*)ir2);
ztrans_adoptTo(zt1, (void*)ir2);
// test class ids
cid1 = ztrans_getStaticClassID(zt1);
cid2 = ztrans_getDynamicClassID(zt2);
// test vzone
v1 = vzone_openID((char16_t*)"America/Chicago", sizeof("America/Chicago"));
v2 = vzone_clone(v1);
//v2 = vzone_openData(const char16_t* vtzdata, int32_t vtzdataLength, UErrorCode& status);
// test equality
b = vzone_equals(v1, v2);
b = vzone_hasSameRules(v1, v2);
// test accessors
b = vzone_getTZURL(v1, data, length);
vzone_setTZURL(v1, data, length);
b = vzone_getLastModified(v1, result);
vzone_setLastModified(v1, result);
// test writers
vzone_write(v1, data, length, status);
vzone_writeFromStart(v1, result, data, length, status);
vzone_writeSimple(v1, result, data, length, status);
// test more accessors
i = vzone_getRawOffset(v1);
vzone_setRawOffset(v1, i);
b = vzone_useDaylightTime(v1);
b = vzone_inDaylightTime(v1, result, status);
b = vzone_getNextTransition(v1, result, false, zt1);
b = vzone_getPreviousTransition(v1, result, false, zt1);
i = vzone_countTransitionRules(v1, status);
cid1 = vzone_getStaticClassID(v1);
cid2 = vzone_getDynamicClassID(v1);
// cleanup
vzone_close(v1);
vzone_close(v2);
ztrans_close(zt1);
ztrans_close(zt2);
#endif
}
//----------- private test helpers -------------------------------------------------
UDate
TimeZoneRuleTest::getUTCMillis(int32_t y, int32_t m, int32_t d,
int32_t hr, int32_t min, int32_t sec, int32_t msec) {
UErrorCode status = U_ZERO_ERROR;
const TimeZone *tz = TimeZone::getGMT();
Calendar *cal = Calendar::createInstance(*tz, status);
if (U_FAILURE(status)) {
delete cal;
dataerrln("FAIL: Calendar::createInstance failed: %s", u_errorName(status));
return 0.0;
}
cal->set(y, m, d, hr, min, sec);
cal->set(UCAL_MILLISECOND, msec);
UDate utc = cal->getTime(status);
if (U_FAILURE(status)) {
delete cal;
errln("FAIL: Calendar::getTime failed");
return 0.0;
}
delete cal;
return utc;
}
/*
* Check if a time shift really happens on each transition returned by getNextTransition or
* getPreviousTransition in the specified time range
*/
void
TimeZoneRuleTest::verifyTransitions(BasicTimeZone& icutz, UDate start, UDate end) {
UErrorCode status = U_ZERO_ERROR;
UDate time;
int32_t raw, dst, raw0, dst0;
TimeZoneTransition tzt, tzt0;
UBool avail;
UBool first = true;
UnicodeString tzid;
// Ascending
time = start;
while (true) {
avail = icutz.getNextTransition(time, false, tzt);
if (!avail) {
break;
}
time = tzt.getTime();
if (time >= end) {
break;
}
icutz.getOffset(time, false, raw, dst, status);
icutz.getOffset(time - 1, false, raw0, dst0, status);
if (U_FAILURE(status)) {
errln("FAIL: Error in getOffset");
break;
}
if (raw == raw0 && dst == dst0) {
errln(UnicodeString("FAIL: False transition returned by getNextTransition for ")
+ icutz.getID(tzid) + " at " + dateToString(time));
}
if (!first &&
(tzt0.getTo()->getRawOffset() != tzt.getFrom()->getRawOffset()
|| tzt0.getTo()->getDSTSavings() != tzt.getFrom()->getDSTSavings())) {
errln(UnicodeString("FAIL: TO rule of the previous transition does not match FROM rule of this transition at ")
+ dateToString(time) + " for " + icutz.getID(tzid));
}
tzt0 = tzt;
first = false;
}
// Descending
first = true;
time = end;
while(true) {
avail = icutz.getPreviousTransition(time, false, tzt);
if (!avail) {
break;
}
time = tzt.getTime();
if (time <= start) {
break;
}
icutz.getOffset(time, false, raw, dst, status);
icutz.getOffset(time - 1, false, raw0, dst0, status);
if (U_FAILURE(status)) {
errln("FAIL: Error in getOffset");
break;
}
if (raw == raw0 && dst == dst0) {
errln(UnicodeString("FAIL: False transition returned by getPreviousTransition for ")
+ icutz.getID(tzid) + " at " + dateToString(time));
}
if (!first &&
(tzt0.getFrom()->getRawOffset() != tzt.getTo()->getRawOffset()
|| tzt0.getFrom()->getDSTSavings() != tzt.getTo()->getDSTSavings())) {
errln(UnicodeString("FAIL: TO rule of the next transition does not match FROM rule in this transition at ")
+ dateToString(time) + " for " + icutz.getID(tzid));
}
tzt0 = tzt;
first = false;
}
}
/*
* Compare all time transitions in 2 time zones in the specified time range in ascending order
*/
void
TimeZoneRuleTest::compareTransitionsAscending(BasicTimeZone& z1, BasicTimeZone& z2,
UDate start, UDate end, UBool inclusive) {
UnicodeString zid1, zid2;
TimeZoneTransition tzt1, tzt2;
UBool avail1, avail2;
UBool inRange1, inRange2;
z1.getID(zid1);
z2.getID(zid2);
UDate time = start;
while (true) {
avail1 = z1.getNextTransition(time, inclusive, tzt1);
avail2 = z2.getNextTransition(time, inclusive, tzt2);
inRange1 = inRange2 = false;
if (avail1) {
if (tzt1.getTime() < end || (inclusive && tzt1.getTime() == end)) {
inRange1 = true;
}
}
if (avail2) {
if (tzt2.getTime() < end || (inclusive && tzt2.getTime() == end)) {
inRange2 = true;
}
}
if (!inRange1 && !inRange2) {
// No more transition in the range
break;
}
if (!inRange1) {
errln(UnicodeString("FAIL: ") + zid1 + " does not have any transitions after "
+ dateToString(time) + " before " + dateToString(end));
break;
}
if (!inRange2) {
errln(UnicodeString("FAIL: ") + zid2 + " does not have any transitions after "
+ dateToString(time) + " before " + dateToString(end));
break;
}
if (tzt1.getTime() != tzt2.getTime()) {
errln(UnicodeString("FAIL: First transition after ") + dateToString(time) + " "
+ zid1 + "[" + dateToString(tzt1.getTime()) + "] "
+ zid2 + "[" + dateToString(tzt2.getTime()) + "]");
break;
}
time = tzt1.getTime();
if (inclusive) {
time += 1;
}
}
}
/*
* Compare all time transitions in 2 time zones in the specified time range in descending order
*/
void
TimeZoneRuleTest::compareTransitionsDescending(BasicTimeZone& z1, BasicTimeZone& z2,
UDate start, UDate end, UBool inclusive) {
UnicodeString zid1, zid2;
TimeZoneTransition tzt1, tzt2;
UBool avail1, avail2;
UBool inRange1, inRange2;
z1.getID(zid1);
z2.getID(zid2);
UDate time = end;
while (true) {
avail1 = z1.getPreviousTransition(time, inclusive, tzt1);
avail2 = z2.getPreviousTransition(time, inclusive, tzt2);
inRange1 = inRange2 = false;
if (avail1) {
if (tzt1.getTime() > start || (inclusive && tzt1.getTime() == start)) {
inRange1 = true;
}
}
if (avail2) {
if (tzt2.getTime() > start || (inclusive && tzt2.getTime() == start)) {
inRange2 = true;
}
}
if (!inRange1 && !inRange2) {
// No more transition in the range
break;
}
if (!inRange1) {
errln(UnicodeString("FAIL: ") + zid1 + " does not have any transitions before "
+ dateToString(time) + " after " + dateToString(start));
break;
}
if (!inRange2) {
errln(UnicodeString("FAIL: ") + zid2 + " does not have any transitions before "
+ dateToString(time) + " after " + dateToString(start));
break;
}
if (tzt1.getTime() != tzt2.getTime()) {
errln(UnicodeString("FAIL: Last transition before ") + dateToString(time) + " "
+ zid1 + "[" + dateToString(tzt1.getTime()) + "] "
+ zid2 + "[" + dateToString(tzt2.getTime()) + "]");
break;
}
time = tzt1.getTime();
if (inclusive) {
time -= 1;
}
}
}
// Slightly modified version of BasicTimeZone::hasEquivalentTransitions.
// This version returns true if transition time delta is within the given
// delta range.
static UBool hasEquivalentTransitions(/*const*/ BasicTimeZone& tz1, /*const*/BasicTimeZone& tz2,
UDate start, UDate end,
UBool ignoreDstAmount, int32_t maxTransitionTimeDelta,
UErrorCode& status) {
if (U_FAILURE(status)) {
return false;
}
if (tz1.hasSameRules(tz2)) {
return true;
}
// Check the offsets at the start time
int32_t raw1, raw2, dst1, dst2;
tz1.getOffset(start, false, raw1, dst1, status);
if (U_FAILURE(status)) {
return false;
}
tz2.getOffset(start, false, raw2, dst2, status);
if (U_FAILURE(status)) {
return false;
}
if (ignoreDstAmount) {
if ((raw1 + dst1 != raw2 + dst2)
|| (dst1 != 0 && dst2 == 0)
|| (dst1 == 0 && dst2 != 0)) {
return false;
}
} else {
if (raw1 != raw2 || dst1 != dst2) {
return false;
}
}
// Check transitions in the range
UDate time = start;
TimeZoneTransition tr1, tr2;
while (true) {
UBool avail1 = tz1.getNextTransition(time, false, tr1);
UBool avail2 = tz2.getNextTransition(time, false, tr2);
if (ignoreDstAmount) {
// Skip a transition which only differ the amount of DST savings
while (true) {
if (avail1
&& tr1.getTime() <= end
&& (tr1.getFrom()->getRawOffset() + tr1.getFrom()->getDSTSavings()
== tr1.getTo()->getRawOffset() + tr1.getTo()->getDSTSavings())
&& (tr1.getFrom()->getDSTSavings() != 0 && tr1.getTo()->getDSTSavings() != 0)) {
tz1.getNextTransition(tr1.getTime(), false, tr1);
} else {
break;
}
}
while (true) {
if (avail2
&& tr2.getTime() <= end
&& (tr2.getFrom()->getRawOffset() + tr2.getFrom()->getDSTSavings()
== tr2.getTo()->getRawOffset() + tr2.getTo()->getDSTSavings())
&& (tr2.getFrom()->getDSTSavings() != 0 && tr2.getTo()->getDSTSavings() != 0)) {
tz2.getNextTransition(tr2.getTime(), false, tr2);
} else {
break;
}
}
}
UBool inRange1 = (avail1 && tr1.getTime() <= end);
UBool inRange2 = (avail2 && tr2.getTime() <= end);
if (!inRange1 && !inRange2) {
// No more transition in the range
break;
}
if (!inRange1 || !inRange2) {
return false;
}
double delta = tr1.getTime() >= tr2.getTime() ? tr1.getTime() - tr2.getTime() : tr2.getTime() - tr1.getTime();
if (delta > static_cast<double>(maxTransitionTimeDelta)) {
return false;
}
if (ignoreDstAmount) {
if (tr1.getTo()->getRawOffset() + tr1.getTo()->getDSTSavings()
!= tr2.getTo()->getRawOffset() + tr2.getTo()->getDSTSavings()
|| (tr1.getTo()->getDSTSavings() != 0 && tr2.getTo()->getDSTSavings() == 0)
|| (tr1.getTo()->getDSTSavings() == 0 && tr2.getTo()->getDSTSavings() != 0)) {
return false;
}
} else {
if (tr1.getTo()->getRawOffset() != tr2.getTo()->getRawOffset() ||
tr1.getTo()->getDSTSavings() != tr2.getTo()->getDSTSavings()) {
return false;
}
}
time = tr1.getTime() > tr2.getTime() ? tr1.getTime() : tr2.getTime();
}
return true;
}
// Test case for ticket#8943
// RuleBasedTimeZone#getOffsets throws NPE
void
TimeZoneRuleTest::TestT8943() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString id("Ekaterinburg Time");
UnicodeString stdName("Ekaterinburg Standard Time");
UnicodeString dstName("Ekaterinburg Daylight Time");
InitialTimeZoneRule *initialRule = new InitialTimeZoneRule(stdName, 18000000, 0);
RuleBasedTimeZone *rbtz = new RuleBasedTimeZone(id, initialRule);
DateTimeRule *dtRule = new DateTimeRule(UCAL_OCTOBER, -1, UCAL_SUNDAY, 10800000, DateTimeRule::WALL_TIME);
AnnualTimeZoneRule *atzRule = new AnnualTimeZoneRule(stdName, 18000000, 0, dtRule, 2000, 2010);
rbtz->addTransitionRule(atzRule, status);
dtRule = new DateTimeRule(UCAL_MARCH, -1, UCAL_SUNDAY, 7200000, DateTimeRule::WALL_TIME);
atzRule = new AnnualTimeZoneRule(dstName, 18000000, 3600000, dtRule, 2000, 2010);
rbtz->addTransitionRule(atzRule, status);
dtRule = new DateTimeRule(UCAL_JANUARY, 1, 0, DateTimeRule::WALL_TIME);
atzRule = new AnnualTimeZoneRule(stdName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(atzRule, status);
dtRule = new DateTimeRule(UCAL_JANUARY, 1, 1, DateTimeRule::WALL_TIME);
atzRule = new AnnualTimeZoneRule(dstName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule::MAX_YEAR);
rbtz->addTransitionRule(atzRule, status);
rbtz->complete(status);
if (U_FAILURE(status)) {
errln("Failed to construct a RuleBasedTimeZone");
} else {
int32_t raw, dst;
rbtz->getOffset(1293822000000.0 /* 2010-12-31 19:00:00 UTC */, false, raw, dst, status);
if (U_FAILURE(status)) {
errln("Error invoking getOffset");
} else if (raw != 21600000 || dst != 0) {
errln(UnicodeString("Fail: Wrong offsets: ") + raw + "/" + dst + " Expected: 21600000/0");
}
}
delete rbtz;
}
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof
|