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 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/***********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation
* and others. All Rights Reserved.
***********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/timezone.h"
#include "unicode/simpletz.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/localpointer.h"
#include "unicode/resbund.h"
#include "unicode/strenum.h"
#include "unicode/ustring.h"
#include "unicode/uversion.h"
#include "tztest.h"
#include "cmemory.h"
#include "putilimp.h"
#include "cstring.h"
#include "olsontz.h"
#define CASE(id,test) case id: \
name = #test; \
if (exec) { \
logln(#test "---"); logln(""); \
test(); \
} \
break
// *****************************************************************************
// class TimeZoneTest
// *****************************************************************************
// Some test case data is current date/tzdata version sensitive and producing errors
// when year/rule are changed. Although we want to keep our eyes on test failures
// caused by tzdata changes while development, keep maintaining test data in maintenance
// stream is a little bit hassle. ICU 49 or later versions are using minor version field
// to indicate a development build (0) or official release build (others). For development
// builds, a test failure triggers an error, while release builds only report them in
// verbose mode with logln.
static UBool isDevelopmentBuild = (U_ICU_VERSION_MINOR_NUM == 0);
void TimeZoneTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) {
logln("TestSuite TestTimeZone");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestPRTOffset);
TESTCASE_AUTO(TestVariousAPI518);
TESTCASE_AUTO(TestGetAvailableIDs913);
TESTCASE_AUTO(TestGenericAPI);
TESTCASE_AUTO(TestRuleAPI);
TESTCASE_AUTO(TestShortZoneIDs);
TESTCASE_AUTO(TestCustomParse);
TESTCASE_AUTO(TestDisplayName);
TESTCASE_AUTO(TestDSTSavings);
TESTCASE_AUTO(TestAlternateRules);
TESTCASE_AUTO(TestCountries);
TESTCASE_AUTO(TestHistorical);
TESTCASE_AUTO(TestEquivalentIDs);
TESTCASE_AUTO(TestAliasedNames);
TESTCASE_AUTO(TestFractionalDST);
TESTCASE_AUTO(TestFebruary);
TESTCASE_AUTO(TestCanonicalIDAPI);
TESTCASE_AUTO(TestCanonicalID);
TESTCASE_AUTO(TestDisplayNamesMeta);
TESTCASE_AUTO(TestGetRegion);
TESTCASE_AUTO(TestGetAvailableIDsNew);
TESTCASE_AUTO(TestGetUnknown);
TESTCASE_AUTO(TestGetGMT);
TESTCASE_AUTO(TestGetWindowsID);
TESTCASE_AUTO(TestGetIDForWindowsID);
TESTCASE_AUTO(TestCasablancaNameAndOffset22041);
TESTCASE_AUTO(TestRawOffsetAndOffsetConsistency22041);
TESTCASE_AUTO(TestGetIanaID);
TESTCASE_AUTO(TestGMTMinus24ICU22526);
TESTCASE_AUTO_END;
}
const int32_t TimeZoneTest::millisPerHour = 3600000;
// ---------------------------------------------------------------------------------
/**
* Generic API testing for API coverage.
*/
void
TimeZoneTest::TestGenericAPI()
{
UnicodeString id("NewGMT");
int32_t offset = 12345;
SimpleTimeZone *zone = new SimpleTimeZone(offset, id);
if (zone->useDaylightTime()) errln("FAIL: useDaylightTime should return false");
TimeZone* zoneclone = zone->clone();
if (!(*zoneclone == *zone)) errln("FAIL: clone or operator== failed");
zoneclone->setID("abc");
if (!(*zoneclone != *zone)) errln("FAIL: clone or operator!= failed");
delete zoneclone;
zoneclone = zone->clone();
if (!(*zoneclone == *zone)) errln("FAIL: clone or operator== failed");
zoneclone->setRawOffset(45678);
if (!(*zoneclone != *zone)) errln("FAIL: clone or operator!= failed");
SimpleTimeZone copy(*zone);
if (!(copy == *zone)) errln("FAIL: copy constructor or operator== failed");
copy = *dynamic_cast<SimpleTimeZone*>(zoneclone);
if (!(copy == *zoneclone)) errln("FAIL: assignment operator or operator== failed");
TimeZone* saveDefault = TimeZone::createDefault();
logln(UnicodeString("TimeZone::createDefault() => ") + saveDefault->getID(id));
TimeZone::adoptDefault(zone);
TimeZone* defaultzone = TimeZone::createDefault();
if (defaultzone == zone ||
!(*defaultzone == *zone))
errln("FAIL: createDefault failed");
TimeZone::adoptDefault(saveDefault);
delete defaultzone;
delete zoneclone;
logln("call uprv_timezone() which uses the host");
logln("to get the difference in seconds between coordinated universal");
logln("time and local time. E.g., -28,800 for PST (GMT-8hrs)");
int32_t tzoffset = uprv_timezone();
if ((tzoffset % 900) != 0) {
/*
* Ticket#6364 and #7648
* A few time zones are using GMT offests not a multiple of 15 minutes.
* Therefore, we should not interpret such case as an error.
* We downgrade this from errln to infoln. When we see this message,
* we should examine if it is ignorable or not.
*/
infoln("WARNING: t_timezone may be incorrect. It is not a multiple of 15min.", tzoffset);
}
TimeZone* hostZone = TimeZone::detectHostTimeZone();
int32_t hostZoneRawOffset = hostZone->getRawOffset();
logln("hostZone->getRawOffset() = %d , tzoffset = %d", hostZoneRawOffset, tzoffset * (-1000));
/* Host time zone's offset should match the offset returned by uprv_timezone() */
if (hostZoneRawOffset != tzoffset * (-1000)) {
UnicodeString id;
hostZone->getID(id);
const char* ignoreRuntimeTZSensitiveTests = getProperty("IgnoreRuntimeTimeZoneSensitiveTests");
UBool bWarnOnly = (ignoreRuntimeTZSensitiveTests && uprv_strcmp(ignoreRuntimeTZSensitiveTests, "true") == 0);
// TODO - enforce warning only until we update the CI env test
bWarnOnly = true;
// Africa/Casablanca Europe/Dublin Africa/El_Aaiun uses negative DST offset on some runtime env
if (id == u"Africa/Casablanca" || id == u"Europe/Dublin" || id == u"Africa/El_Aaiun" || bWarnOnly) {
infoln("WARN: detectHostTimeZone()'s raw offset != host timezone's offset. Time zone version used by OS might be different from ICU.\n"
"hostZone->getRawOffset()=%d\n"
"but uprv_timezone() return %d and "
"uprv_timezone() * -1000=%d",
hostZoneRawOffset, tzoffset, tzoffset * -1000);
} else {
errln("FAIL: detectHostTimeZone()'s raw offset != host timezone's offset.\n"
"hostZone->getRawOffset()=%d\n"
"but uprv_timezone() return %d and "
"uprv_timezone() * -1000=%d",
hostZoneRawOffset, tzoffset, tzoffset * -1000);
}
}
delete hostZone;
UErrorCode status = U_ZERO_ERROR;
const char* tzver = TimeZone::getTZDataVersion(status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: getTZDataVersion failed - %s", u_errorName(status));
} else {
int32_t tzverLen = uprv_strlen(tzver);
if (tzverLen == 5 || tzverLen == 6 /* 4 digits + 1 or 2 letters */) {
logln(UnicodeString("tzdata version: ") + tzver);
} else {
errln(UnicodeString("FAIL: getTZDataVersion returned ") + tzver);
}
}
}
// ---------------------------------------------------------------------------------
/**
* Test the setStartRule/setEndRule API calls.
*/
void
TimeZoneTest::TestRuleAPI()
{
UErrorCode status = U_ZERO_ERROR;
UDate offset = 60*60*1000*1.75; // Pick a weird offset
SimpleTimeZone* zone = new SimpleTimeZone(static_cast<int32_t>(offset), "TestZone");
if (zone->useDaylightTime()) errln("FAIL: useDaylightTime should return false");
// Establish our expected transition times. Do this with a non-DST
// calendar with the (above) declared local offset.
GregorianCalendar *gc = new GregorianCalendar(*zone, status);
if (failure(status, "new GregorianCalendar", true)) return;
gc->clear();
gc->set(1990, UCAL_MARCH, 1);
UDate marchOneStd = gc->getTime(status); // Local Std time midnight
gc->clear();
gc->set(1990, UCAL_JULY, 1);
UDate julyOneStd = gc->getTime(status); // Local Std time midnight
if (failure(status, "GregorianCalendar::getTime")) return;
// Starting and ending hours, WALL TIME
int32_t startHour = static_cast<int32_t>(2.25 * 3600000);
int32_t endHour = static_cast<int32_t>(3.5 * 3600000);
zone->setStartRule(UCAL_MARCH, 1, 0, startHour, status);
zone->setEndRule (UCAL_JULY, 1, 0, endHour, status);
delete gc;
gc = new GregorianCalendar(*zone, status);
if (failure(status, "new GregorianCalendar")) return;
UDate marchOne = marchOneStd + startHour;
UDate julyOne = julyOneStd + endHour - 3600000; // Adjust from wall to Std time
UDate expMarchOne = 636251400000.0;
if (marchOne != expMarchOne)
{
errln(UnicodeString("FAIL: Expected start computed as ") + marchOne +
" = " + dateToString(marchOne));
logln(UnicodeString(" Should be ") + expMarchOne +
" = " + dateToString(expMarchOne));
}
UDate expJulyOne = 646793100000.0;
if (julyOne != expJulyOne)
{
errln(UnicodeString("FAIL: Expected start computed as ") + julyOne +
" = " + dateToString(julyOne));
logln(UnicodeString(" Should be ") + expJulyOne +
" = " + dateToString(expJulyOne));
}
testUsingBinarySearch(*zone, date(90, UCAL_JANUARY, 1), date(90, UCAL_JUNE, 15), marchOne);
testUsingBinarySearch(*zone, date(90, UCAL_JUNE, 1), date(90, UCAL_DECEMBER, 31), julyOne);
if (zone->inDaylightTime(marchOne - 1000, status) ||
!zone->inDaylightTime(marchOne, status))
errln("FAIL: Start rule broken");
if (!zone->inDaylightTime(julyOne - 1000, status) ||
zone->inDaylightTime(julyOne, status))
errln("FAIL: End rule broken");
zone->setStartYear(1991);
if (zone->inDaylightTime(marchOne, status) ||
zone->inDaylightTime(julyOne - 1000, status))
errln("FAIL: Start year broken");
failure(status, "TestRuleAPI");
delete gc;
delete zone;
}
void
TimeZoneTest::findTransition(const TimeZone& tz,
UDate min, UDate max) {
UErrorCode ec = U_ZERO_ERROR;
UnicodeString id,s;
UBool startsInDST = tz.inDaylightTime(min, ec);
if (failure(ec, "TimeZone::inDaylightTime")) return;
if (tz.inDaylightTime(max, ec) == startsInDST) {
logln("Error: " + tz.getID(id) + ".inDaylightTime(" + dateToString(min) + ") = " + (startsInDST?"true":"false") +
", inDaylightTime(" + dateToString(max) + ") = " + (startsInDST?"true":"false"));
return;
}
if (failure(ec, "TimeZone::inDaylightTime")) return;
while ((max - min) > INTERVAL) {
UDate mid = (min + max) / 2;
if (tz.inDaylightTime(mid, ec) == startsInDST) {
min = mid;
} else {
max = mid;
}
if (failure(ec, "TimeZone::inDaylightTime")) return;
}
min = 1000.0 * uprv_floor(min/1000.0);
max = 1000.0 * uprv_floor(max/1000.0);
logln(tz.getID(id) + " Before: " + min/1000 + " = " +
dateToString(min,s,tz));
logln(tz.getID(id) + " After: " + max/1000 + " = " +
dateToString(max,s,tz));
}
void
TimeZoneTest::testUsingBinarySearch(const TimeZone& tz,
UDate min, UDate max,
UDate expectedBoundary)
{
UErrorCode status = U_ZERO_ERROR;
UBool startsInDST = tz.inDaylightTime(min, status);
if (failure(status, "TimeZone::inDaylightTime")) return;
if (tz.inDaylightTime(max, status) == startsInDST) {
logln("Error: inDaylightTime(" + dateToString(max) + ") != " + ((!startsInDST)?"true":"false"));
return;
}
if (failure(status, "TimeZone::inDaylightTime")) return;
while ((max - min) > INTERVAL) {
UDate mid = (min + max) / 2;
if (tz.inDaylightTime(mid, status) == startsInDST) {
min = mid;
} else {
max = mid;
}
if (failure(status, "TimeZone::inDaylightTime")) return;
}
logln(UnicodeString("Binary Search Before: ") + uprv_floor(0.5 + min) + " = " + dateToString(min));
logln(UnicodeString("Binary Search After: ") + uprv_floor(0.5 + max) + " = " + dateToString(max));
UDate mindelta = expectedBoundary - min;
UDate maxdelta = max - expectedBoundary;
if (mindelta >= 0 &&
mindelta <= INTERVAL &&
maxdelta >= 0 &&
maxdelta <= INTERVAL)
logln(UnicodeString("PASS: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
else
errln(UnicodeString("FAIL: Expected bdry: ") + expectedBoundary + " = " + dateToString(expectedBoundary));
}
const UDate TimeZoneTest::INTERVAL = 100;
// ---------------------------------------------------------------------------------
// -------------------------------------
/**
* Test the offset of the PRT timezone.
*/
void
TimeZoneTest::TestPRTOffset()
{
TimeZone* tz = TimeZone::createTimeZone("PRT");
if (tz == nullptr) {
errln("FAIL: TimeZone(PRT) is null");
}
else {
int32_t expectedHour = -4;
double expectedOffset = static_cast<double>(expectedHour) * millisPerHour;
double foundOffset = tz->getRawOffset();
int32_t foundHour = static_cast<int32_t>(foundOffset) / millisPerHour;
if (expectedOffset != foundOffset) {
dataerrln("FAIL: Offset for PRT should be %d, found %d", expectedHour, foundHour);
} else {
logln("PASS: Offset for PRT should be %d, found %d", expectedHour, foundHour);
}
}
delete tz;
}
// -------------------------------------
/**
* Regress a specific bug with a sequence of API calls.
*/
void
TimeZoneTest::TestVariousAPI518()
{
UErrorCode status = U_ZERO_ERROR;
TimeZone* time_zone = TimeZone::createTimeZone("PST");
UDate d = date(97, UCAL_APRIL, 30);
UnicodeString str;
logln("The timezone is " + time_zone->getID(str));
if (!time_zone->inDaylightTime(d, status)) dataerrln("FAIL: inDaylightTime returned false");
if (failure(status, "TimeZone::inDaylightTime", true)) return;
if (!time_zone->useDaylightTime()) dataerrln("FAIL: useDaylightTime returned false");
if (time_zone->getRawOffset() != - 8 * millisPerHour) dataerrln("FAIL: getRawOffset returned wrong value");
GregorianCalendar *gc = new GregorianCalendar(status);
if (U_FAILURE(status)) { errln("FAIL: Couldn't create GregorianCalendar"); return; }
gc->setTime(d, status);
if (U_FAILURE(status)) { errln("FAIL: GregorianCalendar::setTime failed"); return; }
if (time_zone->getOffset(gc->AD, gc->get(UCAL_YEAR, status), gc->get(UCAL_MONTH, status),
gc->get(UCAL_DATE, status), static_cast<uint8_t>(gc->get(UCAL_DAY_OF_WEEK, status)), 0, status) != -7 * millisPerHour)
dataerrln("FAIL: getOffset returned wrong value");
if (U_FAILURE(status)) { errln("FAIL: GregorianCalendar::set failed"); return; }
delete gc;
delete time_zone;
}
// -------------------------------------
/**
* Test the call which retrieves the available IDs.
*/
void
TimeZoneTest::TestGetAvailableIDs913()
{
UErrorCode ec = U_ZERO_ERROR;
int32_t i;
#ifdef U_USE_TIMEZONE_OBSOLETE_2_8
// Test legacy API -- remove these tests when the corresponding API goes away (duh)
int32_t numIDs = -1;
const UnicodeString** ids = TimeZone::createAvailableIDs(numIDs);
if (ids == 0 || numIDs < 1) {
errln("FAIL: createAvailableIDs()");
} else {
UnicodeString buf("TimeZone::createAvailableIDs() = { ");
for(i=0; i<numIDs; ++i) {
if (i) buf.append(", ");
buf.append(*ids[i]);
}
buf.append(" } ");
logln(buf + numIDs);
// we own the array; the caller owns the contained strings (yuck)
uprv_free(ids);
}
numIDs = -1;
ids = TimeZone::createAvailableIDs(-8*U_MILLIS_PER_HOUR, numIDs);
if (ids == 0 || numIDs < 1) {
errln("FAIL: createAvailableIDs(-8:00)");
} else {
UnicodeString buf("TimeZone::createAvailableIDs(-8:00) = { ");
for(i=0; i<numIDs; ++i) {
if (i) buf.append(", ");
buf.append(*ids[i]);
}
buf.append(" } ");
logln(buf + numIDs);
// we own the array; the caller owns the contained strings (yuck)
uprv_free(ids);
}
numIDs = -1;
ids = TimeZone::createAvailableIDs("US", numIDs);
if (ids == 0 || numIDs < 1) {
errln("FAIL: createAvailableIDs(US) ids=%d, numIDs=%d", ids, numIDs);
} else {
UnicodeString buf("TimeZone::createAvailableIDs(US) = { ");
for(i=0; i<numIDs; ++i) {
if (i) buf.append(", ");
buf.append(*ids[i]);
}
buf.append(" } ");
logln(buf + numIDs);
// we own the array; the caller owns the contained strings (yuck)
uprv_free(ids);
}
#endif
UnicodeString str;
UnicodeString buf(u"TimeZone::createEnumeration() = { ");
int32_t s_length;
StringEnumeration* s = TimeZone::createEnumeration(ec);
LocalPointer<StringEnumeration> tmp1(TimeZone::createEnumeration(), ec);
if (U_FAILURE(ec) || s == nullptr) {
dataerrln("Unable to create TimeZone enumeration");
return;
}
s_length = s->count(ec);
if (s_length != tmp1->count(ec)) {
errln("TimeZone::createEnumeration() with no status args returns a different count.");
}
for (i = 0; i < s_length;++i) {
if (i > 0) buf += ", ";
if ((i & 1) == 0) {
buf += *s->snext(ec);
} else {
buf += UnicodeString(s->next(nullptr, ec), "");
}
if((i % 5) == 4) {
// replace s with a clone of itself
StringEnumeration *s2 = s->clone();
if(s2 == nullptr || s_length != s2->count(ec)) {
errln("TimezoneEnumeration.clone() failed");
} else {
delete s;
s = s2;
}
}
}
buf += " };";
logln(buf);
/* Confirm that the following zones can be retrieved: The first
* zone, the last zone, and one in-between. This tests the binary
* search through the system zone data.
*/
s->reset(ec);
int32_t middle = s_length/2;
for (i=0; i<s_length; ++i) {
const UnicodeString* id = s->snext(ec);
if (i==0 || i==middle || i==(s_length-1)) {
TimeZone *z = TimeZone::createTimeZone(*id);
if (z == nullptr) {
errln(UnicodeString("FAIL: createTimeZone(") +
*id + ") -> 0");
} else if (z->getID(str) != *id) {
errln(UnicodeString("FAIL: createTimeZone(") +
*id + ") -> zone " + str);
} else {
logln(UnicodeString("OK: createTimeZone(") +
*id + ") succeeded");
}
delete z;
}
}
delete s;
buf.truncate(0);
buf += "TimeZone::createEnumeration(GMT+01:00) = { ";
s = TimeZone::createEnumerationForRawOffset(1 * U_MILLIS_PER_HOUR, ec);
LocalPointer<StringEnumeration> tmp2(TimeZone::createEnumeration(1 * U_MILLIS_PER_HOUR), ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration for GMT+1");
return;
}
s_length = s->count(ec);
if (s_length != tmp2->count(ec)) {
errln("TimeZone::createEnumeration(GMT+01:00) with no status args returns a different count.");
}
for (i = 0; i < s_length;++i) {
if (i > 0) buf += ", ";
buf += *s->snext(ec);
}
delete s;
buf += " };";
logln(buf);
buf.truncate(0);
buf += "TimeZone::createEnumeration(US) = { ";
s = TimeZone::createEnumerationForRegion("US", ec);
LocalPointer<StringEnumeration> tmp3(TimeZone::createEnumeration("US"), ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration for US");
return;
}
s_length = s->count(ec);
if (s_length != tmp3->count(ec)) {
errln("TimeZone::createEnumeration(\"US\") with no status args returns a different count.");
}
for (i = 0; i < s_length; ++i) {
if (i > 0) buf += ", ";
buf += *s->snext(ec);
}
buf += " };";
logln(buf);
TimeZone *tz = TimeZone::createTimeZone("PST");
if (tz != nullptr) logln("getTimeZone(PST) = " + tz->getID(str));
else errln("FAIL: getTimeZone(PST) = null");
delete tz;
tz = TimeZone::createTimeZone("America/Los_Angeles");
if (tz != nullptr) logln("getTimeZone(America/Los_Angeles) = " + tz->getID(str));
else errln("FAIL: getTimeZone(PST) = null");
delete tz;
// @bug 4096694
tz = TimeZone::createTimeZone("NON_EXISTENT");
UnicodeString temp;
if (tz == nullptr)
errln("FAIL: getTimeZone(NON_EXISTENT) = null");
else if (tz->getID(temp) != UCAL_UNKNOWN_ZONE_ID)
errln("FAIL: getTimeZone(NON_EXISTENT) = " + temp);
delete tz;
delete s;
}
void
TimeZoneTest::TestGetAvailableIDsNew()
{
UErrorCode ec = U_ZERO_ERROR;
StringEnumeration *any, *canonical, *canonicalLoc;
StringEnumeration *any_US, *canonical_US, *canonicalLoc_US;
StringEnumeration *any_W5, *any_CA_W5;
StringEnumeration *any_US_E14;
int32_t rawOffset;
const UnicodeString *id1, *id2;
UnicodeString canonicalID;
UBool isSystemID;
char region[4] = {0};
int32_t zoneCount;
any = canonical = canonicalLoc = any_US = canonical_US = canonicalLoc_US = any_W5 = any_CA_W5 = any_US_E14 = nullptr;
any = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr, nullptr, ec);
if (U_FAILURE(ec)) {
dataerrln("Failed to create enumeration for ANY");
goto cleanup;
}
canonical = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, nullptr, nullptr, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for CANONICAL");
goto cleanup;
}
canonicalLoc = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, nullptr, nullptr, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for CANONICALLOC");
goto cleanup;
}
any_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", nullptr, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for ANY_US");
goto cleanup;
}
canonical_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, "US", nullptr, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for CANONICAL_US");
goto cleanup;
}
canonicalLoc_US = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "US", nullptr, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for CANONICALLOC_US");
goto cleanup;
}
rawOffset = (-5)*60*60*1000;
any_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, nullptr, &rawOffset, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for ANY_W5");
goto cleanup;
}
any_CA_W5 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "CA", &rawOffset, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for ANY_CA_W5");
goto cleanup;
}
rawOffset = 14*60*60*1000;
any_US_E14 = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &rawOffset, ec);
if (U_FAILURE(ec)) {
errln("Failed to create enumeration for ANY_US_E14");
goto cleanup;
}
checkContainsAll(any, "ANY", canonical, "CANONICAL");
checkContainsAll(canonical, "CANONICAL", canonicalLoc, "CANONICALLOC");
checkContainsAll(any, "ANY", any_US, "ANY_US");
checkContainsAll(canonical, "CANONICAL", canonical_US, "CANONICAL_US");
checkContainsAll(canonicalLoc, "CANONICALLOC", canonicalLoc_US, "CANONICALLOC_US");
checkContainsAll(any_US, "ANY_US", canonical_US, "CANONICAL_US");
checkContainsAll(canonical_US, "CANONICAL_US", canonicalLoc_US, "CANONICALLOC_US");
checkContainsAll(any, "ANY", any_W5, "ANY_W5");
checkContainsAll(any_W5, "ANY_W5", any_CA_W5, "ANY_CA_W5");
// And ID in any set, but not in canonical set must not be a canonical ID
any->reset(ec);
while ((id1 = any->snext(ec)) != nullptr) {
UBool found = false;
canonical->reset(ec);
while ((id2 = canonical->snext(ec)) != nullptr) {
if (*id1 == *id2) {
found = true;
break;
}
}
if (U_FAILURE(ec)) {
break;
}
if (!found) {
TimeZone::getCanonicalID(*id1, canonicalID, isSystemID, ec);
if (U_FAILURE(ec)) {
break;
}
if (*id1 == canonicalID) {
errln(UnicodeString("FAIL: canonicalID [") + *id1 + "] is not in CANONICAL");
}
if (!isSystemID) {
errln(UnicodeString("FAIL: ANY contains non-system ID: ") + *id1);
}
}
}
if (U_FAILURE(ec)) {
errln("Error checking IDs in ANY, but not in CANONICAL");
ec = U_ZERO_ERROR;
}
// canonical set must contains only canonical IDs
canonical->reset(ec);
while ((id1 = canonical->snext(ec)) != nullptr) {
TimeZone::getCanonicalID(*id1, canonicalID, isSystemID, ec);
if (U_FAILURE(ec)) {
break;
}
if (*id1 != canonicalID) {
errln(UnicodeString("FAIL: CANONICAL contains non-canonical ID: ") + *id1);
}
if (!isSystemID) {
errln(UnicodeString("FAILE: CANONICAL contains non-system ID: ") + *id1);
}
}
if (U_FAILURE(ec)) {
errln("Error checking IDs in CANONICAL");
ec = U_ZERO_ERROR;
}
// canonicalLoc set must contain only canonical location IDs
canonicalLoc->reset(ec);
while ((id1 = canonicalLoc->snext(ec)) != nullptr) {
TimeZone::getRegion(*id1, region, sizeof(region), ec);
if (U_FAILURE(ec)) {
break;
}
if (uprv_strcmp(region, "001") == 0) {
errln(UnicodeString("FAIL: CANONICALLOC contains non location zone: ") + *id1);
}
}
if (U_FAILURE(ec)) {
errln("Error checking IDs in CANONICALLOC");
ec = U_ZERO_ERROR;
}
// any_US must contain only US zones
any_US->reset(ec);
while ((id1 = any_US->snext(ec)) != nullptr) {
TimeZone::getRegion(*id1, region, sizeof(region), ec);
if (U_FAILURE(ec)) {
break;
}
if (uprv_strcmp(region, "US") != 0) {
errln(UnicodeString("FAIL: ANY_US contains non-US zone ID: ") + *id1);
}
}
if (U_FAILURE(ec)) {
errln("Error checking IDs in ANY_US");
ec = U_ZERO_ERROR;
}
// any_W5 must contain only GMT-05:00 zones
any_W5->reset(ec);
while ((id1 = any_W5->snext(ec)) != nullptr) {
TimeZone *tz = TimeZone::createTimeZone(*id1);
if (tz->getRawOffset() != (-5)*60*60*1000) {
errln(UnicodeString("FAIL: ANY_W5 contains a zone whose offset is not -05:00: ") + *id1);
}
delete tz;
}
if (U_FAILURE(ec)) {
errln("Error checking IDs in ANY_W5");
ec = U_ZERO_ERROR;
}
// No US zone swith GMT+14:00
zoneCount = any_US_E14->count(ec);
if (U_FAILURE(ec)) {
errln("Error checking IDs in ANY_US_E14");
ec = U_ZERO_ERROR;
} else if (zoneCount != 0) {
errln("FAIL: ANY_US_E14 must be empty");
}
cleanup:
delete any;
delete canonical;
delete canonicalLoc;
delete any_US;
delete canonical_US;
delete canonicalLoc_US;
delete any_W5;
delete any_CA_W5;
delete any_US_E14;
}
void
TimeZoneTest::checkContainsAll(StringEnumeration *s1, const char *name1,
StringEnumeration *s2, const char *name2)
{
UErrorCode ec = U_ZERO_ERROR;
const UnicodeString *id1, *id2;
s2->reset(ec);
while ((id2 = s2->snext(ec)) != nullptr) {
UBool found = false;
s1->reset(ec);
while ((id1 = s1->snext(ec)) != nullptr) {
if (*id1 == *id2) {
found = true;
break;
}
}
if (!found) {
errln(UnicodeString("FAIL: ") + name1 + "does not contain "
+ *id2 + " in " + name2);
}
}
if (U_FAILURE(ec)) {
errln(UnicodeString("Error checkContainsAll for ") + name1 + " - " + name2);
}
}
/**
* NOTE: As of ICU 2.8, this test confirms that the "tz.alias"
* file, used to build ICU alias zones, is working. It also
* looks at some genuine Olson compatibility IDs. [aliu]
*
* This test is problematic. It should really just confirm that
* the list of compatibility zone IDs exist and are somewhat
* meaningful (that is, they aren't all aliases of GMT). It goes a
* bit further -- it hard-codes expectations about zone behavior,
* when in fact zones are redefined quite frequently. ICU's build
* process means that it is easy to update ICU to contain the
* latest Olson zone data, but if a zone tested here changes, then
* this test will fail. I have updated the test for 1999j data,
* but further updates will probably be required. Note that some
* of the concerts listed below no longer apply -- in particular,
* we do NOT overwrite real UNIX zones with 3-letter IDs. There
* are two points of overlap as of 1999j: MET and EET. These are
* both real UNIX zones, so we just use the official
* definition. This test has been updated to reflect this.
* 12/3/99 aliu
*
* Added tests for additional zones and aliases from the icuzones file.
* Markus Scherer 2006-nov-06
*
* [srl - from java - 7/5/1998]
* @bug 4130885
* Certain short zone IDs, used since 1.1.x, are incorrect.
*
* The worst of these is:
*
* "CAT" (Central African Time) should be GMT+2:00, but instead returns a
* zone at GMT-1:00. The zone at GMT-1:00 should be called EGT, CVT, EGST,
* or AZOST, depending on which zone is meant, but in no case is it CAT.
*
* Other wrong zone IDs:
*
* ECT (European Central Time) GMT+1:00: ECT is Ecuador Time,
* GMT-5:00. European Central time is abbreviated CEST.
*
* SST (Solomon Island Time) GMT+11:00. SST is actually Samoa Standard Time,
* GMT-11:00. Solomon Island time is SBT.
*
* NST (New Zealand Time) GMT+12:00. NST is the abbreviation for
* Newfoundland Standard Time, GMT-3:30. New Zealanders use NZST.
*
* AST (Alaska Standard Time) GMT-9:00. [This has already been noted in
* another bug.] It should be "AKST". AST is Atlantic Standard Time,
* GMT-4:00.
*
* PNT (Phoenix Time) GMT-7:00. PNT usually means Pitcairn Time,
* GMT-8:30. There is no standard abbreviation for Phoenix time, as distinct
* from MST with daylight savings.
*
* In addition to these problems, a number of zones are FAKE. That is, they
* don't match what people use in the real world.
*
* FAKE zones:
*
* EET (should be EEST)
* ART (should be EEST)
* MET (should be IRST)
* NET (should be AMST)
* PLT (should be PKT)
* BST (should be BDT)
* VST (should be ICT)
* CTT (should be CST) +
* ACT (should be CST) +
* AET (should be EST) +
* MIT (should be WST) +
* IET (should be EST) +
* PRT (should be AST) +
* CNT (should be NST)
* AGT (should be ARST)
* BET (should be EST) +
*
* + A zone with the correct name already exists and means something
* else. E.g., EST usually indicates the US Eastern zone, so it cannot be
* used for Brazil (BET).
*/
void TimeZoneTest::TestShortZoneIDs()
{
int32_t i;
// Create a small struct to hold the array
struct
{
const char *id;
int32_t offset;
UBool daylight;
}
kReferenceList [] =
{
{"HST", -600, false}, // Olson northamerica -10:00
{"AST", -540, true}, // ICU Link - America/Anchorage
{"PST", -480, true}, // ICU Link - America/Los_Angeles
{"PNT", -420, false}, // ICU Link - America/Phoenix
{"MST", -420, false}, // updated Aug 2003 aliu
{"CST", -360, true}, // Olson northamerica -7:00
{"IET", -300, true}, // ICU Link - America/Indiana/Indianapolis
{"EST", -300, false}, // Olson northamerica -5:00
{"PRT", -240, false}, // ICU Link - America/Puerto_Rico
{"CNT", -210, true}, // ICU Link - America/St_Johns
{"AGT", -180, false}, // ICU Link - America/Argentina/Buenos_Aires
// Per https://mm.icann.org/pipermail/tz-announce/2019-July/000056.html
// Brazil has canceled DST and will stay on standard time indefinitely.
{"BET", -180, false}, // ICU Link - America/Sao_Paulo
{"GMT", 0, false}, // Olson etcetera Link - Etc/GMT
{"UTC", 0, false}, // Olson etcetera 0
{"ECT", 60, true}, // ICU Link - Europe/Paris
{"MET", 60, true}, // Olson europe 1:00 C-Eur
{"CAT", 120, false}, // ICU Link - Africa/Maputo
{"ART", 120, false}, // ICU Link - Africa/Cairo
{"EET", 120, true}, // Olson europe 2:00 EU
{"EAT", 180, false}, // ICU Link - Africa/Addis_Ababa
{"NET", 240, false}, // ICU Link - Asia/Yerevan
{"PLT", 300, false}, // ICU Link - Asia/Karachi
{"IST", 330, false}, // ICU Link - Asia/Kolkata
{"BST", 360, false}, // ICU Link - Asia/Dhaka
{"VST", 420, false}, // ICU Link - Asia/Ho_Chi_Minh
{"CTT", 480, false}, // ICU Link - Asia/Shanghai
{"JST", 540, false}, // ICU Link - Asia/Tokyo
{"ACT", 570, false}, // ICU Link - Australia/Darwin
{"AET", 600, true}, // ICU Link - Australia/Sydney
{"SST", 660, false}, // ICU Link - Pacific/Guadalcanal
{"NST", 720, true}, // ICU Link - Pacific/Auckland
{"MIT", 780, false}, // ICU Link - Pacific/Apia
{"Etc/Unknown", 0, false}, // CLDR
{"SystemV/AST4ADT", -240, true},
{"SystemV/EST5EDT", -300, true},
{"SystemV/CST6CDT", -360, true},
{"SystemV/MST7MDT", -420, true},
{"SystemV/PST8PDT", -480, true},
{"SystemV/YST9YDT", -540, true},
{"SystemV/AST4", -240, false},
{"SystemV/EST5", -300, false},
{"SystemV/CST6", -360, false},
{"SystemV/MST7", -420, false},
{"SystemV/PST8", -480, false},
{"SystemV/YST9", -540, false},
{"SystemV/HST10", -600, false},
{"",0,false}
};
for(i=0;kReferenceList[i].id[0];i++) {
UnicodeString itsID(kReferenceList[i].id);
UBool ok = true;
// Check existence.
TimeZone *tz = TimeZone::createTimeZone(itsID);
if (!tz || (kReferenceList[i].offset != 0 && *tz == *TimeZone::getGMT())) {
errln("FAIL: Time Zone " + itsID + " does not exist!");
continue;
}
// Check daylight usage.
UBool usesDaylight = tz->useDaylightTime();
if (usesDaylight != kReferenceList[i].daylight) {
if (!isDevelopmentBuild) {
logln("Warning: Time Zone " + itsID + " use daylight is " +
(usesDaylight?"true":"false") +
" but it should be " +
((kReferenceList[i].daylight)?"true":"false"));
} else if (!(itsID==UnicodeString(u"ART",-1) && logKnownIssue("ICU-22436", "Wrong DST status for time zone ART"))) {
dataerrln("FAIL: Time Zone " + itsID + " use daylight is " +
(usesDaylight?"true":"false") +
" but it should be " +
((kReferenceList[i].daylight)?"true":"false"));
}
ok = false;
}
// Check offset
int32_t offsetInMinutes = tz->getRawOffset()/60000;
if (offsetInMinutes != kReferenceList[i].offset) {
if (!isDevelopmentBuild) {
logln("FAIL: Time Zone " + itsID + " raw offset is " +
offsetInMinutes +
" but it should be " + kReferenceList[i].offset);
} else {
dataerrln("FAIL: Time Zone " + itsID + " raw offset is " +
offsetInMinutes +
" but it should be " + kReferenceList[i].offset);
}
ok = false;
}
if (ok) {
logln("OK: " + itsID +
" useDaylightTime() & getRawOffset() as expected");
}
delete tz;
}
// OK now test compat
logln("Testing for compatibility zones");
const char* compatibilityMap[] = {
// This list is copied from tz.alias. If tz.alias
// changes, this list must be updated. Current as of Mar 2007
"ACT", "Australia/Darwin",
"AET", "Australia/Sydney",
"AGT", "America/Buenos_Aires",
"ART", "Africa/Cairo",
"AST", "America/Anchorage",
"BET", "America/Sao_Paulo",
"BST", "Asia/Dhaka", // # spelling changed in 2000h; was Asia/Dacca
"CAT", "Africa/Maputo",
"CNT", "America/St_Johns",
"CST", "America/Chicago",
"CTT", "Asia/Shanghai",
"EAT", "Africa/Addis_Ababa",
"ECT", "Europe/Paris",
// EET Europe/Istanbul # EET is a standard UNIX zone
// "EST", "America/New_York", # Defined as -05:00
// "HST", "Pacific/Honolulu", # Defined as -10:00
"IET", "America/Indianapolis",
"IST", "Asia/Calcutta",
"JST", "Asia/Tokyo",
// MET Asia/Tehran # MET is a standard UNIX zone
"MIT", "Pacific/Apia",
// "MST", "America/Denver", # Defined as -07:00
"NET", "Asia/Yerevan",
"NST", "Pacific/Auckland",
"PLT", "Asia/Karachi",
"PNT", "America/Phoenix",
"PRT", "America/Puerto_Rico",
"PST", "America/Los_Angeles",
"SST", "Pacific/Guadalcanal",
"UTC", "Etc/GMT",
"VST", "Asia/Saigon",
"","",""
};
for (i=0;*compatibilityMap[i];i+=2) {
UnicodeString itsID;
const char *zone1 = compatibilityMap[i];
const char *zone2 = compatibilityMap[i+1];
TimeZone *tz1 = TimeZone::createTimeZone(zone1);
TimeZone *tz2 = TimeZone::createTimeZone(zone2);
if (!tz1) {
errln(UnicodeString("FAIL: Could not find short ID zone ") + zone1);
}
if (!tz2) {
errln(UnicodeString("FAIL: Could not find long ID zone ") + zone2);
}
if (tz1 && tz2) {
// make NAME same so comparison will only look at the rest
tz2->setID(tz1->getID(itsID));
if (*tz1 != *tz2) {
errln("FAIL: " + UnicodeString(zone1) +
" != " + UnicodeString(zone2));
} else {
logln("OK: " + UnicodeString(zone1) +
" == " + UnicodeString(zone2));
}
}
delete tz1;
delete tz2;
}
}
/**
* Utility function for TestCustomParse
*/
UnicodeString& TimeZoneTest::formatOffset(int32_t offset, UnicodeString &rv) {
rv.remove();
char16_t sign = 0x002B;
if (offset < 0) {
sign = 0x002D;
offset = -offset;
}
int32_t s = offset % 60;
offset /= 60;
int32_t m = offset % 60;
int32_t h = offset / 60;
rv += sign;
if (h >= 10) {
rv += static_cast<char16_t>(0x0030 + (h / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (h % 10));
rv += static_cast<char16_t>(0x003A); /* ':' */
if (m >= 10) {
rv += static_cast<char16_t>(0x0030 + (m / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (m % 10));
if (s) {
rv += static_cast<char16_t>(0x003A); /* ':' */
if (s >= 10) {
rv += static_cast<char16_t>(0x0030 + (s / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (s % 10));
}
return rv;
}
/**
* Utility function for TestCustomParse, generating time zone ID
* string for the give offset.
*/
UnicodeString& TimeZoneTest::formatTZID(int32_t offset, UnicodeString &rv) {
rv.remove();
char16_t sign = 0x002B;
if (offset < 0) {
sign = 0x002D;
offset = -offset;
}
int32_t s = offset % 60;
offset /= 60;
int32_t m = offset % 60;
int32_t h = offset / 60;
rv += "GMT";
rv += sign;
if (h >= 10) {
rv += static_cast<char16_t>(0x0030 + (h / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (h % 10));
rv += static_cast<char16_t>(0x003A);
if (m >= 10) {
rv += static_cast<char16_t>(0x0030 + (m / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (m % 10));
if (s) {
rv += static_cast<char16_t>(0x003A);
if (s >= 10) {
rv += static_cast<char16_t>(0x0030 + (s / 10));
} else {
rv += static_cast<char16_t>(0x0030);
}
rv += static_cast<char16_t>(0x0030 + (s % 10));
}
return rv;
}
/**
* As part of the VM fix (see CCC approved RFE 4028006, bug
* 4044013), TimeZone.getTimeZone() has been modified to recognize
* generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
* GMT[+-]hh. Test this behavior here.
*
* @bug 4044013
*/
void TimeZoneTest::TestCustomParse()
{
int32_t i;
const int32_t kUnparseable = 604800; // the number of seconds in a week. More than any offset should be.
struct
{
const char16_t *customId;
int32_t expectedOffset;
}
kData[] =
{
// ID Expected offset in seconds
{u"GMT", kUnparseable}, //Isn't custom. [returns normal GMT]
{u"GMT-YOUR.AD.HERE", kUnparseable},
{u"GMT0", kUnparseable},
{u"GMT+0", (0)},
{u"GMT+1", (1*60*60)},
{u"GMT-0030", (-30*60)},
{u"GMT+15:99", kUnparseable},
{u"GMT+", kUnparseable},
{u"GMT-", kUnparseable},
{u"GMT+0:", kUnparseable},
{u"GMT-:", kUnparseable},
{u"GMT-YOUR.AD.HERE", kUnparseable},
{u"GMT+0010", (10*60)}, // Interpret this as 00:10
{u"GMT-10", (-10*60*60)},
{u"GMT+30", kUnparseable},
{u"GMT-3:30", (-(3*60+30)*60)},
{u"GMT-230", (-(2*60+30)*60)},
{u"GMT+05:13:05", ((5*60+13)*60+5)},
{u"GMT-71023", (-((7*60+10)*60+23))},
{u"GMT+01:23:45:67", kUnparseable},
{u"GMT+01:234", kUnparseable},
{u"GMT-2:31:123", kUnparseable},
{u"GMT+3:75", kUnparseable},
{u"GMT-01010101", kUnparseable},
{u"GMT-4E58", kUnparseable}, // ICU-22637
{u"GMT-4e58", kUnparseable}, // ICU-22637
{u"GMT-1E01", kUnparseable}, // ICU-22637
{u"GMT-2E01", kUnparseable}, // ICU-22637
{u"GMT-2e01", kUnparseable}, // ICU-22637
{u"GMT-9e02", kUnparseable}, // ICU-22637
{u"GMT-1e03", kUnparseable}, // ICU-22637
{u"GMT-2e03", kUnparseable}, // ICU-22637
{u"GMT-500M", kUnparseable}, // ICU-22637
{u"GMT-500T", kUnparseable}, // ICU-22637
{u"GMT-9E00", kUnparseable}, // ICU-22637
{u"GMT-0X0F", kUnparseable}, // ICU-22637
{u"GMT-0x0F", kUnparseable}, // ICU-22637
{u"GMT-0x12", kUnparseable}, // ICU-22637
{u"GMT-B111", kUnparseable}, // ICU-22637
{u"GMT-b111", kUnparseable}, // ICU-22637
{u"GMT-0b11", kUnparseable}, // ICU-22637
{u"GMT-๑๒", (-((12*60)*60))}, // ICU-22637
{u"GMT-๑๒:๓๔", (-((12*60+34)*60))}, // ICU-22637
{u"GMT+๑๒:๓๔:๕๖", ((12*60+34)*60+56)}, // ICU-22637
{nullptr, 0}
};
for (i = 0; kData[i].customId != nullptr; i++) {
UnicodeString id(kData[i].customId);
int32_t exp = kData[i].expectedOffset;
TimeZone *zone = TimeZone::createTimeZone(id);
UnicodeString itsID, temp;
OlsonTimeZone *ozone = dynamic_cast<OlsonTimeZone *>(zone);
if (ozone != nullptr) {
logln(id + " -> Olson time zone");
ozone->operator=(*ozone); // self-assignment should be a no-op
} else {
zone->getID(itsID);
int32_t ioffset = zone->getRawOffset()/1000;
UnicodeString offset, expectedID;
formatOffset(ioffset, offset);
formatTZID(ioffset, expectedID);
logln(id + " -> " + itsID + " " + offset);
if (exp == kUnparseable && itsID != UCAL_UNKNOWN_ZONE_ID) {
errln("Expected parse failure for " + id +
", got offset of " + offset +
", id " + itsID);
}
// JDK 1.3 creates custom zones with the ID "Custom"
// JDK 1.4 creates custom zones with IDs of the form "GMT+02:00"
// ICU creates custom zones with IDs of the form "GMT+02:00"
else if (exp != kUnparseable && (ioffset != exp || itsID != expectedID)) {
dataerrln("Expected offset of " + formatOffset(exp, temp) +
", id " + expectedID +
", for " + id +
", got offset of " + offset +
", id " + itsID);
}
}
delete zone;
}
}
void
TimeZoneTest::TestAliasedNames()
{
struct {
const char *from;
const char *to;
} kData[] = {
/* Generated by org.unicode.cldr.tool.CountItems */
/* zoneID, canonical zoneID */
{"Africa/Asmara", "Africa/Addis_Ababa"},
{"Africa/Timbuktu", "Africa/Abidjan"},
{"America/Argentina/Buenos_Aires", "America/Buenos_Aires"},
{"America/Argentina/Catamarca", "America/Catamarca"},
{"America/Argentina/ComodRivadavia", "America/Catamarca"},
{"America/Argentina/Cordoba", "America/Cordoba"},
{"America/Argentina/Jujuy", "America/Jujuy"},
{"America/Argentina/Mendoza", "America/Mendoza"},
{"America/Atka", "America/Adak"},
{"America/Ensenada", "America/Tijuana"},
{"America/Fort_Wayne", "America/Indianapolis"},
{"America/Indiana/Indianapolis", "America/Indianapolis"},
{"America/Kentucky/Louisville", "America/Louisville"},
{"America/Knox_IN", "America/Indiana/Knox"},
{"America/Porto_Acre", "America/Rio_Branco"},
{"America/Rosario", "America/Cordoba"},
{"America/Shiprock", "America/Denver"},
{"America/Virgin", "America/Anguilla"},
{"Antarctica/South_Pole", "Antarctica/McMurdo"},
{"Asia/Ashkhabad", "Asia/Ashgabat"},
{"Asia/Chongqing", "Asia/Shanghai"},
{"Asia/Chungking", "Asia/Shanghai"},
{"Asia/Dacca", "Asia/Dhaka"},
{"Asia/Harbin", "Asia/Shanghai"},
{"Asia/Ho_Chi_Minh", "Asia/Saigon"},
{"Asia/Istanbul", "Europe/Istanbul"},
{"Asia/Kashgar", "Asia/Urumqi"},
{"Asia/Kathmandu", "Asia/Katmandu"},
{"Asia/Kolkata", "Asia/Calcutta"},
{"Asia/Macao", "Asia/Macau"},
{"Asia/Tel_Aviv", "Asia/Jerusalem"},
{"Asia/Thimbu", "Asia/Thimphu"},
{"Asia/Ujung_Pandang", "Asia/Makassar"},
{"Asia/Ulan_Bator", "Asia/Ulaanbaatar"},
{"Atlantic/Faroe", "Atlantic/Faeroe"},
{"Atlantic/Jan_Mayen", "Arctic/Longyearbyen"},
{"Australia/ACT", "Australia/Sydney"},
{"Australia/Canberra", "Australia/Sydney"},
{"Australia/LHI", "Australia/Lord_Howe"},
{"Australia/NSW", "Australia/Sydney"},
{"Australia/North", "Australia/Darwin"},
{"Australia/Queensland", "Australia/Brisbane"},
{"Australia/South", "Australia/Adelaide"},
{"Australia/Tasmania", "Australia/Hobart"},
{"Australia/Victoria", "Australia/Melbourne"},
{"Australia/West", "Australia/Perth"},
{"Australia/Yancowinna", "Australia/Broken_Hill"},
{"Brazil/Acre", "America/Rio_Branco"},
{"Brazil/DeNoronha", "America/Noronha"},
{"Brazil/East", "America/Sao_Paulo"},
{"Brazil/West", "America/Manaus"},
{"Canada/Atlantic", "America/Halifax"},
{"Canada/Central", "America/Winnipeg"},
{"Canada/East-Saskatchewan", "America/Regina"},
{"Canada/Eastern", "America/Toronto"},
{"Canada/Mountain", "America/Edmonton"},
{"Canada/Newfoundland", "America/St_Johns"},
{"Canada/Pacific", "America/Vancouver"},
{"Canada/Saskatchewan", "America/Regina"},
{"Canada/Yukon", "America/Whitehorse"},
{"Chile/Continental", "America/Santiago"},
{"Chile/EasterIsland", "Pacific/Easter"},
{"Cuba", "America/Havana"},
{"Egypt", "Africa/Cairo"},
{"Eire", "Europe/Dublin"},
{"Etc/GMT+0", "Etc/GMT"},
{"Etc/GMT-0", "Etc/GMT"},
{"Etc/GMT0", "Etc/GMT"},
{"Etc/Greenwich", "Etc/GMT"},
{"Etc/UCT", "Etc/UTC"},
{"Etc/Universal", "Etc/UTC"},
{"Etc/Zulu", "Etc/UTC"},
{"Europe/Belfast", "Europe/London"},
{"Europe/Nicosia", "Asia/Nicosia"},
{"Europe/Tiraspol", "Europe/Chisinau"},
{"GB", "Europe/London"},
{"GB-Eire", "Europe/London"},
{"GMT", "Etc/GMT"},
{"GMT+0", "Etc/GMT"},
{"GMT-0", "Etc/GMT"},
{"GMT0", "Etc/GMT"},
{"Greenwich", "Etc/GMT"},
{"Hongkong", "Asia/Hong_Kong"},
{"Iceland", "Atlantic/Reykjavik"},
{"Iran", "Asia/Tehran"},
{"Israel", "Asia/Jerusalem"},
{"Jamaica", "America/Jamaica"},
{"Japan", "Asia/Tokyo"},
{"Kwajalein", "Pacific/Kwajalein"},
{"Libya", "Africa/Tripoli"},
{"Mexico/BajaNorte", "America/Tijuana"},
{"Mexico/BajaSur", "America/Mazatlan"},
{"Mexico/General", "America/Mexico_City"},
{"NZ", "Antarctica/McMurdo"},
{"NZ-CHAT", "Pacific/Chatham"},
{"Navajo", "America/Denver"},
{"PRC", "Asia/Shanghai"},
{"Pacific/Chuuk", "Pacific/Truk"},
{"Pacific/Pohnpei", "Pacific/Ponape"},
{"Pacific/Samoa", "Pacific/Midway"},
{"Pacific/Yap", "Pacific/Truk"},
{"Poland", "Europe/Warsaw"},
{"Portugal", "Europe/Lisbon"},
{"ROC", "Asia/Taipei"},
{"ROK", "Asia/Seoul"},
{"Singapore", "Asia/Singapore"},
{"SystemV/AST4", "Etc/GMT+4"},
{"SystemV/CST6", "Etc/GMT+6"},
{"SystemV/EST5", "Etc/GMT+5"},
{"SystemV/HST10", "Etc/GMT+10"},
{"SystemV/MST7", "Etc/GMT+7"},
{"SystemV/PST8", "Etc/GMT+8"},
{"SystemV/YST9", "Etc/GMT+9"},
{"Turkey", "Europe/Istanbul"},
{"UCT", "Etc/UTC"},
{"US/Alaska", "America/Anchorage"},
{"US/Aleutian", "America/Adak"},
{"US/Arizona", "America/Phoenix"},
{"US/Central", "America/Chicago"},
{"US/East-Indiana", "America/Indianapolis"},
{"US/Eastern", "America/New_York"},
{"US/Hawaii", "Pacific/Honolulu"},
{"US/Indiana-Starke", "America/Indiana/Knox"},
{"US/Michigan", "America/Detroit"},
{"US/Mountain", "America/Denver"},
{"US/Pacific", "America/Los_Angeles"},
{"US/Pacific-New", "America/Los_Angeles"},
{"US/Samoa", "Pacific/Midway"},
{"UTC", "Etc/UTC"},
{"Universal", "Etc/UTC"},
{"W-SU", "Europe/Moscow"},
{"Zulu", "Etc/UTC"},
/* Total: 136 */
};
TimeZone::EDisplayType styles[] = { TimeZone::SHORT, TimeZone::LONG };
UBool useDst[] = { false, true };
int32_t noLoc = uloc_countAvailable();
int32_t i, j, k, loc;
UnicodeString fromName, toName;
TimeZone *from = nullptr, *to = nullptr;
for(i = 0; i < UPRV_LENGTHOF(kData); i++) {
from = TimeZone::createTimeZone(kData[i].from);
to = TimeZone::createTimeZone(kData[i].to);
if(!from->hasSameRules(*to)) {
errln("different at %i\n", i);
}
if(!quick) {
for(loc = 0; loc < noLoc; loc++) {
const char* locale = uloc_getAvailable(loc);
for(j = 0; j < UPRV_LENGTHOF(styles); j++) {
for(k = 0; k < UPRV_LENGTHOF(useDst); k++) {
fromName.remove();
toName.remove();
from->getDisplayName(useDst[k], styles[j],locale, fromName);
to->getDisplayName(useDst[k], styles[j], locale, toName);
if(fromName.compare(toName) != 0) {
errln("Fail: Expected "+toName+" but got " + prettify(fromName)
+ " for locale: " + locale + " index: "+ loc
+ " to id "+ kData[i].to
+ " from id " + kData[i].from);
}
}
}
}
} else {
fromName.remove();
toName.remove();
from->getDisplayName(fromName);
to->getDisplayName(toName);
if(fromName.compare(toName) != 0) {
errln("Fail: Expected " + toName + " but got " + fromName + " for index: "+ i);
}
}
delete from;
delete to;
}
}
/**
* Test the basic functionality of the getDisplayName() API.
*
* @bug 4112869
* @bug 4028006
*
* See also API change request A41.
*
* 4/21/98 - make smarter, so the test works if the ext resources
* are present or not.
*/
void
TimeZoneTest::TestDisplayName()
{
UErrorCode status = U_ZERO_ERROR;
int32_t i;
TimeZone *zone = TimeZone::createTimeZone("PST");
UnicodeString name;
zone->getDisplayName(Locale::getEnglish(), name);
logln("PST->" + name);
if (name.compare("Pacific Standard Time") != 0)
dataerrln("Fail: Expected \"Pacific Standard Time\" but got " + name);
//*****************************************************************
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
// THE FOLLOWING LINES MUST BE UPDATED IF THE LOCALE DATA CHANGES
//*****************************************************************
struct
{
UBool useDst;
TimeZone::EDisplayType style;
const char *expect;
} kData[] = {
{false, TimeZone::SHORT, "PST"},
{true, TimeZone::SHORT, "PDT"},
{false, TimeZone::LONG, "Pacific Standard Time"},
{true, TimeZone::LONG, "Pacific Daylight Time"},
{false, TimeZone::SHORT_GENERIC, "PT"},
{true, TimeZone::SHORT_GENERIC, "PT"},
{false, TimeZone::LONG_GENERIC, "Pacific Time"},
{true, TimeZone::LONG_GENERIC, "Pacific Time"},
{false, TimeZone::SHORT_GMT, "-0800"},
{true, TimeZone::SHORT_GMT, "-0700"},
{false, TimeZone::LONG_GMT, "GMT-08:00"},
{true, TimeZone::LONG_GMT, "GMT-07:00"},
{false, TimeZone::SHORT_COMMONLY_USED, "PST"},
{true, TimeZone::SHORT_COMMONLY_USED, "PDT"},
{false, TimeZone::GENERIC_LOCATION, "Los Angeles Time"},
{true, TimeZone::GENERIC_LOCATION, "Los Angeles Time"},
{false, TimeZone::LONG, ""}
};
for (i=0; kData[i].expect[0] != '\0'; i++)
{
name.remove();
name = zone->getDisplayName(kData[i].useDst,
kData[i].style,
Locale::getEnglish(), name);
if (name.compare(kData[i].expect) != 0)
dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
logln("PST [with options]->" + name);
}
for (i=0; kData[i].expect[0] != '\0'; i++)
{
name.remove();
name = zone->getDisplayName(kData[i].useDst,
kData[i].style, name);
if (name.compare(kData[i].expect) != 0)
dataerrln("Fail: Expected " + UnicodeString(kData[i].expect) + "; got " + name);
logln("PST [with options]->" + name);
}
// Make sure that we don't display the DST name by constructing a fake
// PST zone that has DST all year long.
SimpleTimeZone *zone2 = new SimpleTimeZone(0, "PST");
zone2->setStartRule(UCAL_JANUARY, 1, 0, 0, status);
zone2->setEndRule(UCAL_DECEMBER, 31, 0, 0, status);
UnicodeString inDaylight;
if (zone2->inDaylightTime(static_cast<UDate>(0), status)) {
inDaylight = UnicodeString("true");
} else {
inDaylight = UnicodeString("false");
}
logln(UnicodeString("Modified PST inDaylightTime->") + inDaylight );
if(U_FAILURE(status))
{
dataerrln("Some sort of error..." + UnicodeString(u_errorName(status))); // REVISIT
}
name.remove();
name = zone2->getDisplayName(Locale::getEnglish(),name);
logln("Modified PST->" + name);
if (name.compare("Pacific Standard Time") != 0)
dataerrln("Fail: Expected \"Pacific Standard Time\"");
// Make sure we get the default display format for Locales
// with no display name data.
Locale mt_MT("mt_MT");
name.remove();
name = zone->getDisplayName(mt_MT,name);
//*****************************************************************
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
// THE FOLLOWING LINE MUST BE UPDATED IF THE LOCALE DATA CHANGES
//*****************************************************************
logln("PST(mt_MT)->" + name);
// *** REVISIT SRL how in the world do I check this? looks java specific.
// Now be smart -- check to see if zh resource is even present.
// If not, we expect the en fallback behavior.
ResourceBundle enRB(nullptr,
Locale::getEnglish(), status);
if(U_FAILURE(status))
dataerrln("Couldn't get ResourceBundle for en - %s", u_errorName(status));
ResourceBundle mtRB(nullptr,
mt_MT, status);
//if(U_FAILURE(status))
// errln("Couldn't get ResourceBundle for mt_MT");
UBool noZH = U_FAILURE(status);
if (noZH) {
logln("Warning: Not testing the mt_MT behavior because resource is absent");
if (name != "Pacific Standard Time")
dataerrln("Fail: Expected Pacific Standard Time");
}
if (name.compare("GMT-08:00") &&
name.compare("GMT-8:00") &&
name.compare("GMT-0800") &&
name.compare("GMT-800")) {
dataerrln(UnicodeString("Fail: Expected GMT-08:00 or something similar for PST in mt_MT but got ") + name );
dataerrln("************************************************************");
dataerrln("THE ABOVE FAILURE MAY JUST MEAN THE LOCALE DATA HAS CHANGED");
dataerrln("************************************************************");
}
// Now try a non-existent zone
delete zone2;
zone2 = new SimpleTimeZone(90*60*1000, "xyzzy");
name.remove();
name = zone2->getDisplayName(Locale::getEnglish(),name);
logln("GMT+90min->" + name);
if (name.compare("GMT+01:30") &&
name.compare("GMT+1:30") &&
name.compare("GMT+0130") &&
name.compare("GMT+130"))
dataerrln("Fail: Expected GMT+01:30 or something similar");
name.truncate(0);
zone2->getDisplayName(name);
logln("GMT+90min->" + name);
if (name.compare("GMT+01:30") &&
name.compare("GMT+1:30") &&
name.compare("GMT+0130") &&
name.compare("GMT+130"))
dataerrln("Fail: Expected GMT+01:30 or something similar");
// clean up
delete zone;
delete zone2;
}
/**
* @bug 4107276
*/
void
TimeZoneTest::TestDSTSavings()
{
UErrorCode status = U_ZERO_ERROR;
// It might be better to find a way to integrate this test into the main TimeZone
// tests above, but I don't have time to figure out how to do this (or if it's
// even really a good idea). Let's consider that a future. --rtg 1/27/98
SimpleTimeZone *tz = new SimpleTimeZone(-5 * U_MILLIS_PER_HOUR, "dstSavingsTest",
UCAL_MARCH, 1, 0, 0, UCAL_SEPTEMBER, 1, 0, 0,
static_cast<int32_t>(0.5 * U_MILLIS_PER_HOUR), status);
if(U_FAILURE(status))
errln("couldn't create TimeZone");
if (tz->getRawOffset() != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("Got back a raw offset of ") + (tz->getRawOffset() / U_MILLIS_PER_HOUR) +
" hours instead of -5 hours.");
if (!tz->useDaylightTime())
errln("Test time zone should use DST but claims it doesn't.");
if (tz->getDSTSavings() != 0.5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("Set DST offset to 0.5 hour, but got back ") + (tz->getDSTSavings() /
U_MILLIS_PER_HOUR) + " hours instead.");
int32_t offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JANUARY, 1,
UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JUNE, 1, UCAL_MONDAY,
10 * U_MILLIS_PER_HOUR,status);
if (offset != -4.5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
tz->setDSTSavings(U_MILLIS_PER_HOUR, status);
offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JANUARY, 1,
UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz->getOffset(GregorianCalendar::AD, 1998, UCAL_JUNE, 1, UCAL_MONDAY,
10 * U_MILLIS_PER_HOUR,status);
if (offset != -4 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
delete tz;
}
/**
* @bug 4107570
*/
void
TimeZoneTest::TestAlternateRules()
{
// Like TestDSTSavings, this test should probably be integrated somehow with the main
// test at the top of this class, but I didn't have time to figure out how to do that.
// --rtg 1/28/98
SimpleTimeZone tz(-5 * U_MILLIS_PER_HOUR, "alternateRuleTest");
// test the day-of-month API
UErrorCode status = U_ZERO_ERROR;
tz.setStartRule(UCAL_MARCH, 10, 12 * U_MILLIS_PER_HOUR, status);
if(U_FAILURE(status))
errln("tz.setStartRule failed");
tz.setEndRule(UCAL_OCTOBER, 20, 12 * U_MILLIS_PER_HOUR, status);
if(U_FAILURE(status))
errln("tz.setStartRule failed");
int32_t offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 5,
UCAL_THURSDAY, 10 * U_MILLIS_PER_HOUR,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 3/5/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 15,
UCAL_SUNDAY, 10 * millisPerHour,status);
if (offset != -4 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 3/15/98 should have been -4 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 15,
UCAL_THURSDAY, 10 * millisPerHour,status);
if (offset != -4 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ") + (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 25,
UCAL_SUNDAY, 10 * millisPerHour,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 10/25/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
// test the day-of-week-after-day-in-month API
tz.setStartRule(UCAL_MARCH, 10, UCAL_FRIDAY, 12 * millisPerHour, true, status);
if(U_FAILURE(status))
errln("tz.setStartRule failed");
tz.setEndRule(UCAL_OCTOBER, 20, UCAL_FRIDAY, 12 * millisPerHour, false, status);
if(U_FAILURE(status))
errln("tz.setStartRule failed");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 11,
UCAL_WEDNESDAY, 10 * millisPerHour,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 3/11/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_MARCH, 14,
UCAL_SATURDAY, 10 * millisPerHour,status);
if (offset != -4 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 3/14/98 should have been -4 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 15,
UCAL_THURSDAY, 10 * millisPerHour,status);
if (offset != -4 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 10/15/98 should have been -4 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
offset = tz.getOffset(GregorianCalendar::AD, 1998, UCAL_OCTOBER, 17,
UCAL_SATURDAY, 10 * millisPerHour,status);
if (offset != -5 * U_MILLIS_PER_HOUR)
errln(UnicodeString("The offset for 10AM, 10/17/98 should have been -5 hours, but we got ")
+ (offset / U_MILLIS_PER_HOUR) + " hours.");
}
void TimeZoneTest::TestFractionalDST() {
const char* tzName = "Australia/Lord_Howe"; // 30 min offset
TimeZone* tz_icu = TimeZone::createTimeZone(tzName);
int dst_icu = tz_icu->getDSTSavings();
UnicodeString id;
int32_t expected = 1800000;
if (expected != dst_icu) {
dataerrln(UnicodeString("java reports dst savings of ") + expected +
" but icu reports " + dst_icu +
" for tz " + tz_icu->getID(id));
} else {
logln(UnicodeString("both java and icu report dst savings of ") + expected + " for tz " + tz_icu->getID(id));
}
delete tz_icu;
}
/**
* Test country code support. Jitterbug 776.
*/
void TimeZoneTest::TestCountries() {
// Make sure America/Los_Angeles is in the "US" group, and
// Asia/Tokyo isn't. Vice versa for the "JP" group.
UErrorCode ec = U_ZERO_ERROR;
int32_t n;
StringEnumeration* s = TimeZone::createEnumerationForRegion("US", ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration for US");
return;
}
n = s->count(ec);
UBool la = false, tokyo = false;
UnicodeString laZone("America/Los_Angeles", "");
UnicodeString tokyoZone("Asia/Tokyo", "");
int32_t i;
if (n <= 0) {
dataerrln("FAIL: TimeZone::createEnumeration() returned nothing");
return;
}
for (i=0; i<n; ++i) {
const UnicodeString* id = s->snext(ec);
if (*id == (laZone)) {
la = true;
}
if (*id == (tokyoZone)) {
tokyo = true;
}
}
if (!la || tokyo) {
errln("FAIL: " + laZone + " in US = " + la);
errln("FAIL: " + tokyoZone + " in US = " + tokyo);
}
delete s;
s = TimeZone::createEnumerationForRegion("JP", ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration for JP");
return;
}
n = s->count(ec);
la = false; tokyo = false;
for (i=0; i<n; ++i) {
const UnicodeString* id = s->snext(ec);
if (*id == (laZone)) {
la = true;
}
if (*id == (tokyoZone)) {
tokyo = true;
}
}
if (la || !tokyo) {
errln("FAIL: " + laZone + " in JP = " + la);
errln("FAIL: " + tokyoZone + " in JP = " + tokyo);
}
StringEnumeration* s1 = TimeZone::createEnumerationForRegion("US", ec);
StringEnumeration* s2 = TimeZone::createEnumerationForRegion("US", ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration for US");
return;
}
for(i=0;i<n;++i){
const UnicodeString* id1 = s1->snext(ec);
if(id1==nullptr || U_FAILURE(ec)){
errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
}
TimeZone* tz1 = TimeZone::createTimeZone(*id1);
for(int j=0; j<n;++j){
const UnicodeString* id2 = s2->snext(ec);
if(id2==nullptr || U_FAILURE(ec)){
errln("Failed to fetch next from TimeZone enumeration. Length returned : %i Current Index: %i", n,i);
}
TimeZone* tz2 = TimeZone::createTimeZone(*id2);
if(tz1->hasSameRules(*tz2)){
logln("ID1 : " + *id1+" == ID2 : " +*id2);
}
delete tz2;
}
delete tz1;
}
delete s1;
delete s2;
delete s;
}
void TimeZoneTest::TestHistorical() {
const int32_t H = U_MILLIS_PER_HOUR;
struct {
const char* id;
int32_t time; // epoch seconds
int32_t offset; // total offset (millis)
} DATA[] = {
// Add transition points (before/after) as desired to test historical
// behavior.
{"America/Los_Angeles", 638963999, -8*H}, // Sun Apr 01 01:59:59 GMT-08:00 1990
{"America/Los_Angeles", 638964000, -7*H}, // Sun Apr 01 03:00:00 GMT-07:00 1990
{"America/Los_Angeles", 657104399, -7*H}, // Sun Oct 28 01:59:59 GMT-07:00 1990
{"America/Los_Angeles", 657104400, -8*H}, // Sun Oct 28 01:00:00 GMT-08:00 1990
{"America/Goose_Bay", -116445601, -4*H}, // Sun Apr 24 01:59:59 GMT-04:00 1966
{"America/Goose_Bay", -116445600, -3*H}, // Sun Apr 24 03:00:00 GMT-03:00 1966
{"America/Goose_Bay", -100119601, -3*H}, // Sun Oct 30 01:59:59 GMT-03:00 1966
{"America/Goose_Bay", -100119600, -4*H}, // Sun Oct 30 01:00:00 GMT-04:00 1966
{"America/Goose_Bay", -84391201, -4*H}, // Sun Apr 30 01:59:59 GMT-04:00 1967
{"America/Goose_Bay", -84391200, -3*H}, // Sun Apr 30 03:00:00 GMT-03:00 1967
{"America/Goose_Bay", -68670001, -3*H}, // Sun Oct 29 01:59:59 GMT-03:00 1967
{"America/Goose_Bay", -68670000, -4*H}, // Sun Oct 29 01:00:00 GMT-04:00 1967
{nullptr, 0, 0}
};
for (int32_t i = 0; DATA[i].id != nullptr; ++i) {
const char* id = DATA[i].id;
TimeZone *tz = TimeZone::createTimeZone(id);
UnicodeString s;
if (tz == nullptr) {
errln("FAIL: Cannot create %s", id);
} else if (tz->getID(s) != UnicodeString(id)) {
dataerrln(UnicodeString("FAIL: createTimeZone(") + id + ") => " + s);
} else {
UErrorCode ec = U_ZERO_ERROR;
int32_t raw, dst;
UDate when = static_cast<double>(DATA[i].time) * U_MILLIS_PER_SECOND;
tz->getOffset(when, false, raw, dst, ec);
if (U_FAILURE(ec)) {
errln("FAIL: getOffset");
} else if ((raw+dst) != DATA[i].offset) {
errln(UnicodeString("FAIL: ") + DATA[i].id + ".getOffset(" +
//when + " = " +
dateToString(when) + ") => " +
raw + ", " + dst);
} else {
logln(UnicodeString("Ok: ") + DATA[i].id + ".getOffset(" +
//when + " = " +
dateToString(when) + ") => " +
raw + ", " + dst);
}
}
delete tz;
}
}
void TimeZoneTest::TestEquivalentIDs() {
int32_t n = TimeZone::countEquivalentIDs("PST");
if (n < 2) {
dataerrln(UnicodeString("FAIL: countEquivalentIDs(PST) = ") + n);
} else {
UBool sawLA = false;
for (int32_t i=0; i<n; ++i) {
UnicodeString id = TimeZone::getEquivalentID("PST", i);
logln(UnicodeString("") + i + " : " + id);
if (id == UnicodeString("America/Los_Angeles")) {
sawLA = true;
}
}
if (!sawLA) {
errln("FAIL: America/Los_Angeles should be in the list");
}
}
}
// Test that a transition at the end of February is handled correctly.
void TimeZoneTest::TestFebruary() {
UErrorCode status = U_ZERO_ERROR;
// Time zone with daylight savings time from the first Sunday in November
// to the last Sunday in February.
// Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
//
// Note: In tzdata2007h, the rule had changed, so no actual zones uses
// lastSun in Feb anymore.
SimpleTimeZone tz1(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
UNICODE_STRING("nov-feb", 7),
UCAL_NOVEMBER, 1, UCAL_SUNDAY, // start: November, first, Sunday
0, // midnight wall time
UCAL_FEBRUARY, -1, UCAL_SUNDAY, // end: February, last, Sunday
0, // midnight wall time
status);
if (U_FAILURE(status)) {
errln("Unable to create the SimpleTimeZone(nov-feb): %s", u_errorName(status));
return;
}
// Now hardcode the same rules as for Brazil in tzdata 2006n, so that
// we cover the intended code even when in the future zoneinfo hardcodes
// these transition dates.
SimpleTimeZone tz2(-3 * U_MILLIS_PER_HOUR, // raw offset: 3h before (west of) GMT
UNICODE_STRING("nov-feb2", 8),
UCAL_NOVEMBER, 1, -UCAL_SUNDAY, // start: November, 1 or after, Sunday
0, // midnight wall time
UCAL_FEBRUARY, -29, -UCAL_SUNDAY,// end: February, 29 or before, Sunday
0, // midnight wall time
status);
if (U_FAILURE(status)) {
errln("Unable to create the SimpleTimeZone(nov-feb2): %s", u_errorName(status));
return;
}
// Gregorian calendar with the UTC time zone for getting sample test date/times.
GregorianCalendar gc(*TimeZone::getGMT(), status);
if (U_FAILURE(status)) {
dataerrln("Unable to create the UTC calendar: %s", u_errorName(status));
return;
}
struct {
// UTC time.
int32_t year, month, day, hour, minute, second;
// Expected time zone offset in hours after GMT (negative=before GMT).
int32_t offsetHours;
} data[] = {
{ 2006, UCAL_NOVEMBER, 5, 02, 59, 59, -3 },
{ 2006, UCAL_NOVEMBER, 5, 03, 00, 00, -2 },
{ 2007, UCAL_FEBRUARY, 25, 01, 59, 59, -2 },
{ 2007, UCAL_FEBRUARY, 25, 02, 00, 00, -3 },
{ 2007, UCAL_NOVEMBER, 4, 02, 59, 59, -3 },
{ 2007, UCAL_NOVEMBER, 4, 03, 00, 00, -2 },
{ 2008, UCAL_FEBRUARY, 24, 01, 59, 59, -2 },
{ 2008, UCAL_FEBRUARY, 24, 02, 00, 00, -3 },
{ 2008, UCAL_NOVEMBER, 2, 02, 59, 59, -3 },
{ 2008, UCAL_NOVEMBER, 2, 03, 00, 00, -2 },
{ 2009, UCAL_FEBRUARY, 22, 01, 59, 59, -2 },
{ 2009, UCAL_FEBRUARY, 22, 02, 00, 00, -3 },
{ 2009, UCAL_NOVEMBER, 1, 02, 59, 59, -3 },
{ 2009, UCAL_NOVEMBER, 1, 03, 00, 00, -2 },
{ 2010, UCAL_FEBRUARY, 28, 01, 59, 59, -2 },
{ 2010, UCAL_FEBRUARY, 28, 02, 00, 00, -3 }
};
TimeZone *timezones[] = { &tz1, &tz2 };
TimeZone *tz;
UDate dt;
int32_t t, i, raw, dst;
for (t = 0; t < UPRV_LENGTHOF(timezones); ++t) {
tz = timezones[t];
for (i = 0; i < UPRV_LENGTHOF(data); ++i) {
gc.set(data[i].year, data[i].month, data[i].day,
data[i].hour, data[i].minute, data[i].second);
dt = gc.getTime(status);
if (U_FAILURE(status)) {
errln("test case %d.%d: bad date/time %04d-%02d-%02d %02d:%02d:%02d",
t, i,
data[i].year, data[i].month + 1, data[i].day,
data[i].hour, data[i].minute, data[i].second);
status = U_ZERO_ERROR;
continue;
}
tz->getOffset(dt, false, raw, dst, status);
if (U_FAILURE(status)) {
errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) fails: %s",
t, i,
data[i].year, data[i].month + 1, data[i].day,
data[i].hour, data[i].minute, data[i].second,
u_errorName(status));
status = U_ZERO_ERROR;
} else if ((raw + dst) != data[i].offsetHours * U_MILLIS_PER_HOUR) {
errln("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d",
t, i,
data[i].year, data[i].month + 1, data[i].day,
data[i].hour, data[i].minute, data[i].second,
raw, dst, data[i].offsetHours * U_MILLIS_PER_HOUR);
}
}
}
}
void TimeZoneTest::TestCanonicalIDAPI() {
// Bogus input string.
UnicodeString bogus;
bogus.setToBogus();
UnicodeString canonicalID;
UErrorCode ec = U_ZERO_ERROR;
UnicodeString *pResult = &TimeZone::getCanonicalID(bogus, canonicalID, ec);
assertEquals("TimeZone::getCanonicalID(bogus) should fail", U_ILLEGAL_ARGUMENT_ERROR, ec);
assertTrue("TimeZone::getCanonicalID(bogus) should return the dest string", pResult == &canonicalID);
// U_FAILURE on input.
UnicodeString berlin("Europe/Berlin");
ec = U_MEMORY_ALLOCATION_ERROR;
pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
assertEquals("TimeZone::getCanonicalID(failure) should fail", U_MEMORY_ALLOCATION_ERROR, ec);
assertTrue("TimeZone::getCanonicalID(failure) should return the dest string", pResult == &canonicalID);
// Valid input should un-bogus the dest string.
canonicalID.setToBogus();
ec = U_ZERO_ERROR;
pResult = &TimeZone::getCanonicalID(berlin, canonicalID, ec);
assertSuccess("TimeZone::getCanonicalID(bogus dest) should succeed", ec, true);
assertTrue("TimeZone::getCanonicalID(bogus dest) should return the dest string", pResult == &canonicalID);
assertFalse("TimeZone::getCanonicalID(bogus dest) should un-bogus the dest string", canonicalID.isBogus());
assertEquals("TimeZone::getCanonicalID(bogus dest) unexpected result", canonicalID, berlin, true);
}
void TimeZoneTest::TestCanonicalID() {
// Olson (IANA) tzdata used to have very few "Link"s long time ago.
// This test case was written when most of CLDR canonical time zones are
// defined as independent "Zone" in the TZ database.
// Since then, the TZ maintainer found some historic rules in mid 20th century
// were not really reliable, and many zones are now sharing rules.
// As of TZ database release 2022a, there are quite a lot of zones defined
// by "Link" to another zone, so the exception table below becomes really
// big. It might be still useful to make sure CLDR zone aliases are consistent
// with zone rules.
static const struct {
const char *alias; // link-from
const char *zone; // link-to (A zone ID with "Zone" rule)
} excluded1[] = {
{"Africa/Accra", "Africa/Abidjan"},
{"Africa/Addis_Ababa", "Africa/Nairobi"},
{"Africa/Asmera", "Africa/Nairobi"},
{"Africa/Bamako", "Africa/Abidjan"},
{"Africa/Bangui", "Africa/Lagos"},
{"Africa/Banjul", "Africa/Abidjan"},
{"Africa/Blantyre", "Africa/Maputo"},
{"Africa/Brazzaville", "Africa/Lagos"},
{"Africa/Bujumbura", "Africa/Maputo"},
{"Africa/Conakry", "Africa/Abidjan"},
{"Africa/Dakar", "Africa/Abidjan"},
{"Africa/Dar_es_Salaam", "Africa/Nairobi"},
{"Africa/Djibouti", "Africa/Nairobi"},
{"Africa/Douala", "Africa/Lagos"},
{"Africa/Freetown", "Africa/Abidjan"},
{"Africa/Gaborone", "Africa/Maputo"},
{"Africa/Harare", "Africa/Maputo"},
{"Africa/Kampala", "Africa/Nairobi"},
{"Africa/Khartoum", "Africa/Juba"},
{"Africa/Kigali", "Africa/Maputo"},
{"Africa/Kinshasa", "Africa/Lagos"},
{"Africa/Libreville", "Africa/Lagos"},
{"Africa/Lome", "Africa/Abidjan"},
{"Africa/Luanda", "Africa/Lagos"},
{"Africa/Lubumbashi", "Africa/Maputo"},
{"Africa/Lusaka", "Africa/Maputo"},
{"Africa/Maseru", "Africa/Johannesburg"},
{"Africa/Malabo", "Africa/Lagos"},
{"Africa/Mbabane", "Africa/Johannesburg"},
{"Africa/Mogadishu", "Africa/Nairobi"},
{"Africa/Niamey", "Africa/Lagos"},
{"Africa/Nouakchott", "Africa/Abidjan"},
{"Africa/Ouagadougou", "Africa/Abidjan"},
{"Africa/Porto-Novo", "Africa/Lagos"},
{"Africa/Sao_Tome", "Africa/Abidjan"},
{"America/Antigua", "America/Puerto_Rico"},
{"America/Anguilla", "America/Puerto_Rico"},
{"America/Aruba", "America/Puerto_Rico"},
{"America/Atikokan", "America/Panama"},
{"America/Blanc-Sablon", "America/Puerto_Rico"},
{"America/Cayman", "America/Panama"},
{"America/Coral_Harbour", "America/Panama"},
{"America/Creston", "America/Phoenix"},
{"America/Curacao", "America/Puerto_Rico"},
{"America/Dominica", "America/Puerto_Rico"},
{"America/Grenada", "America/Puerto_Rico"},
{"America/Guadeloupe", "America/Puerto_Rico"},
{"America/Kralendijk", "America/Puerto_Rico"},
{"America/Lower_Princes", "America/Puerto_Rico"},
{"America/Marigot", "America/Puerto_Rico"},
{"America/Montreal", "America/Toronto"},
{"America/Montserrat", "America/Puerto_Rico"},
{"America/Nassau", "America/Toronto"},
{"America/Nipigon", "America/Toronto"},
{"America/Pangnirtung", "America/Iqaluit"},
{"America/Port_of_Spain", "America/Puerto_Rico"},
{"America/Rainy_River", "America/Winnipeg"},
{"America/Santa_Isabel", "America/Tijuana"},
{"America/Shiprock", "America/Denver"},
{"America/St_Barthelemy", "America/Puerto_Rico"},
{"America/St_Kitts", "America/Puerto_Rico"},
{"America/St_Lucia", "America/Puerto_Rico"},
{"America/St_Thomas", "America/Puerto_Rico"},
{"America/St_Vincent", "America/Puerto_Rico"},
{"America/Thunder_Bay", "America/Toronto"},
{"America/Tortola", "America/Puerto_Rico"},
{"America/Virgin", "America/Puerto_Rico"},
{"America/Yellowknife", "America/Edmonton"},
{"Antarctica/DumontDUrville", "Pacific/Port_Moresby"},
{"Antarctica/South_Pole", "Antarctica/McMurdo"},
{"Antarctica/Syowa", "Asia/Riyadh"},
{"Arctic/Longyearbyen", "Europe/Berlin"},
{"Asia/Aden", "Asia/Riyadh"},
{"Asia/Brunei", "Asia/Kuching"},
{"Asia/Kuala_Lumpur", "Asia/Singapore"},
{"Asia/Kuwait", "Asia/Riyadh"},
{"Asia/Muscat", "Asia/Dubai"},
{"Asia/Phnom_Penh", "Asia/Bangkok"},
{"Asia/Qatar", "Asia/Bahrain"},
{"Asia/Vientiane", "Asia/Bangkok"},
{"Atlantic/Jan_Mayen", "Europe/Berlin"},
{"Atlantic/Reykjavik", "Africa/Abidjan"},
{"Atlantic/St_Helena", "Africa/Abidjan"},
{"Australia/Currie", "Australia/Hobart"},
{"Australia/Tasmania", "Australia/Hobart"},
{"Europe/Bratislava", "Europe/Prague"},
{"Europe/Brussels", "Europe/Amsterdam"},
{"Europe/Busingen", "Europe/Zurich"},
{"Europe/Copenhagen", "Europe/Berlin"},
{"Europe/Guernsey", "Europe/London"},
{"Europe/Isle_of_Man", "Europe/London"},
{"Europe/Jersey", "Europe/London"},
{"Europe/Ljubljana", "Europe/Belgrade"},
{"Europe/Luxembourg", "Europe/Amsterdam"},
{"Europe/Mariehamn", "Europe/Helsinki"},
{"Europe/Monaco", "Europe/Paris"},
{"Europe/Oslo", "Europe/Berlin"},
{"Europe/Podgorica", "Europe/Belgrade"},
{"Europe/San_Marino", "Europe/Rome"},
{"Europe/Sarajevo", "Europe/Belgrade"},
{"Europe/Skopje", "Europe/Belgrade"},
{"Europe/Stockholm", "Europe/Berlin"},
{"Europe/Uzhgorod", "Europe/Kiev"},
{"Europe/Vaduz", "Europe/Zurich"},
{"Europe/Vatican", "Europe/Rome"},
{"Europe/Zagreb", "Europe/Belgrade"},
{"Europe/Zaporozhye", "Europe/Kiev"},
{"Indian/Antananarivo", "Africa/Nairobi"},
{"Indian/Christmas", "Asia/Bangkok"},
{"Indian/Cocos", "Asia/Rangoon"},
{"Indian/Comoro", "Africa/Nairobi"},
{"Indian/Mahe", "Asia/Dubai"},
{"Indian/Maldives", "Indian/Kerguelen"},
{"Indian/Mayotte", "Africa/Nairobi"},
{"Indian/Reunion", "Asia/Dubai"},
{"Pacific/Auckland", "Antarctica/McMurdo"},
{"Pacific/Johnston", "Pacific/Honolulu"},
{"Pacific/Majuro", "Pacific/Funafuti"},
{"Pacific/Midway", "Pacific/Pago_Pago"},
{"Pacific/Ponape", "Pacific/Guadalcanal"},
{"Pacific/Saipan", "Pacific/Guam"},
{"Pacific/Tarawa", "Pacific/Funafuti"},
{"Pacific/Truk", "Pacific/Port_Moresby"},
{"Pacific/Wake", "Pacific/Funafuti"},
{"Pacific/Wallis", "Pacific/Funafuti"},
{nullptr, nullptr}
};
// Following IDs are aliases of Etc/GMT in CLDR,
// but Olson tzdata has 3 independent definitions
// for Etc/GMT, Etc/UTC, Etc/UCT.
// Until we merge them into one equivalent group
// in zoneinfo.res, we exclude them in the test
// below.
static const char* excluded2[] = {
"Etc/UCT", "UCT",
"Etc/UTC", "UTC",
"Etc/Universal", "Universal",
"Etc/Zulu", "Zulu", nullptr
};
// Walk through equivalency groups
UErrorCode ec = U_ZERO_ERROR;
int32_t s_length, i, j, k;
StringEnumeration* s = TimeZone::createEnumeration(ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to create TimeZone enumeration");
return;
}
UnicodeString canonicalID, tmpCanonical;
s_length = s->count(ec);
for (i = 0; i < s_length;++i) {
const UnicodeString *tzid = s->snext(ec);
int32_t nEquiv = TimeZone::countEquivalentIDs(*tzid);
if (nEquiv == 0) {
continue;
}
UBool bFoundCanonical = false;
// Make sure getCanonicalID returns the exact same result
// for all entries within a same equivalency group with some
// exceptions listed in exluded1.
// Also, one of them must be canonical id.
for (j = 0; j < nEquiv; j++) {
UnicodeString tmp = TimeZone::getEquivalentID(*tzid, j);
TimeZone::getCanonicalID(tmp, tmpCanonical, ec);
if (U_FAILURE(ec)) {
errln(UnicodeString("FAIL: getCanonicalID(") + tmp + ") failed.");
ec = U_ZERO_ERROR;
continue;
}
// Some exceptional cases
for (k = 0; excluded1[k].alias != nullptr; k++) {
if (tmpCanonical == excluded1[k].alias) {
tmpCanonical = excluded1[k].zone;
break;
}
}
if (j == 0) {
canonicalID = tmpCanonical;
} else if (canonicalID != tmpCanonical) {
errln("FAIL: getCanonicalID(" + tmp + ") returned " + tmpCanonical + " expected:" + canonicalID);
}
if (canonicalID == tmp) {
bFoundCanonical = true;
}
}
// At least one ID in an equvalency group must match the
// canonicalID
if (bFoundCanonical == false) {
// test exclusion because of differences between Olson tzdata and CLDR
UBool isExcluded = false;
for (k = 0; excluded2[k] != nullptr; k++) {
if (*tzid == UnicodeString(excluded2[k])) {
isExcluded = true;
break;
}
}
if (isExcluded) {
continue;
}
errln(UnicodeString("FAIL: No timezone ids match the canonical ID ") + canonicalID);
}
}
delete s;
// Testing some special cases
static const struct {
const char *id;
const char *expected;
UBool isSystem;
} data[] = {
{"GMT-03", "GMT-03:00", false},
{"GMT+4", "GMT+04:00", false},
{"GMT-055", "GMT-00:55", false},
{"GMT+430", "GMT+04:30", false},
{"GMT-12:15", "GMT-12:15", false},
{"GMT-091015", "GMT-09:10:15", false},
{"GMT+1:90", nullptr, false},
{"America/Argentina/Buenos_Aires", "America/Buenos_Aires", true},
{"Etc/Unknown", "Etc/Unknown", false},
{"bogus", nullptr, false},
{"", nullptr, false},
{"America/Marigot", "America/Marigot", true}, // Olson link, but CLDR canonical (#8953)
{"Europe/Bratislava", "Europe/Bratislava", true}, // Same as above
{nullptr, nullptr, false}
};
UBool isSystemID;
for (i = 0; data[i].id != nullptr; i++) {
TimeZone::getCanonicalID(UnicodeString(data[i].id), canonicalID, isSystemID, ec);
if (U_FAILURE(ec)) {
if (ec != U_ILLEGAL_ARGUMENT_ERROR || data[i].expected != nullptr) {
errln(UnicodeString("FAIL: getCanonicalID(\"") + data[i].id
+ "\") returned status U_ILLEGAL_ARGUMENT_ERROR");
}
ec = U_ZERO_ERROR;
continue;
}
if (canonicalID != data[i].expected) {
dataerrln(UnicodeString("FAIL: getCanonicalID(\"") + data[i].id
+ "\") returned " + canonicalID + " - expected: " + data[i].expected);
}
if (isSystemID != data[i].isSystem) {
dataerrln(UnicodeString("FAIL: getCanonicalID(\"") + data[i].id
+ "\") set " + isSystemID + " to isSystemID");
}
}
}
//
// Test Display Names, choosing zones and lcoales where there are multiple
// meta-zones defined.
//
static struct {
const char *zoneName;
const char *localeName;
UBool summerTime;
TimeZone::EDisplayType style;
const char *expectedDisplayName; }
zoneDisplayTestData [] = {
// zone id locale summer format expected display name
{"Europe/London", "en", false, TimeZone::SHORT, "GMT"},
{"Europe/London", "en", false, TimeZone::LONG, "Greenwich Mean Time"},
{"Europe/London", "en", true, TimeZone::SHORT, "GMT+1" /*"BST"*/},
{"Europe/London", "en", true, TimeZone::LONG, "British Summer Time"},
{"America/Anchorage", "en", false, TimeZone::SHORT, "AKST"},
{"America/Anchorage", "en", false, TimeZone::LONG, "Alaska Standard Time"},
{"America/Anchorage", "en", true, TimeZone::SHORT, "AKDT"},
{"America/Anchorage", "en", true, TimeZone::LONG, "Alaska Daylight Time"},
// Southern Hemisphere, all data from meta:Australia_Western
{"Australia/Perth", "en", false, TimeZone::SHORT, "GMT+8"/*"AWST"*/},
{"Australia/Perth", "en", false, TimeZone::LONG, "Australian Western Standard Time"},
// Note: Perth does not observe DST currently. When display name is missing,
// the localized GMT format with the current offset is used even daylight name was
// requested. See #9350.
{"Australia/Perth", "en", true, TimeZone::SHORT, "GMT+8"/*"AWDT"*/},
{"Australia/Perth", "en", true, TimeZone::LONG, "Australian Western Daylight Time"},
{"America/Sao_Paulo", "en", false, TimeZone::SHORT, "GMT-3"/*"BRT"*/},
{"America/Sao_Paulo", "en", false, TimeZone::LONG, "Brasilia Standard Time"},
// Per https://mm.icann.org/pipermail/tz-announce/2019-July/000056.html
// Brazil has canceled DST and will stay on standard time indefinitely.
// {"America/Sao_Paulo", "en", true, TimeZone::SHORT, "GMT-2"/*"BRST"*/},
// {"America/Sao_Paulo", "en", true, TimeZone::LONG, "Brasilia Summer Time"},
// No Summer Time, but had it before 1983.
{"Pacific/Honolulu", "en", false, TimeZone::SHORT, "HST"},
{"Pacific/Honolulu", "en", false, TimeZone::LONG, "Hawaii-Aleutian Standard Time"},
{"Pacific/Honolulu", "en", true, TimeZone::SHORT, "GMT-10"},
{"Pacific/Honolulu", "en", true, TimeZone::LONG, "GMT-10:00"},
// Northern, has Summer, not commonly used.
{"Europe/Helsinki", "en", false, TimeZone::SHORT, "GMT+2"/*"EET"*/},
{"Europe/Helsinki", "en", false, TimeZone::LONG, "Eastern European Standard Time"},
{"Europe/Helsinki", "en", true, TimeZone::SHORT, "GMT+3"/*"EEST"*/},
{"Europe/Helsinki", "en", true, TimeZone::LONG, "Eastern European Summer Time"},
// Repeating the test data for DST. The test data below trigger the problem reported
// by Ticket#6644
{"Europe/London", "en", false, TimeZone::SHORT, "GMT" /*"BST"*/},
{"Europe/London", "en", false, TimeZone::LONG, "Greenwich Mean Time"},
{"Europe/London", "en", true, TimeZone::SHORT, "GMT+1" /*"BST"*/},
{"Europe/London", "en", true, TimeZone::LONG, "British Summer Time"},
{"Europe/Dublin", "en", false, TimeZone::SHORT, "GMT" /*"IST"*/},
{"Europe/Dublin", "en", false, TimeZone::LONG, "Greenwich Mean Time"},
{"Europe/Dublin", "en", true, TimeZone::SHORT, "GMT+1" /*"IST"*/},
{"Europe/Dublin", "en", true, TimeZone::LONG, "Irish Standard Time"},
{nullptr, nullptr, false, TimeZone::SHORT, nullptr} // nullptr values terminate list
};
void TimeZoneTest::TestDisplayNamesMeta() {
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar cal(*TimeZone::getGMT(), status);
if (failure(status, "GregorianCalendar", true)) return;
UBool sawAnError = false;
for (int testNum = 0; zoneDisplayTestData[testNum].zoneName != nullptr; testNum++) {
Locale locale = Locale::createFromName(zoneDisplayTestData[testNum].localeName);
TimeZone *zone = TimeZone::createTimeZone(zoneDisplayTestData[testNum].zoneName);
UnicodeString displayName;
zone->getDisplayName(zoneDisplayTestData[testNum].summerTime,
zoneDisplayTestData[testNum].style,
locale,
displayName);
if (displayName != zoneDisplayTestData[testNum].expectedDisplayName) {
char name[100];
UErrorCode status = U_ZERO_ERROR;
displayName.extract(name, 100, nullptr, status);
if (isDevelopmentBuild) {
sawAnError = true;
dataerrln("Incorrect time zone display name. zone = \"%s\",\n"
" locale = \"%s\", style = %s, Summertime = %d\n"
" Expected \"%s\", "
" Got \"%s\"\n Error: %s", zoneDisplayTestData[testNum].zoneName,
zoneDisplayTestData[testNum].localeName,
zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
"SHORT" : "LONG",
zoneDisplayTestData[testNum].summerTime,
zoneDisplayTestData[testNum].expectedDisplayName,
name,
u_errorName(status));
} else {
logln("Incorrect time zone display name. zone = \"%s\",\n"
" locale = \"%s\", style = %s, Summertime = %d\n"
" Expected \"%s\", "
" Got \"%s\"\n", zoneDisplayTestData[testNum].zoneName,
zoneDisplayTestData[testNum].localeName,
zoneDisplayTestData[testNum].style==TimeZone::SHORT ?
"SHORT" : "LONG",
zoneDisplayTestData[testNum].summerTime,
zoneDisplayTestData[testNum].expectedDisplayName,
name);
}
}
delete zone;
}
if (sawAnError) {
dataerrln("***Note: Errors could be the result of changes to zoneStrings locale data");
}
}
void TimeZoneTest::TestGetRegion()
{
static const struct {
const char *id;
const char *region;
} data[] = {
{"America/Los_Angeles", "US"},
{"America/Indianapolis", "US"}, // CLDR canonical, Olson backward
{"America/Indiana/Indianapolis", "US"}, // CLDR alias
{"Mexico/General", "MX"}, // Link America/Mexico_City, Olson backward
{"Etc/UTC", "001"},
{"EST5EDT", "US"},
{"PST", "US"}, // Link America/Los_Angeles
{"Europe/Helsinki", "FI"},
{"Europe/Mariehamn", "AX"}, // Link Europe/Helsinki, but in zone.tab
{"Asia/Riyadh", "SA"},
// tz file solar87 was removed from tzdata2013i
// {"Asia/Riyadh87", "001"}, // this should be "SA" actually, but not in zone.tab
{"Atlantic/Jan_Mayen", "SJ"},
{"Pacific/Truk", "FM"},
{"Etc/Unknown", nullptr}, // CLDR canonical, but not a sysmte zone ID
{"bogus", nullptr}, // bogus
{"GMT+08:00", nullptr}, // a custom ID, not a system zone ID
{nullptr, nullptr}
};
int32_t i;
char region[4];
UErrorCode sts;
for (i = 0; data[i].id; i++) {
sts = U_ZERO_ERROR;
TimeZone::getRegion(data[i].id, region, sizeof(region), sts);
if (U_SUCCESS(sts)) {
if (data[i].region == nullptr) {
errln(UnicodeString("Fail: getRegion(\"") + data[i].id + "\") returns "
+ region + " [expected: U_ILLEGAL_ARGUMENT_ERROR]");
} else if (uprv_strcmp(region, data[i].region) != 0) {
errln(UnicodeString("Fail: getRegion(\"") + data[i].id + "\") returns "
+ region + " [expected: " + data[i].region + "]");
}
} else if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
if (data[i].region != nullptr) {
dataerrln(UnicodeString("Fail: getRegion(\"") + data[i].id
+ "\") returns error status U_ILLEGAL_ARGUMENT_ERROR [expected: "
+ data[i].region + "]");
}
} else {
errln(UnicodeString("Fail: getRegion(\"") + data[i].id
+ "\") returns an unexpected error status");
}
}
// Extra test cases for short buffer
int32_t len;
char region2[2];
sts = U_ZERO_ERROR;
len = TimeZone::getRegion("America/New_York", region2, sizeof(region2), sts);
if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
dataerrln("Error calling TimeZone::getRegion");
} else {
if (sts != U_STRING_NOT_TERMINATED_WARNING) {
errln("Expected U_STRING_NOT_TERMINATED_WARNING");
}
if (len != 2) { // length of "US"
errln("Incorrect result length");
}
if (uprv_strncmp(region2, "US", 2) != 0) {
errln("Incorrect result");
}
}
char region1[1];
sts = U_ZERO_ERROR;
len = TimeZone::getRegion("America/Chicago", region1, sizeof(region1), sts);
if (sts == U_ILLEGAL_ARGUMENT_ERROR) {
dataerrln("Error calling TimeZone::getRegion");
} else {
if (sts != U_BUFFER_OVERFLOW_ERROR) {
errln("Expected U_BUFFER_OVERFLOW_ERROR");
}
if (len != 2) { // length of "US"
errln("Incorrect result length");
}
}
}
void TimeZoneTest::TestGetUnknown() {
const TimeZone &unknown = TimeZone::getUnknown();
UnicodeString expectedID = UNICODE_STRING_SIMPLE("Etc/Unknown");
UnicodeString id;
assertEquals("getUnknown() wrong ID", expectedID, unknown.getID(id));
assertTrue("getUnknown() wrong offset", 0 == unknown.getRawOffset());
assertFalse("getUnknown() uses DST", unknown.useDaylightTime());
}
void TimeZoneTest::TestGetGMT() {
const TimeZone *gmt = TimeZone::getGMT();
UnicodeString expectedID = UNICODE_STRING_SIMPLE("GMT");
UnicodeString id;
assertEquals("getGMT() wrong ID", expectedID, gmt->getID(id));
assertTrue("getGMT() wrong offset", 0 == gmt->getRawOffset());
assertFalse("getGMT() uses DST", gmt->useDaylightTime());
}
void TimeZoneTest::TestGetWindowsID() {
static const struct {
const char *id;
const char *winid;
} TESTDATA[] = {
{"America/New_York", "Eastern Standard Time"},
{"America/Montreal", "Eastern Standard Time"},
{"America/Los_Angeles", "Pacific Standard Time"},
{"America/Vancouver", "Pacific Standard Time"},
{"Asia/Shanghai", "China Standard Time"},
{"Asia/Chongqing", "China Standard Time"},
{"America/Indianapolis", "US Eastern Standard Time"}, // CLDR canonical name
{"America/Indiana/Indianapolis", "US Eastern Standard Time"}, // tzdb canonical name
{"Asia/Khandyga", "Yakutsk Standard Time"},
{"Australia/Eucla", "Aus Central W. Standard Time"}, // formerly no Windows ID mapping, now has one
{"Bogus", ""},
{nullptr, nullptr},
};
for (int32_t i = 0; TESTDATA[i].id != nullptr; i++) {
UErrorCode sts = U_ZERO_ERROR;
UnicodeString windowsID;
TimeZone::getWindowsID(UnicodeString(TESTDATA[i].id), windowsID, sts);
assertSuccess(TESTDATA[i].id, sts);
assertEquals(TESTDATA[i].id, UnicodeString(TESTDATA[i].winid), windowsID, true);
}
}
void TimeZoneTest::TestGetIDForWindowsID() {
static const struct {
const char *winid;
const char *region;
const char *id;
} TESTDATA[] = {
{"Eastern Standard Time", nullptr, "America/New_York"},
{"Eastern Standard Time", "US", "America/New_York"},
{"Eastern Standard Time", "CA", "America/Toronto"},
{"Eastern Standard Time", "CN", "America/New_York"},
{"China Standard Time", nullptr, "Asia/Shanghai"},
{"China Standard Time", "CN", "Asia/Shanghai"},
{"China Standard Time", "HK", "Asia/Hong_Kong"},
{"Mid-Atlantic Standard Time", nullptr, ""}, // No tz database mapping
{"Bogus", nullptr, ""},
{nullptr, nullptr, nullptr},
};
for (int32_t i = 0; TESTDATA[i].winid != nullptr; i++) {
UErrorCode sts = U_ZERO_ERROR;
UnicodeString id;
TimeZone::getIDForWindowsID(UnicodeString(TESTDATA[i].winid), TESTDATA[i].region,
id, sts);
assertSuccess(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, sts);
assertEquals(UnicodeString(TESTDATA[i].winid) + "/" + TESTDATA[i].region, TESTDATA[i].id, id, true);
}
}
void TimeZoneTest::TestCasablancaNameAndOffset22041() {
std::unique_ptr<TimeZone> zone(TimeZone::createTimeZone("Africa/Casablanca"));
UnicodeString standardName, summerName;
zone->getDisplayName(false, TimeZone::LONG, Locale::getEnglish(), standardName);
zone->getDisplayName(true, TimeZone::LONG, Locale::getEnglish(), summerName);
int32_t raw, dst;
UErrorCode status = U_ZERO_ERROR;
zone->getOffset(Calendar::getNow(), false, raw, dst, status);
assertEquals(u"TimeZone name for Africa/Casablanca should not contain '+02' since it is located in UTC, but got "
+ standardName, -1, standardName.indexOf("+02"));
assertEquals(u"TimeZone name for Africa/Casablanca should not contain '+02' since it is located in UTC, but got "
+ summerName, -1, summerName.indexOf("+02"));
assertEquals("getRawOffset() and the raw from getOffset(now, false, raw, dst, status) should not be different but got",
zone->getRawOffset(), raw);
}
void TimeZoneTest::TestRawOffsetAndOffsetConsistency22041() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<StringEnumeration> s(TimeZone::createEnumeration(status));
if (U_FAILURE(status)) {
dataerrln("Unable to create TimeZone enumeration");
return;
}
const char* tz;
UDate now = Calendar::getNow();
while ((tz = s->next(nullptr, status)) != nullptr && U_SUCCESS(status)) {
std::unique_ptr<TimeZone> zone(TimeZone::createTimeZone(tz));
int32_t raw, dst;
zone->getOffset(now, false, raw, dst, status);
if (U_FAILURE(status)) {
errln("TimeZone '%s' getOffset() return error", tz);
}
assertEquals(u"TimeZone '" + UnicodeString(tz) +
u"' getRawOffset() and the raw from getOffset(now, false, raw, dst, status) should not be different but got",
zone->getRawOffset(), raw);
}
}
void TimeZoneTest::TestGetIanaID() {
const char16_t* UNKNOWN = u"Etc/Unknown";
static const struct {
const char16_t* id;
const char16_t* expected;
} TESTDATA[] = {
{u"", UNKNOWN},
{nullptr, UNKNOWN},
{UNKNOWN, UNKNOWN},
{u"America/New_York", u"America/New_York"},
{u"Asia/Calcutta", u"Asia/Kolkata"},
{u"Europe/Kiev", u"Europe/Kyiv"},
{u"Europe/Zaporozhye", u"Europe/Kyiv"},
{u"Etc/GMT-1", u"Etc/GMT-1"},
{u"Etc/GMT+20", UNKNOWN},
{u"PST8PDT", u"America/Los_Angeles"},
{u"GMT-08:00", UNKNOWN},
{nullptr, nullptr}
};
for (int32_t i = 0; TESTDATA[i].expected != nullptr; i++) {
UErrorCode sts = U_ZERO_ERROR;
UnicodeString inputID(TESTDATA[i].id);
UnicodeString ianaID;
TimeZone::getIanaID(inputID, ianaID, sts);
if (u_strcmp(TESTDATA[i].expected, UNKNOWN) == 0) {
assertEquals(inputID + " should fail", U_ILLEGAL_ARGUMENT_ERROR, sts);
assertTrue(inputID + " should set bogus", ianaID.isBogus());
} else {
assertEquals(inputID, UnicodeString(TESTDATA[i].expected), ianaID);
// Calling getIanaID with an IANA ID should return the same
UnicodeString ianaID2;
TimeZone::getIanaID(ianaID, ianaID2, sts);
assertEquals(ianaID, ianaID, ianaID2);
}
}
}
void TimeZoneTest::TestGMTMinus24ICU22526() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<TimeZone> tz(TimeZone::createTimeZone("GMT-23:59"), status);
U_ASSERT(U_SUCCESS(status));
GregorianCalendar gc(tz.orphan(), status);
gc.setTime(123456789, status);
gc.get(UCAL_MONTH, status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|