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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* Copyright (c) 1997-2016, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/simpletz.h"
#include "unicode/smpdtfmt.h"
#include "unicode/strenum.h"
#include "unicode/gregocal.h"
#include "tzregts.h"
#include "calregts.h"
#include "cmemory.h"
// *****************************************************************************
// class TimeZoneRegressionTest
// *****************************************************************************
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
void
TimeZoneRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
// if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
switch (index) {
CASE(0, Test4052967);
CASE(1, Test4073209);
CASE(2, Test4073215);
CASE(3, Test4084933);
CASE(4, Test4096952);
CASE(5, Test4109314);
CASE(6, Test4126678);
CASE(7, Test4151406);
CASE(8, Test4151429);
CASE(9, Test4154537);
CASE(10, Test4154542);
CASE(11, Test4154650);
CASE(12, Test4154525);
CASE(13, Test4162593);
CASE(14, TestJ186);
CASE(15, TestJ449);
CASE(16, TestJDK12API);
CASE(17, Test4176686);
CASE(18, Test4184229);
CASE(19, TestNegativeDaylightSaving);
default: name = ""; break;
}
}
UBool
TimeZoneRegressionTest::failure(UErrorCode status, const char* msg)
{
if(U_FAILURE(status)) {
errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
return true;
}
return false;
}
/**
* @bug 4052967
*/
void TimeZoneRegressionTest:: Test4052967() {
// {sfb} not applicable in C++ ?
/*logln("*** CHECK TIMEZONE AGAINST HOST OS SETTING ***");
logln("user.timezone:" + System.getProperty("user.timezone", "<not set>"));
logln(new Date().toString());
logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
}
/**
* @bug 4073209
*/
void TimeZoneRegressionTest:: Test4073209() {
TimeZone *z1 = TimeZone::createTimeZone("PST");
TimeZone *z2 = TimeZone::createTimeZone("PST");
if (z1 == z2)
errln("Fail: TimeZone should return clones");
delete z1;
delete z2;
}
UDate TimeZoneRegressionTest::findTransitionBinary(const SimpleTimeZone& tz, UDate min, UDate max) {
UErrorCode status = U_ZERO_ERROR;
UBool startsInDST = tz.inDaylightTime(min, status);
if (failure(status, "SimpleTimeZone::inDaylightTime")) return 0;
if (tz.inDaylightTime(max, status) == startsInDST) {
logln(UnicodeString("Error: inDaylightTime() != ") + (!startsInDST ? "true" : "false"));
return 0;
}
if (failure(status, "SimpleTimeZone::inDaylightTime")) return 0;
while ((max - min) > 100) { // Min accuracy in ms
UDate mid = (min + max) / 2;
if (tz.inDaylightTime(mid, status) == startsInDST) {
min = mid;
} else {
max = mid;
}
if (failure(status, "SimpleTimeZone::inDaylightTime")) return 0;
}
return (min + max) / 2;
}
UDate TimeZoneRegressionTest::findTransitionStepwise(const SimpleTimeZone& tz, UDate min, UDate max) {
UErrorCode status = U_ZERO_ERROR;
UBool startsInDST = tz.inDaylightTime(min, status);
if (failure(status, "SimpleTimeZone::inDaylightTime")) return 0;
while (min < max) {
if (tz.inDaylightTime(min, status) != startsInDST) {
return min;
}
if (failure(status, "SimpleTimeZone::inDaylightTime")) return 0;
min += static_cast<UDate>(24) * 60 * 60 * 1000; // one day
}
return 0;
}
/**
* @bug 4073215
*/
// {sfb} will this work using a Calendar?
void TimeZoneRegressionTest:: Test4073215()
{
UErrorCode status = U_ZERO_ERROR;
UnicodeString str, str2;
LocalPointer<SimpleTimeZone> z(new SimpleTimeZone(0, "GMT"), status);
if (U_FAILURE(status)) {
errln("Fail: Failed to create SimpleTimeZone %s", u_errorName(status));
return;
}
if (z->useDaylightTime()) {
errln("Fail: Fix test to start with non-DST zone");
}
z->setStartRule(UCAL_FEBRUARY, 1, UCAL_SUNDAY, 0, status);
failure(status, "z->setStartRule()");
z->setEndRule(UCAL_MARCH, -1, UCAL_SUNDAY, 0, status);
failure(status, "z->setStartRule()");
if (!z->useDaylightTime()) {
errln("Fail: DST not active");
}
GregorianCalendar cal(1997, UCAL_JANUARY, 31, status);
if(U_FAILURE(status)) {
dataerrln("Error creating calendar %s", u_errorName(status));
return;
}
failure(status, "new GregorianCalendar");
cal.adoptTimeZone(z.orphan());
SimpleDateFormat sdf(UnicodeString("E d MMM yyyy G HH:mm"), status);
if(U_FAILURE(status)) {
dataerrln("Error creating date format %s", u_errorName(status));
return;
}
sdf.setCalendar(cal);
failure(status, "new SimpleDateFormat");
UDate jan31, mar1, mar31;
UBool indt = cal.getTimeZone().inDaylightTime(jan31 = cal.getTime(status), status);
failure(status, "inDaylightTime or getTime call on Jan 31");
if (indt) {
errln("Fail: Jan 31 inDaylightTime=true, exp false");
}
cal.set(1997, UCAL_MARCH, 1);
indt = cal.getTimeZone().inDaylightTime(mar1 = cal.getTime(status), status);
failure(status, "inDaylightTime or getTime call on Mar 1");
if (!indt) {
UnicodeString str;
sdf.format(cal.getTime(status), str);
failure(status, "getTime");
errln(UnicodeString("Fail: ") + str + " inDaylightTime=false, exp true");
}
cal.set(1997, UCAL_MARCH, 31);
indt = cal.getTimeZone().inDaylightTime(mar31 = cal.getTime(status), status);
failure(status, "inDaylightTime or getTime call on Mar 31");
if (indt) {
errln("Fail: Mar 31 inDaylightTime=true, exp false");
}
/*
cal.set(1997, Calendar::DECEMBER, 31);
UDate dec31 = cal.getTime(status);
failure(status, "getTime");
UDate trans = findTransitionStepwise(*z, jan31, dec31);
logln((UnicodeString)"Stepwise from " +
sdf.format(jan31, str.remove()) + "; transition at " +
(trans?sdf.format(trans, str2.remove()):(UnicodeString)"NONE"));
trans = findTransitionStepwise(*z, mar1, dec31);
logln((UnicodeString)"Stepwise from " +
sdf.format(mar1, str.remove()) + "; transition at " +
(trans?sdf.format(trans, str2.remove()):(UnicodeString)"NONE"));
trans = findTransitionStepwise(*z, mar31, dec31);
logln((UnicodeString)"Stepwise from " +
sdf.format(mar31, str.remove()) + "; transition at " +
(trans?sdf.format(trans, str2.remove()):(UnicodeString)"NONE"));
*/
}
/**
* @bug 4084933
* The expected behavior of TimeZone around the boundaries is:
* (Assume transition time of 2:00 AM)
* day of onset 1:59 AM STD = display name 1:59 AM ST
* 2:00 AM STD = display name 3:00 AM DT
* day of end 0:59 AM STD = display name 1:59 AM DT
* 1:00 AM STD = display name 1:00 AM ST
*/
void TimeZoneRegressionTest:: Test4084933() {
UErrorCode status = U_ZERO_ERROR;
TimeZone *tz = TimeZone::createTimeZone("PST");
int32_t offset1 = tz->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (2*60*60*1000), status);
int32_t offset2 = tz->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (2*60*60*1000)-1, status);
int32_t offset3 = tz->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (1*60*60*1000), status);
int32_t offset4 = tz->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (1*60*60*1000)-1, status);
/*
* The following was added just for consistency. It shows that going *to* Daylight
* Savings Time (PDT) does work at 2am.
*/
int32_t offset5 = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (2*60*60*1000), status);
int32_t offset6 = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (2*60*60*1000)-1, status);
int32_t offset5a = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (3*60*60*1000), status);
int32_t offset6a = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (3*60*60*1000)-1, status);
int32_t offset7 = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (1*60*60*1000), status);
int32_t offset8 = tz->getOffset(1,
1997, UCAL_APRIL, 6, UCAL_SUNDAY, (1*60*60*1000)-1, status);
int32_t SToffset = static_cast<int32_t>(-8 * 60 * 60 * 1000L);
int32_t DToffset = static_cast<int32_t>(-7 * 60 * 60 * 1000L);
#define ERR_IF_FAIL(x) if(x) { dataerrln("FAIL: TimeZone misbehaving - %s", #x); }
ERR_IF_FAIL(U_FAILURE(status))
ERR_IF_FAIL(offset1 != SToffset)
ERR_IF_FAIL(offset2 != SToffset)
ERR_IF_FAIL(offset3 != SToffset)
ERR_IF_FAIL(offset4 != DToffset)
ERR_IF_FAIL(offset5 != DToffset)
ERR_IF_FAIL(offset6 != SToffset)
ERR_IF_FAIL(offset5a != DToffset)
ERR_IF_FAIL(offset6a != DToffset)
ERR_IF_FAIL(offset7 != SToffset)
ERR_IF_FAIL(offset8 != SToffset)
#undef ERR_IF_FAIL
delete tz;
}
/**
* @bug 4096952
*/
void TimeZoneRegressionTest:: Test4096952() {
// {sfb} serialization not applicable
/*
UnicodeString ZONES [] = { UnicodeString("GMT"), UnicodeString("MET"), UnicodeString("IST") };
UBool pass = true;
//try {
for (int32_t i=0; i < ZONES.length; ++i) {
TimeZone *zone = TimeZone::createTimeZone(ZONES[i]);
UnicodeString id;
if (zone->getID(id) != ZONES[i])
errln("Fail: Test broken; zones not instantiating");
ByteArrayOutputStream baos;
ObjectOutputStream ostream =
new ObjectOutputStream(baos = new
ByteArrayOutputStream());
ostream.writeObject(zone);
ostream.close();
baos.close();
ObjectInputStream istream =
new ObjectInputStream(new
ByteArrayInputStream(baos.toByteArray()));
TimeZone frankenZone = (TimeZone) istream.readObject();
//logln("Zone: " + zone);
//logln("FrankenZone: " + frankenZone);
if (!zone.equals(frankenZone)) {
logln("TimeZone " + zone.getID() +
" not equal to serialized/deserialized one");
pass = false;
}
}
if (!pass) errln("Fail: TimeZone serialization/equality bug");
}
catch (IOException e) {
errln("Fail: " + e);
e.print32_tStackTrace();
}
catch (ClassNotFoundException e) {
errln("Fail: " + e);
e.print32_tStackTrace();
}
*/
}
/**
* @bug 4109314
*/
void TimeZoneRegressionTest:: Test4109314() {
UErrorCode status = U_ZERO_ERROR;
GregorianCalendar *testCal = dynamic_cast<GregorianCalendar*>(Calendar::createInstance(status));
if(U_FAILURE(status)) {
dataerrln("Error creating calendar %s", u_errorName(status));
delete testCal;
return;
}
failure(status, "Calendar::createInstance");
TimeZone *PST = TimeZone::createTimeZone("PST");
/*Object[] testData = {
PST, new Date(98,Calendar.APRIL,4,22,0), new Date(98, Calendar.APRIL, 5,6,0),
PST, new Date(98,Calendar.OCTOBER,24,22,0), new Date(98,Calendar.OCTOBER,25,6,0),
};*/
UDate testData [] = {
CalendarRegressionTest::makeDate(98,UCAL_APRIL,4,22,0),
CalendarRegressionTest::makeDate(98, UCAL_APRIL,5,6,0),
CalendarRegressionTest::makeDate(98,UCAL_OCTOBER,24,22,0),
CalendarRegressionTest::makeDate(98,UCAL_OCTOBER,25,6,0)
};
UBool pass = true;
for (int32_t i = 0; i < 4; i+=2) {
//testCal->setTimeZone((TimeZone) testData[i]);
testCal->setTimeZone(*PST);
UDate t = testData[i];
UDate end = testData[i+1];
while(testCal->getTime(status) < end) {
testCal->setTime(t, status);
if ( ! checkCalendar314(testCal, PST))
pass = false;
t += 60*60*1000.0;
}
}
if ( ! pass)
errln("Fail: TZ API inconsistent");
delete testCal;
delete PST;
}
UBool
TimeZoneRegressionTest::checkCalendar314(GregorianCalendar *testCal, TimeZone *testTZ)
{
UErrorCode status = U_ZERO_ERROR;
// GregorianCalendar testCal = aCal.clone();
int32_t tzOffset, tzRawOffset;
float tzOffsetFloat,tzRawOffsetFloat;
// Here is where the user made an error. They were passing in the value of
// the MILLSECOND field; you need to pass in the millis in the day in STANDARD
// time.
UDate millis = testCal->get(UCAL_MILLISECOND, status) +
1000.0 * (testCal->get(UCAL_SECOND, status) +
60.0 * (testCal->get(UCAL_MINUTE, status) +
60.0 * (testCal->get(UCAL_HOUR_OF_DAY, status)))) -
testCal->get(UCAL_DST_OFFSET, status);
/* Fix up millis to be in range. ASSUME THAT WE ARE NOT AT THE
* BEGINNING OR END OF A MONTH. We must add this code because
* getOffset() has been changed to be more strict about the parameters
* it receives -- it turns out that this test was passing in illegal
* values. */
int32_t date = testCal->get(UCAL_DATE, status);
int32_t dow = testCal->get(UCAL_DAY_OF_WEEK, status);
while(millis < 0) {
millis += U_MILLIS_PER_DAY;
--date;
dow = UCAL_SUNDAY + ((dow - UCAL_SUNDAY + 6) % 7);
}
while (millis >= U_MILLIS_PER_DAY) {
millis -= U_MILLIS_PER_DAY;
++date;
dow = UCAL_SUNDAY + ((dow - UCAL_SUNDAY + 1) % 7);
}
tzOffset = testTZ->getOffset(static_cast<uint8_t>(testCal->get(UCAL_ERA, status)),
testCal->get(UCAL_YEAR, status),
testCal->get(UCAL_MONTH, status),
date,
static_cast<uint8_t>(dow),
static_cast<int32_t>(millis),
status);
tzRawOffset = testTZ->getRawOffset();
tzOffsetFloat = static_cast<float>(tzOffset) / static_cast<float>(3600000);
tzRawOffsetFloat = static_cast<float>(tzRawOffset) / static_cast<float>(3600000);
UDate testDate = testCal->getTime(status);
UBool inDaylightTime = testTZ->inDaylightTime(testDate, status);
LocalPointer<SimpleDateFormat> sdf(new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status),
status);
if (U_FAILURE(status)) {
errln("Error creating SimpleDateFormat %s", u_errorName(status));
return false;
}
sdf->setCalendar(*testCal);
UnicodeString inDaylightTimeString;
UBool passed;
if(inDaylightTime)
{
inDaylightTimeString = " DST ";
passed = (tzOffset == (tzRawOffset + 3600000));
}
else
{
inDaylightTimeString = " ";
passed = (tzOffset == tzRawOffset);
}
UnicodeString output;
FieldPosition pos(FieldPosition::DONT_CARE);
output = testTZ->getID(output) + " " + sdf->format(testDate, output, pos) +
" Offset(" + tzOffsetFloat + ")" +
" RawOffset(" + tzRawOffsetFloat + ")" +
" " + millis / static_cast<float>(3600000) + " " +
inDaylightTimeString;
if (passed)
output += " ";
else
output += "ERROR";
if (passed)
logln(output);
else
errln(output);
return passed;
}
/**
* @bug 4126678
* CANNOT REPRODUDE
*
* Yet another _alleged_ bug in TimeZone::getOffset(), a method that never
* should have been made public. It's simply too hard to use correctly.
*
* The original test code failed to do the following:
* (1) Call Calendar::setTime() before getting the fields!
* (2) Use the right millis (as usual) for getOffset(); they were passing
* in the MILLIS field, instead of the STANDARD MILLIS IN DAY.
* When you fix these two problems, the test passes, as expected.
*/
void TimeZoneRegressionTest:: Test4126678()
{
UErrorCode status = U_ZERO_ERROR;
Calendar *cal = Calendar::createInstance(status);
if(U_FAILURE(status)) {
dataerrln("Error creating calendar %s", u_errorName(status));
delete cal;
return;
}
failure(status, "Calendar::createInstance");
TimeZone *tz = TimeZone::createTimeZone("PST");
cal->adoptTimeZone(tz);
cal->set(1998, UCAL_APRIL, 5, 10, 0);
if (! tz->useDaylightTime() || U_FAILURE(status))
dataerrln("We're not in Daylight Savings Time and we should be. - %s", u_errorName(status));
//cal.setTime(dt);
int32_t era = cal->get(UCAL_ERA, status);
int32_t year = cal->get(UCAL_YEAR, status);
int32_t month = cal->get(UCAL_MONTH, status);
int32_t day = cal->get(UCAL_DATE, status);
int32_t dayOfWeek = cal->get(UCAL_DAY_OF_WEEK, status);
int32_t millis = cal->get(UCAL_MILLISECOND, status) +
(cal->get(UCAL_SECOND, status) +
(cal->get(UCAL_MINUTE, status) +
(cal->get(UCAL_HOUR, status) * 60) * 60) * 1000) -
cal->get(UCAL_DST_OFFSET, status);
failure(status, "cal->get");
int32_t offset = tz->getOffset(static_cast<uint8_t>(era), year, month, day, static_cast<uint8_t>(dayOfWeek), millis, status);
int32_t raw_offset = tz->getRawOffset();
if (offset == raw_offset)
dataerrln("Offsets should match");
delete cal;
}
/**
* @bug 4151406
* TimeZone::getAvailableIDs(int32_t) throws exception for certain values,
* due to a faulty constant in TimeZone::java.
*/
void TimeZoneRegressionTest:: Test4151406() {
int32_t max = 0;
for (int32_t h=-28; h<=30; ++h) {
// h is in half-hours from GMT; rawoffset is in millis
int32_t rawoffset = h * 1800000;
int32_t hh = (h<0) ? -h : h;
UnicodeString hname = UnicodeString((h<0) ? "GMT-" : "GMT+") +
((hh/2 < 10) ? "0" : "") +
(hh/2) + ':' +
((hh%2==0) ? "00" : "30");
//try {
UErrorCode ec = U_ZERO_ERROR;
int32_t count;
StringEnumeration* ids = TimeZone::createEnumerationForRawOffset(rawoffset, ec);
if (U_FAILURE(ec)) {
dataerrln("Fail: TimeZone::createEnumeration(rawoffset)");
continue;
}
count = ids->count(ec);
if (count> max)
max = count;
if (count > 0) {
logln(hname + ' ' + UnicodeString(count) + UnicodeString(" e.g. ") + *ids->snext(ec));
} else {
logln(hname + ' ' + count);
}
// weiv 11/27/2002: why uprv_free? This should be a delete
delete ids;
//delete [] ids;
//uprv_free(ids);
/*} catch (Exception e) {
errln(hname + ' ' + "Fail: " + e);
}*/
}
logln("Maximum zones per offset = %d", max);
}
/**
* @bug 4151429
*/
void TimeZoneRegressionTest:: Test4151429() {
// {sfb} silly test in C++, since we are using an enum and not an int
//try {
/*TimeZone *tz = TimeZone::createTimeZone("GMT");
UnicodeString name;
tz->getDisplayName(true, TimeZone::LONG,
Locale.getDefault(), name);
errln("IllegalArgumentException not thrown by TimeZone::getDisplayName()");*/
//} catch(IllegalArgumentException e) {}
}
/**
* @bug 4154537
* SimpleTimeZone::hasSameRules() doesn't work for zones with no DST
* and different DST parameters.
*/
void TimeZoneRegressionTest:: Test4154537() {
UErrorCode status = U_ZERO_ERROR;
// tz1 and tz2 have no DST and different rule parameters
LocalPointer<SimpleTimeZone> tz1(new SimpleTimeZone(0, "1", 0, 0, 0, 0, 2, 0, 0, 0, status), status);
LocalPointer<SimpleTimeZone> tz2(new SimpleTimeZone(0, "2", 1, 0, 0, 0, 3, 0, 0, 0, status), status);
// tza and tzA have the same rule params
LocalPointer<SimpleTimeZone> tza(new SimpleTimeZone(0, "a", 0, 1, 0, 0, 3, 2, 0, 0, status), status);
LocalPointer<SimpleTimeZone> tzA(new SimpleTimeZone(0, "A", 0, 1, 0, 0, 3, 2, 0, 0, status), status);
// tzb differs from tza
LocalPointer<SimpleTimeZone> tzb(new SimpleTimeZone(0, "b", 0, 1, 0, 0, 3, 1, 0, 0, status), status);
if (U_FAILURE(status)) {
errln("Couldn't create TimeZones %s", u_errorName(status));
return;
}
if (tz1->useDaylightTime() || tz2->useDaylightTime() ||
!tza->useDaylightTime() || !tzA->useDaylightTime() ||
!tzb->useDaylightTime()) {
errln("Test is broken -- rewrite it");
}
if (!tza->hasSameRules(*tzA) || tza->hasSameRules(*tzb)) {
errln("Fail: hasSameRules() broken for zones with rules");
}
if (!tz1->hasSameRules(*tz2)) {
errln("Fail: hasSameRules() returns false for zones without rules");
//errln("zone 1 = " + tz1);
//errln("zone 2 = " + tz2);
}
}
/**
* @bug 4154542
* SimpleTimeZOne constructors, setStartRule(), and setEndRule() don't
* check for out-of-range arguments.
*/
void TimeZoneRegressionTest:: Test4154542()
{
const int32_t GOOD = 1;
const int32_t BAD = 0;
const int32_t GOOD_MONTH = UCAL_JANUARY;
const int32_t GOOD_DAY = 1;
const int32_t GOOD_DAY_OF_WEEK = UCAL_SUNDAY;
const int32_t GOOD_TIME = 0;
int32_t DATA [] = {
GOOD, INT32_MIN, 0, INT32_MAX, INT32_MIN,
GOOD, UCAL_JANUARY, -5, UCAL_SUNDAY, 0,
GOOD, UCAL_DECEMBER, 5, UCAL_SATURDAY, 24*60*60*1000,
BAD, UCAL_DECEMBER, 5, UCAL_SATURDAY, 24*60*60*1000+1,
BAD, UCAL_DECEMBER, 5, UCAL_SATURDAY, -1,
BAD, UCAL_JANUARY, -6, UCAL_SUNDAY, 0,
BAD, UCAL_DECEMBER, 6, UCAL_SATURDAY, 24*60*60*1000,
GOOD, UCAL_DECEMBER, 1, 0, 0,
GOOD, UCAL_DECEMBER, 31, 0, 0,
BAD, UCAL_APRIL, 31, 0, 0,
BAD, UCAL_DECEMBER, 32, 0, 0,
BAD, UCAL_JANUARY-1, 1, UCAL_SUNDAY, 0,
BAD, UCAL_DECEMBER+1, 1, UCAL_SUNDAY, 0,
GOOD, UCAL_DECEMBER, 31, -UCAL_SUNDAY, 0,
GOOD, UCAL_DECEMBER, 31, -UCAL_SATURDAY, 0,
BAD, UCAL_DECEMBER, 32, -UCAL_SATURDAY, 0,
BAD, UCAL_DECEMBER, -32, -UCAL_SATURDAY, 0,
BAD, UCAL_DECEMBER, 31, -UCAL_SATURDAY-1, 0,
};
UErrorCode status = U_ZERO_ERROR;
LocalPointer<SimpleTimeZone> zone(new SimpleTimeZone(0, "Z"), status);
if (U_FAILURE(status)) {
errln("Fail: failed to create SimpleTimeZone %s", u_errorName(status));
return;
}
for (int32_t i=0; i < 18*5; i+=5) {
UBool shouldBeGood = (DATA[i] == GOOD);
int32_t month = DATA[i+1];
int32_t day = DATA[i+2];
int32_t dayOfWeek = DATA[i+3];
int32_t time = DATA[i+4];
status = U_ZERO_ERROR;
//Exception ex = null;
//try {
zone->setStartRule(month, day, dayOfWeek, time, status);
//} catch (IllegalArgumentException e) {
// ex = e;
//}
if (U_SUCCESS(status) != shouldBeGood) {
errln(UnicodeString("setStartRule(month=") + month + ", day=" + day +
", dayOfWeek=" + dayOfWeek + ", time=" + time +
(shouldBeGood ? (") should work")
: ") should fail but doesn't"));
}
//ex = null;
//try {
status = U_ZERO_ERROR;
zone->setEndRule(month, day, dayOfWeek, time, status);
//} catch (IllegalArgumentException e) {
// ex = e;
//}
if (U_SUCCESS(status) != shouldBeGood) {
errln(UnicodeString("setEndRule(month=") + month + ", day=" + day +
", dayOfWeek=" + dayOfWeek + ", time=" + time +
(shouldBeGood ? (") should work")
: ") should fail but doesn't"));
}
//ex = null;
//try {
// {sfb} need to look into ctor problems! (UErrorCode vs. dst signature confusion)
status = U_ZERO_ERROR;
SimpleTimeZone *temp = new SimpleTimeZone(0, "Z",
static_cast<int8_t>(month), static_cast<int8_t>(day), static_cast<int8_t>(dayOfWeek), time,
static_cast<int8_t>(GOOD_MONTH), static_cast<int8_t>(GOOD_DAY), static_cast<int8_t>(GOOD_DAY_OF_WEEK),
GOOD_TIME,status);
if (temp == nullptr) {
errln("Fail: failed to create SimpleTimeZone %s", u_errorName(status));
return;
}
//} catch (IllegalArgumentException e) {
// ex = e;
//}
if (U_SUCCESS(status) != shouldBeGood) {
errln(UnicodeString("SimpleTimeZone(month=") + month + ", day=" + day +
", dayOfWeek=" + dayOfWeek + ", time=" + time +
(shouldBeGood ? (", <end>) should work")// + ex)
: ", <end>) should fail but doesn't"));
}
delete temp;
//ex = null;
//try {
status = U_ZERO_ERROR;
temp = new SimpleTimeZone(0, "Z",
static_cast<int8_t>(GOOD_MONTH), static_cast<int8_t>(GOOD_DAY), static_cast<int8_t>(GOOD_DAY_OF_WEEK),
GOOD_TIME,
static_cast<int8_t>(month), static_cast<int8_t>(day), static_cast<int8_t>(dayOfWeek), time, status);
if (temp == nullptr) {
errln("Fail: failed to create SimpleTimeZone %s", u_errorName(status));
return;
}
//} catch (IllegalArgumentException e) {
// ex = e;
//}
if (U_SUCCESS(status) != shouldBeGood) {
errln(UnicodeString("SimpleTimeZone(<start>, month=") + month + ", day=" + day +
", dayOfWeek=" + dayOfWeek + ", time=" + time +
(shouldBeGood ? (") should work")// + ex)
: ") should fail but doesn't"));
}
delete temp;
}
}
/**
* @bug 4154525
* SimpleTimeZone accepts illegal DST savings values. These values
* must be non-zero. There is no upper limit at this time.
*/
void
TimeZoneRegressionTest::Test4154525()
{
const int32_t GOOD = 1, BAD = 0;
int32_t DATA [] = {
1, GOOD,
0, BAD,
-1, GOOD, // #13566 updates SimpleTimeZone to support negative DST saving amount
60*60*1000, GOOD,
INT32_MAX, GOOD, // no upper limit on DST savings at this time
INT32_MIN, GOOD // no lower limit as well
};
UErrorCode status = U_ZERO_ERROR;
for(int32_t i = 0; i < 10; i+=2) {
int32_t savings = DATA[i];
UBool valid = DATA[i+1] == GOOD;
UnicodeString method;
for(int32_t j=0; j < 2; ++j) {
LocalPointer<SimpleTimeZone> z;
switch (j) {
case 0:
method = "constructor";
z.adoptInsteadAndCheckErrorCode(new SimpleTimeZone(0, "id",
UCAL_JANUARY, 1, 0, 0,
UCAL_MARCH, 1, 0, 0,
savings, status), status); // <- what we're interested in
break;
case 1:
method = "setDSTSavings()";
z.adoptInsteadAndCheckErrorCode(new SimpleTimeZone(0, "GMT"), status);
if (z.isValid()) {
z->setDSTSavings(savings, status);
}
break;
}
if(U_FAILURE(status)) {
if(valid) {
errln(UnicodeString("Fail: DST savings of ") + savings + " to " + method + " gave " + u_errorName(status));
}
else {
logln(UnicodeString("Pass: DST savings of ") + savings + " to " + method + " gave " + u_errorName(status));
}
}
else {
if(valid) {
logln(UnicodeString("Pass: DST savings of ") + savings + " accepted by " + method);
}
else {
errln(UnicodeString("Fail: DST savings of ") + savings + " accepted by " + method);
}
}
status = U_ZERO_ERROR;
}
}
}
/**
* @bug 4154650
* SimpleTimeZone.getOffset accepts illegal arguments.
*/
void
TimeZoneRegressionTest::Test4154650()
{
const int32_t GOOD = 1, BAD = 0;
const int32_t GOOD_ERA = GregorianCalendar::AD, GOOD_YEAR = 1998, GOOD_MONTH = UCAL_AUGUST;
const int32_t GOOD_DAY = 2, GOOD_DOW = UCAL_SUNDAY, GOOD_TIME = 16*3600000;
int32_t DATA []= {
GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
GOOD, GregorianCalendar::BC, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
GOOD, GregorianCalendar::AD, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
BAD, GregorianCalendar::BC-1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
BAD, GregorianCalendar::AD+1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY, GOOD_DAY, GOOD_DOW, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, UCAL_DECEMBER, GOOD_DAY, GOOD_DOW, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY-1, GOOD_DAY, GOOD_DOW, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, UCAL_DECEMBER+1, GOOD_DAY, GOOD_DOW, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY, 1, GOOD_DOW, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY, 31, GOOD_DOW, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY, 0, GOOD_DOW, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, UCAL_JANUARY, 32, GOOD_DOW, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, UCAL_SUNDAY, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, UCAL_SATURDAY, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, UCAL_SUNDAY-1, GOOD_TIME,
BAD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, UCAL_SATURDAY+1, GOOD_TIME,
GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 0,
GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000-1,
BAD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, -1,
BAD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000,
};
int32_t dataLen = UPRV_LENGTHOF(DATA);
UErrorCode status = U_ZERO_ERROR;
TimeZone *tz = TimeZone::createDefault();
for(int32_t i = 0; i < dataLen; i += 7) {
UBool good = DATA[i] == GOOD;
//IllegalArgumentException e = null;
//try {
/*int32_t offset = */
tz->getOffset(static_cast<uint8_t>(DATA[i + 1]), DATA[i + 2], DATA[i + 3],
DATA[i + 4], static_cast<uint8_t>(DATA[i + 5]), DATA[i + 6], status);
//} catch (IllegalArgumentException ex) {
// e = ex;
//}
if(good != U_SUCCESS(status)) {
UnicodeString errMsg;
if (good) {
errMsg = (UnicodeString(") threw ") + u_errorName(status));
}
else {
errMsg = UnicodeString(") accepts invalid args", "");
}
errln(UnicodeString("Fail: getOffset(") +
DATA[i+1] + ", " + DATA[i+2] + ", " + DATA[i+3] + ", " +
DATA[i+4] + ", " + DATA[i+5] + ", " + DATA[i+6] +
errMsg);
}
status = U_ZERO_ERROR; // reset
}
delete tz;
}
/**
* @bug 4162593
* TimeZone broken at midnight. The TimeZone code fails to handle
* transitions at midnight correctly.
*/
void
TimeZoneRegressionTest::Test4162593()
{
UErrorCode status = U_ZERO_ERROR;
LocalPointer<SimpleDateFormat> fmt(new SimpleDateFormat("z", Locale::getUS(), status), status);
if (U_FAILURE(status)) {
dataerrln("Error creating SimpleDateFormat %s", u_errorName(status));
return;
}
const int32_t ONE_HOUR = 60*60*1000;
LocalPointer<SimpleTimeZone> asuncion(new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
UCAL_OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
UCAL_MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR, status), status);
if (U_FAILURE(status)) {
dataerrln("Error creating SimpleTimeZone %s", u_errorName(status));
return;
}
/* Zone
* Starting time
* Transition expected between start+1H and start+2H
*/
TimeZone* DATA_TZ[] = {nullptr, nullptr, nullptr};
int32_t DATA_INT [] [5] = {
// These years must be AFTER the Gregorian cutover
{1998, UCAL_SEPTEMBER, 30, 22, 0},
{2000, UCAL_FEBRUARY, 28, 22, 0},
{2000, UCAL_FEBRUARY, 29, 22, 0},
};
bool DATA_BOOL [] = {
true,
false,
true,
};
UnicodeString zone [4];// = new String[4];
DATA_TZ[0] =
new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
UCAL_APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
UCAL_OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR, status);
DATA_TZ[1] = asuncion.getAlias(); DATA_TZ[2] = asuncion.getAlias();
if (DATA_TZ[0] == nullptr || U_FAILURE(status)) {
errln("Error creating DATA_TZ[0] %s", u_errorName(status));
return;
}
for(int32_t j = 0; j < 3; j++) {
TimeZone *tz = DATA_TZ[j];
TimeZone::setDefault(*tz);
fmt->setTimeZone(*tz);
// Must construct the Date object AFTER setting the default zone
int32_t *p = (int32_t*)DATA_INT[j];
UDate d = CalendarRegressionTest::makeDate(p[0], p[1], p[2], p[3], p[4]);
bool transitionExpected = DATA_BOOL[j];
UnicodeString temp;
logln(tz->getID(temp) + ":");
for (int32_t i = 0; i < 4; ++i) {
FieldPosition pos(FieldPosition::DONT_CARE);
zone[i].remove();
zone[i] = fmt->format(d+ i*ONE_HOUR, zone[i], pos);
logln(UnicodeString("") + i + ": " + d + " / " + zone[i]);
//d += (double) ONE_HOUR;
}
if(zone[0] == zone[1] &&
(zone[1] == zone[2]) != transitionExpected &&
zone[2] == zone[3]) {
logln(UnicodeString("Ok: transition ") + transitionExpected);
}
else {
errln("Fail: boundary transition incorrect");
}
}
delete DATA_TZ[0];
}
/**
* getDisplayName doesn't work with unusual savings/offsets.
*/
void TimeZoneRegressionTest::Test4176686() {
// Construct a zone that does not observe DST but
// that does have a DST savings (which should be ignored).
UErrorCode status = U_ZERO_ERROR;
int32_t offset = 90 * 60000; // 1:30
SimpleTimeZone z1(offset, "_std_zone_");
z1.setDSTSavings(45 * 60000, status); // 0:45
// Construct a zone that observes DST for the first 6 months.
SimpleTimeZone z2(offset, "_dst_zone_");
z2.setDSTSavings(45 * 60000, status); // 0:45
z2.setStartRule(UCAL_JANUARY, 1, 0, status);
z2.setEndRule(UCAL_JULY, 1, 0, status);
// Also check DateFormat
SimpleDateFormat fmt1(UnicodeString(u"z"), status);
if (U_FAILURE(status)) {
dataerrln("Failure trying to construct: %s", u_errorName(status));
return;
}
fmt1.setTimeZone(z1); // Format uses standard zone
SimpleDateFormat fmt2(UnicodeString(u"z"), status);
if(!assertSuccess("trying to construct", status))return;
fmt2.setTimeZone(z2); // Format uses DST zone
LocalPointer<Calendar> tempcal(Calendar::createInstance(status));
tempcal->clear();
tempcal->set(1970, UCAL_FEBRUARY, 1);
UDate dst = tempcal->getTime(status); // Time in DST
tempcal->set(1970, UCAL_AUGUST, 1);
UDate std = tempcal->getTime(status); // Time in standard
// Description, Result, Expected Result
UnicodeString a,b,c,d,e,f,g,h,i,j,k,l;
UnicodeString DATA[] = {
"z1.getDisplayName(false, SHORT)/std zone",
z1.getDisplayName(false, TimeZone::SHORT, a), "GMT+1:30",
"z1.getDisplayName(false, LONG)/std zone",
z1.getDisplayName(false, TimeZone::LONG, b), "GMT+01:30",
"z1.getDisplayName(true, SHORT)/std zone",
z1.getDisplayName(true, TimeZone::SHORT, c), "GMT+1:30",
"z1.getDisplayName(true, LONG)/std zone",
z1.getDisplayName(true, TimeZone::LONG, d ), "GMT+01:30",
"z2.getDisplayName(false, SHORT)/dst zone",
z2.getDisplayName(false, TimeZone::SHORT, e), "GMT+1:30",
"z2.getDisplayName(false, LONG)/dst zone",
z2.getDisplayName(false, TimeZone::LONG, f ), "GMT+01:30",
"z2.getDisplayName(true, SHORT)/dst zone",
z2.getDisplayName(true, TimeZone::SHORT, g), "GMT+2:15",
"z2.getDisplayName(true, LONG)/dst zone",
z2.getDisplayName(true, TimeZone::LONG, h ), "GMT+02:15",
"DateFormat.format(std)/std zone", fmt1.format(std, i), "GMT+1:30",
"DateFormat.format(dst)/std zone", fmt1.format(dst, j), "GMT+1:30",
"DateFormat.format(std)/dst zone", fmt2.format(std, k), "GMT+1:30",
"DateFormat.format(dst)/dst zone", fmt2.format(dst, l), "GMT+2:15",
};
for (int32_t idx=0; idx<UPRV_LENGTHOF(DATA); idx+=3) {
if (DATA[idx+1]!=(DATA[idx+2])) {
errln("FAIL: " + DATA[idx] + " -> " + DATA[idx+1] + ", exp " + DATA[idx+2]);
}
}
}
/**
* Make sure setStartRule and setEndRule set the DST savings to nonzero
* if it was zero.
*/
void TimeZoneRegressionTest::TestJ186() {
UErrorCode status = U_ZERO_ERROR;
// NOTE: Setting the DST savings to zero is illegal, so we
// are limited in the testing we can do here. This is why
// lines marked //~ are commented out.
SimpleTimeZone z(0, "ID");
//~z.setDSTSavings(0, status); // Must do this!
z.setStartRule(UCAL_FEBRUARY, 1, UCAL_SUNDAY, 0, status);
failure(status, "setStartRule()");
if (z.useDaylightTime()) {
errln("Fail: useDaylightTime true with start rule only");
}
//~if (z.getDSTSavings() != 0) {
//~ errln("Fail: dst savings != 0 with start rule only");
//~}
z.setEndRule(UCAL_MARCH, -1, UCAL_SUNDAY, 0, status);
failure(status, "setStartRule()");
if (!z.useDaylightTime()) {
errln("Fail: useDaylightTime false with rules set");
}
if (z.getDSTSavings() == 0) {
errln("Fail: dst savings == 0 with rules set");
}
}
/**
* Test to see if DateFormat understands zone equivalency groups. It
* might seem that this should be a DateFormat test, but it's really a
* TimeZone test -- the changes to DateFormat are minor.
*
* We use two known, stable zones that shouldn't change much over time
* -- America/Vancouver and America/Los_Angeles. However, they MAY
* change at some point -- if that happens, replace them with any two
* zones in an equivalency group where one zone has localized name
* data, and the other doesn't, in some locale.
*/
void TimeZoneRegressionTest::TestJ449() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString str;
// Modify the following three as necessary. The two IDs must
// specify two zones in the same equivalency group. One must have
// locale data in 'loc'; the other must not.
const char* idWithLocaleData = "America/Los_Angeles";
const char* idWithoutLocaleData = "US/Pacific";
const Locale loc("en", "", "");
TimeZone *zoneWith = TimeZone::createTimeZone(idWithLocaleData);
TimeZone *zoneWithout = TimeZone::createTimeZone(idWithoutLocaleData);
// Make sure we got valid zones
if (zoneWith->getID(str) != UnicodeString(idWithLocaleData) ||
zoneWithout->getID(str) != UnicodeString(idWithoutLocaleData)) {
dataerrln(UnicodeString("Fail: Unable to create zones - wanted ") + idWithLocaleData + ", got " + zoneWith->getID(str) + ", and wanted " + idWithoutLocaleData + " but got " + zoneWithout->getID(str));
} else {
GregorianCalendar calWith(*zoneWith, status);
GregorianCalendar calWithout(*zoneWithout, status);
SimpleDateFormat fmt("MMM d yyyy hh:mm a zzz", loc, status);
if (U_FAILURE(status)) {
errln("Fail: Unable to create GregorianCalendar/SimpleDateFormat");
} else {
UDate date = 0;
UnicodeString strWith, strWithout;
fmt.setCalendar(calWith);
fmt.format(date, strWith);
fmt.setCalendar(calWithout);
fmt.format(date, strWithout);
if (strWith == strWithout) {
logln(UnicodeString("Ok: ") + idWithLocaleData + " -> " +
strWith + "; " + idWithoutLocaleData + " -> " +
strWithout);
} else {
errln(UnicodeString("FAIL: ") + idWithLocaleData + " -> " +
strWith + "; " + idWithoutLocaleData + " -> " +
strWithout);
}
}
}
delete zoneWith;
delete zoneWithout;
}
// test new API for JDK 1.2 8/31 putback
void
TimeZoneRegressionTest::TestJDK12API()
{
// TimeZone *pst = TimeZone::createTimeZone("PST");
// TimeZone *cst1 = TimeZone::createTimeZone("CST");
UErrorCode ec = U_ZERO_ERROR;
//d,-28800,3,1,-1,120,w,9,-1,1,120,w,60
LocalPointer<TimeZone> pst(new SimpleTimeZone(-28800*U_MILLIS_PER_SECOND,
"PST",
3,1,-1,120*U_MILLIS_PER_MINUTE,
SimpleTimeZone::WALL_TIME,
9,-1,1,120*U_MILLIS_PER_MINUTE,
SimpleTimeZone::WALL_TIME,
60*U_MILLIS_PER_MINUTE,ec), ec);
//d,-21600,3,1,-1,120,w,9,-1,1,120,w,60
LocalPointer<TimeZone> cst1(new SimpleTimeZone(-21600*U_MILLIS_PER_SECOND,
"CST",
3,1,-1,120*U_MILLIS_PER_MINUTE,
SimpleTimeZone::WALL_TIME,
9,-1,1,120*U_MILLIS_PER_MINUTE,
SimpleTimeZone::WALL_TIME,
60*U_MILLIS_PER_MINUTE,ec), ec);
if (U_FAILURE(ec)) {
errln("FAIL: SimpleTimeZone constructor");
return;
}
SimpleTimeZone *cst = dynamic_cast<SimpleTimeZone *>(cst1.getAlias());
if(pst->hasSameRules(*cst)) {
errln("FAILURE: PST and CST have same rules");
}
UErrorCode status = U_ZERO_ERROR;
int32_t offset1 = pst->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (2*60*60*1000), status);
failure(status, "getOffset() failed");
int32_t offset2 = cst->getOffset(1,
1997, UCAL_OCTOBER, 26, UCAL_SUNDAY, (2*60*60*1000), 31, status);
failure(status, "getOffset() failed");
if(offset1 == offset2)
errln("FAILURE: Sunday Oct. 26 1997 2:00 has same offset for PST and CST");
// verify error checking
pst->getOffset(1,
1997, UCAL_FIELD_COUNT+1, 26, UCAL_SUNDAY, (2*60*60*1000), status);
if(U_SUCCESS(status))
errln("FAILURE: getOffset() succeeded with -1 for month");
status = U_ZERO_ERROR;
cst->setDSTSavings(60*60*1000, status);
failure(status, "setDSTSavings() failed");
int32_t savings = cst->getDSTSavings();
if(savings != 60*60*1000) {
errln("setDSTSavings() failed");
}
}
/**
* SimpleTimeZone allows invalid DOM values.
*/
void TimeZoneRegressionTest::Test4184229() {
SimpleTimeZone* zone = nullptr;
UErrorCode status = U_ZERO_ERROR;
zone = new SimpleTimeZone(0, "A", 0, -1, 0, 0, 0, 0, 0, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 startDay");
}else{
logln("(a) " + UnicodeString( u_errorName(status)));
}
status = U_ZERO_ERROR;
delete zone;
zone = new SimpleTimeZone(0, "A", 0, 0, 0, 0, 0, -1, 0, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 endDay");
}else{
logln("(b) " + UnicodeString(u_errorName(status)));
}
status = U_ZERO_ERROR;
delete zone;
zone = new SimpleTimeZone(0, "A", 0, -1, 0, 0, 0, 0, 0, 1000, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 startDay+savings");
}else{
logln("(c) " + UnicodeString(u_errorName(status)));
}
status = U_ZERO_ERROR;
delete zone;
zone = new SimpleTimeZone(0, "A", 0, 0, 0, 0, 0, -1, 0, 0, 1000, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 endDay+ savings");
}else{
logln("(d) " + UnicodeString(u_errorName(status)));
}
status = U_ZERO_ERROR;
delete zone;
// Make a valid constructor call for subsequent tests.
zone = new SimpleTimeZone(0, "A", 0, 1, 0, 0, 0, 1, 0, 0, status);
zone->setStartRule(0, -1, 0, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 setStartRule +savings");
} else{
logln("(e) " + UnicodeString(u_errorName(status)));
}
zone->setStartRule(0, -1, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 setStartRule");
} else{
logln("(f) " + UnicodeString(u_errorName(status)));
}
zone->setEndRule(0, -1, 0, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 setEndRule+savings");
} else{
logln("(g) " + UnicodeString(u_errorName(status)));
}
zone->setEndRule(0, -1, 0, status);
if(U_SUCCESS(status)){
errln("Failed. No exception has been thrown for DOM -1 setEndRule");
} else{
logln("(h) " + UnicodeString(u_errorName(status)));
}
delete zone;
}
void TimeZoneRegressionTest::TestNegativeDaylightSaving() {
UErrorCode status = U_ZERO_ERROR;
int32_t stdOff = 1 * 60*60*1000; // Standard offset UTC+1
int save = -1 * 60*60*1000; // DST saving amount -1 hour
SimpleTimeZone stzDublin(stdOff, "Dublin-2018",
UCAL_OCTOBER, -1, -UCAL_SUNDAY, 2*60*60*1000,
UCAL_MARCH, -1, -UCAL_SUNDAY, 1*60*60*1000,
save, status);
failure(status, "SimpleTimeZone constructor");
if (save != stzDublin.getDSTSavings()) {
errln(UnicodeString("FAIL: DST saving is not ") + save);
}
GregorianCalendar cal(* TimeZone::getGMT(), status);
failure(status, "GregorianCalendar constructor");
UDate testDate;
int32_t rawOffset;
int32_t dstOffset;
cal.set(2018, UCAL_JANUARY, 15, 0, 0, 0);
testDate = cal.getTime(status);
failure(status, "calendar getTime() - Jan 15");
if (!stzDublin.inDaylightTime(testDate, status)) {
errln("FAIL: The test date (Jan 15) must be in DST.");
}
failure(status, "inDaylightTime() - Jan 15");
stzDublin.getOffset(testDate, false, rawOffset, dstOffset, status);
failure(status, "getOffset() - Jan 15");
if (rawOffset != stdOff || dstOffset != save) {
errln(UnicodeString("FAIL: Expected [stdoff=") + stdOff + ",save=" + save
+ "] on the test date (Jan 15), actual[stdoff=" + rawOffset
+ ",save=" + dstOffset + "]");
}
cal.set(2018, UCAL_JULY, 15, 0, 0, 0);
testDate = cal.getTime(status);
failure(status, "calendar getTime() - Jul 15");
if (stzDublin.inDaylightTime(testDate, status)) {
errln("FAIL: The test date (Jul 15) must be in DST.");
}
failure(status, "inDaylightTime() - Jul 15");
stzDublin.getOffset(testDate, false, rawOffset, dstOffset, status);
failure(status, "getOffset() - Jul 15");
if (rawOffset != stdOff || dstOffset != 0) {
errln(UnicodeString("FAIL: Expected [stdoff=") + stdOff + ",save=" + 0
+ "] on the test date (Jul 15), actual[stdoff=" + rawOffset
+ ",save=" + dstOffset + "]");
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|