Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

APPLIED: Patch for ThreeTen/threeten#262

View ThreeTen262
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
diff -r 05525f23180e src/share/classes/java/time/ZoneId.java
--- a/src/share/classes/java/time/ZoneId.java Fri Feb 08 09:11:51 2013 -0800
+++ b/src/share/classes/java/time/ZoneId.java Mon Feb 11 20:14:51 2013 +0000
@@ -93,6 +93,8 @@
* the offset from UTC/Greenwich apply
* </ul><p>
* Most fixed offsets are represented by {@link ZoneOffset}.
+ * Calling {@link #normalized()} on any {@code ZoneId} will ensure that a
+ * fixed offset ID will be represented as a {@code ZoneOffset}.
* <p>
* The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
* This class is simply an ID used to obtain the underlying rules.
@@ -103,28 +105,29 @@
* the ID, whereas serializing the rules sends the entire data set.
* Similarly, a comparison of two IDs only examines the ID, whereas
* a comparison of two rules examines the entire data set.
- * <p>
- * The code supports loading a {@code ZoneId} on a JVM which does not have available rules
- * for that ID. This allows the date-time object, such as {@link ZonedDateTime},
- * to still be queried.
*
* <h3>Time-zone IDs</h3>
* The ID is unique within the system.
- * The formats for offset and region IDs differ.
+ * There are three types of ID.
* <p>
- * An ID is parsed as an offset ID if it starts with 'UTC', 'GMT', 'UT' '+' or '-', or
- * is a single letter. For example, 'Z', '+02:00', '-05:00', 'UTC+05', 'GMT-6' and
- * 'UT+01:00' are all valid offset IDs.
- * Note that some IDs, such as 'D' or '+ABC' meet the criteria to be parsed as offset IDs,
- * but have an invalid offset.
+ * The simplest type of ID is that from {@code ZoneOffset}.
+ * This consists of 'Z' and IDs starting with '+' or '-'.
* <p>
- * All other IDs are considered to be region IDs.
+ * The next type of ID are offset-style IDs with some form of prefix,
+ * such as 'GMT+2' or 'UTC+01:00'.
+ * The recognised prefixes are 'UTC', 'GMT' and 'UT'.
+ * The offset is the suffix and will be normalized during creation.
+ * These IDs can be normalized to a {@code ZoneOffset} using {@code normalized()}.
* <p>
- * Region IDs are defined by configuration, which can be thought of as a {@code Map}
- * from region ID to {@code ZoneRules}, see {@link ZoneRulesProvider}.
+ * The third type of ID are region-based IDs. A region-based ID must be of
+ * two or more characters, and not start with 'UTC', 'GMT', 'UT' '+' or '-'.
+ * Region-based IDs are defined by configuration, see {@link ZoneRulesProvider}.
+ * The configuration focuses on providing the lookup from the ID to the
+ * underlying {@code ZoneRules}.
* <p>
- * Time-zones are defined by governments and change frequently. There are a number of
- * organizations, known here as groups, that monitor time-zone changes and collate them.
+ * Time-zone rules are defined by governments and change frequently.
+ * There are a number of organizations, known here as groups, that monitor
+ * time-zone changes and collate them.
* The default group is the IANA Time Zone Database (TZDB).
* Other organizations include IATA (the airline industry body) and Microsoft.
* <p>
@@ -139,6 +142,20 @@
* The recommended format for region IDs from groups other than TZDB is 'group~region'.
* Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.
*
+ * <h3>Serialization</h3>
+ * This class can be serialized and stores the string zone ID in the external form.
+ * The {@code ZoneOffset} subclass uses a dedicated format that only stores the
+ * offset from UTC/Greenwich.
+ * <p>
+ * A {@code ZoneId} can be deserialized in a JVM where the ID is unknown.
+ * For example, if a server-side JVM has been updated with a new zone ID, but
+ * the client-side JVM has not been updated. In this case, the {@code ZoneId}
+ * object will exist, and can be queried using {@code getId}, {@code equals},
+ * {@code hashCode}, {@code toString}, {@code getDisplayName} and {@code normalized}.
+ * However, any call to {@code getRules} will fail with {@code ZoneRulesException}.
+ * This approach is designed to allow a {@link ZonedDateTime} to be loaded and
+ * queried, but not modified, on a JVM with incomplete time-zone information.
+ *
* <h3>Specification for implementors</h3>
* This abstract class has two implementations, both of which are immutable and thread-safe.
* One implementation models region-based IDs, the other is {@code ZoneOffset} modelling
@@ -310,31 +327,36 @@
* Obtains an instance of {@code ZoneId} from an ID ensuring that the
* ID is valid and available for use.
* <p>
- * This method parses the ID, applies any appropriate normalization, and validates it
- * against the known set of IDs for which rules are available.
+ * This method parses the ID producing a {@code ZoneId} or {@code ZoneOffset}.
+ * A {@code ZoneOffset} is returned if the ID is 'Z', or starts with '+' or '-'.
+ * The result will always be a valid ID for which {@link ZoneRules} can be obtained.
* <p>
- * An ID is parsed as though it is an offset ID if it starts with 'UTC', 'GMT', 'UT', '+'
- * or '-', or if it has less then two letters.
- * The offset of {@linkplain ZoneOffset#UTC zero} may be represented in multiple ways,
- * including 'Z', 'UTC', 'GMT', 'UT', 'UTC0', 'GMT0', 'UT0', '+00:00', '-00:00' and 'UTC+00:00'.
- * <p>
- * Six forms of ID are recognized:
- * <p><ul>
- * <li><code>Z</code> - an offset of zero, which is {@code ZoneOffset.UTC}
- * <li><code>{offset}</code> - a {@code ZoneOffset} ID, such as '+02:00'
- * <li><code>{utcPrefix}</code> - a {@code ZoneOffset} ID equal to 'Z'
- * <li><code>{utcPrefix}0</code> - a {@code ZoneOffset} ID equal to 'Z'
- * <li><code>{utcPrefix}{offset}</code> - a {@code ZoneOffset} ID equal to '{offset}'
- * <li><code>{regionID}</code> - full region ID, loaded from configuration
- * </ul><p>
- * The {offset} is a valid format for {@link ZoneOffset#of(String)}, excluding 'Z'.
- * The {utcPrefix} is 'UTC', 'GMT' or 'UT'.
- * Region IDs must match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>.
- * <p>
- * The detailed format of the region ID depends on the group supplying the data.
- * The default set of data is supplied by the IANA Time Zone Database (TZDB)
- * This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
- * This is compatible with most IDs from {@link java.util.TimeZone}.
+ * Parsing matches the zone ID step by step as follows.
+ * <ul>
+ * <li>If the zone ID equals 'Z', the result is {@code ZoneOffset.UTC}.
+ * <li>If the zone ID consists of a single letter, the zone ID is invalid
+ * and {@code DateTimeException} is thrown.
+ * <li>If the zone ID starts with '+' or '-', the ID is parsed as a
+ * {@code ZoneOffset} using {@link ZoneOffset#of(String)}.
+ * <li>If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a {@code ZoneId}
+ * with the same ID and rules equivalent to {@code ZoneOffset.UTC}.
+ * <li>If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-'
+ * then the ID is a prefixed offset-based ID. The ID is split in two, with
+ * a two or three letter prefix and a suffix starting with the sign.
+ * The suffix is parsed as a {@link ZoneOffset#of(String) ZoneOffset}.
+ * The result will be a {@code ZoneId} with the specified UTC/GMT/UT prefix
+ * and the normalized offset ID as per {@link ZoneOffset#getId()}.
+ * The rules of the returned {@code ZoneId} will be equivalent to the
+ * parsed {@code ZoneOffset}.
+ * <li>All other IDs are parsed as region-based zone IDs. Region IDs must
+ * match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>
+ * otherwise a {@code DateTimeException} is thrown. If the zone ID is not
+ * in the configured set of IDs, {@code ZoneRulesException} is thrown.
+ * The detailed format of the region ID depends on the group supplying the data.
+ * The default set of data is supplied by the IANA Time Zone Database (TZDB)
+ * This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
+ * This is compatible with most IDs from {@link java.util.TimeZone}.
+ * </ul>
*
* @param zoneId the time-zone ID, not null
* @return the zone ID, not null
@@ -342,15 +364,29 @@
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
*/
public static ZoneId of(String zoneId) {
+ return of(zoneId, true);
+ }
+
+ /**
+ * Parses the ID, taking a flag to indicate whether {@code ZoneRulesException}
+ * should be thrown or not, used in deserialization.
+ *
+ * @param zoneId the time-zone ID, not null
+ * @param checkAvailable whether to check if the zone ID is available
+ * @return the zone ID, not null
+ * @throws DateTimeException if the ID format is invalid
+ * @throws ZoneRulesException if checking availability and the ID cannot be found
+ */
+ static ZoneId of(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");
if (zoneId.length() <= 1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {
return ZoneOffset.of(zoneId);
} else if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
- return ofWithPrefix(zoneId, 3);
+ return ofWithPrefix(zoneId, 3, checkAvailable);
} else if (zoneId.startsWith("UT")) {
- return ofWithPrefix(zoneId, 2);
+ return ofWithPrefix(zoneId, 2, checkAvailable);
}
- return ZoneRegion.ofId(zoneId, true);
+ return ZoneRegion.ofId(zoneId, checkAvailable);
}
/**
@@ -359,22 +395,25 @@
* @param zoneId the time-zone ID, not null
* @param prefixLength the length of the prefix, 2 or 3
* @return the zone ID, not null
- * @return the zone ID, not null
* @throws DateTimeException if the zone ID has an invalid format
*/
- private static ZoneId ofWithPrefix(String zoneId, int prefixLength) {
- if (zoneId.length() == prefixLength ||
- (zoneId.length() == prefixLength + 1 && zoneId.charAt(prefixLength) == '0')) {
- return ZoneOffset.UTC;
+ private static ZoneId ofWithPrefix(String zoneId, int prefixLength, boolean checkAvailable) {
+ String prefix = zoneId.substring(0, prefixLength);
+ if (zoneId.length() == prefixLength) {
+ return ZoneRegion.ofPrefixedOffset(prefix, ZoneOffset.UTC);
}
- if (zoneId.charAt(prefixLength) == '+' || zoneId.charAt(prefixLength) == '-') {
- try {
- return ZoneOffset.of(zoneId.substring(prefixLength));
- } catch (DateTimeException ex) {
- throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);
+ if (zoneId.charAt(prefixLength) != '+' && zoneId.charAt(prefixLength) != '-') {
+ return ZoneRegion.ofId(zoneId, checkAvailable); // drop through to ZoneRulesProvider
+ }
+ try {
+ ZoneOffset offset = ZoneOffset.of(zoneId.substring(prefixLength));
+ if (offset == ZoneOffset.UTC) {
+ return ZoneRegion.ofPrefixedOffset(prefix, offset);
}
+ return ZoneRegion.ofPrefixedOffset(prefix + offset.toString(), offset);
+ } catch (DateTimeException ex) {
+ throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);
}
- throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId);
}
//-----------------------------------------------------------------------
@@ -488,6 +527,32 @@
//-----------------------------------------------------------------------
/**
+ * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
+ * <p>
+ * The returns a normalized {@code ZoneId} that can be used in place of this ID.
+ * The result will have {@code ZoneRules} equivalent to those returned by this object,
+ * however the ID returned by {@code getId()} may be different.
+ * <p>
+ * The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
+ * If they do, then the {@code ZoneOffset} equal to that offset is returned.
+ * Otherwise {@code this} is returned.
+ *
+ * @return the time-zone unique ID, not null
+ */
+ public ZoneId normalized() {
+ try {
+ ZoneRules rules = getRules();
+ if (rules.isFixedOffset()) {
+ return rules.getOffset(Instant.EPOCH);
+ }
+ } catch (ZoneRulesException ex) {
+ // invalid ZoneRegion is not important to this method
+ }
+ return this;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Checks if this time-zone ID is equal to another time-zone ID.
* <p>
* The comparison is based on the ID.
@@ -536,6 +601,10 @@
* out.writeByte(7); // identifies this as a ZoneId (not ZoneOffset)
* out.writeUTF(zoneId);
* </pre>
+ * <p>
+ * When read back in, the {@code ZoneId} will be created as though using
+ * {@link #of(String)}, but without any exception in the case where the
+ * ID has a valid format, but is not in the known set of region-based IDs.
*
* @return the instance of {@code Ser}, not null
*/
diff -r 05525f23180e src/share/classes/java/time/ZoneRegion.java
--- a/src/share/classes/java/time/ZoneRegion.java Fri Feb 08 09:11:51 2013 -0800
+++ b/src/share/classes/java/time/ZoneRegion.java Mon Feb 11 20:14:51 2013 +0000
@@ -109,26 +109,6 @@
private final transient ZoneRules rules;
/**
- * Obtains an instance of {@code ZoneRegion} from an identifier without checking
- * if the time-zone has available rules.
- * <p>
- * This method parses the ID and applies any appropriate normalization.
- * It does not validate the ID against the known set of IDs for which rules are available.
- * <p>
- * This method is intended for advanced use cases.
- * For example, consider a system that always retrieves time-zone rules from a remote server.
- * Using this factory would allow a {@code ZoneRegion}, and thus a {@code ZonedDateTime},
- * to be created without loading the rules from the remote server.
- *
- * @param zoneId the time-zone ID, not null
- * @return the zone ID, not null
- * @throws DateTimeException if the ID format is invalid
- */
- private static ZoneRegion ofLenient(String zoneId) {
- return ofId(zoneId, false);
- }
-
- /**
* Obtains an instance of {@code ZoneId} from an identifier.
*
* @param zoneId the time-zone ID, not null
@@ -139,10 +119,7 @@
*/
static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");
- if (zoneId.length() < 2 ||
- zoneId.startsWith("UT") || // includes UTC
- zoneId.startsWith("GMT") ||
- (PATTERN.matcher(zoneId).matches() == false)) {
+ if (zoneId.length() < 2 || PATTERN.matcher(zoneId).matches() == false) {
throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
}
ZoneRules rules = null;
@@ -157,6 +134,19 @@
return new ZoneRegion(zoneId, rules);
}
+ /**
+ * Obtains an instance of {@code ZoneId} wrapping an offset.
+ * <p>
+ * For example, zone IDs like 'UTC', 'GMT', 'UT' and 'UTC+01:30' will be setup here.
+ *
+ * @param zoneId the time-zone ID, not null
+ * @param offset the offset, not null
+ * @return the zone ID, not null
+ */
+ static ZoneRegion ofPrefixedOffset(String zoneId, ZoneOffset offset) {
+ return new ZoneRegion(zoneId, offset.getRules());
+ }
+
//-------------------------------------------------------------------------
/**
* Constructor.
@@ -218,7 +208,7 @@
static ZoneId readExternal(DataInput in) throws IOException {
String id = in.readUTF();
- return ofLenient(id);
+ return ZoneId.of(id, false);
}
}
diff -r 05525f23180e test/java/time/tck/java/time/TCKZoneId.java
--- a/test/java/time/tck/java/time/TCKZoneId.java Fri Feb 08 09:11:51 2013 -0800
+++ b/test/java/time/tck/java/time/TCKZoneId.java Mon Feb 11 20:14:51 2013 +0000
@@ -64,22 +64,23 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
import java.io.DataOutputStream;
-import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamConstants;
import java.lang.reflect.Field;
import java.time.DateTimeException;
+import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
+import java.time.format.TextStyle;
import java.time.temporal.Queries;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQuery;
import java.time.zone.ZoneRulesException;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
import org.testng.annotations.DataProvider;
@@ -114,9 +115,12 @@
// an ID can be loaded without validation during deserialization
String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
ZoneId deser = deserialize(id);
- // getting the ID and string are OK
+ // getId, equals, hashCode, toString and normalized are OK
assertEquals(deser.getId(), id);
assertEquals(deser.toString(), id);
+ assertEquals(deser, deser);
+ assertEquals(deser.hashCode(), deser.hashCode());
+ assertEquals(deser.normalized(), deser);
// getting the rules is not
try {
deser.getRules();
@@ -133,32 +137,32 @@
deserialize("|!?");
}
- @Test(dataProvider="offsetBasedValid", expectedExceptions=DateTimeException.class)
+ @Test(dataProvider="offsetBasedValid")
public void test_deserialization_lenient_offsetNotAllowed_noPrefix(String input, String resolvedId) throws Exception {
- // an ID can be loaded without validation during deserialization
- // but there is a check to ensure the ID format is valid
- deserialize(input);
+ ZoneId deserialized = deserialize(input);
+ assertEquals(deserialized, ZoneId.of(input));
+ assertEquals(deserialized, ZoneId.of(resolvedId));
}
- @Test(dataProvider="offsetBasedValid", expectedExceptions=DateTimeException.class)
- public void test_deserialization_lenient_offsetNotAllowed_prefixUTC(String input, String resolvedId) throws Exception {
- // an ID can be loaded without validation during deserialization
- // but there is a check to ensure the ID format is valid
- deserialize("UTC" + input);
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void test_deserialization_lenient_offsetNotAllowed_prefixUTC(String input, String resolvedId, String offsetId) throws Exception {
+ ZoneId deserialized = deserialize("UTC" + input);
+ assertEquals(deserialized, ZoneId.of("UTC" + input));
+ assertEquals(deserialized, ZoneId.of("UTC" + resolvedId));
}
- @Test(dataProvider="offsetBasedValid", expectedExceptions=DateTimeException.class)
- public void test_deserialization_lenient_offsetNotAllowed_prefixGMT(String input, String resolvedId) throws Exception {
- // an ID can be loaded without validation during deserialization
- // but there is a check to ensure the ID format is valid
- deserialize("GMT" + input);
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void test_deserialization_lenient_offsetNotAllowed_prefixGMT(String input, String resolvedId, String offsetId) throws Exception {
+ ZoneId deserialized = deserialize("GMT" + input);
+ assertEquals(deserialized, ZoneId.of("GMT" + input));
+ assertEquals(deserialized, ZoneId.of("GMT" + resolvedId));
}
- @Test(dataProvider="offsetBasedValid", expectedExceptions=DateTimeException.class)
- public void test_deserialization_lenient_offsetNotAllowed_prefixUT(String input, String resolvedId) throws Exception {
- // an ID can be loaded without validation during deserialization
- // but there is a check to ensure the ID format is valid
- deserialize("UT" + input);
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void test_deserialization_lenient_offsetNotAllowed_prefixUT(String input, String resolvedId, String offsetId) throws Exception {
+ ZoneId deserialized = deserialize("UT" + input);
+ assertEquals(deserialized, ZoneId.of("UT" + input));
+ assertEquals(deserialized, ZoneId.of("UT" + resolvedId));
}
private ZoneId deserialize(String id) throws Exception {
@@ -315,65 +319,41 @@
}
//-----------------------------------------------------------------------
- // regular factory
- //-----------------------------------------------------------------------
- @DataProvider(name="offsetBasedZero")
- Object[][] data_offsetBasedZero() {
- return new Object[][] {
- {""}, {"0"},
- {"+00"},{"+0000"},{"+00:00"},{"+000000"},{"+00:00:00"},
- {"-00"},{"-0000"},{"-00:00"},{"-000000"},{"-00:00:00"},
- };
- }
-
- @Test(dataProvider="offsetBasedZero")
- public void factory_of_String_offsetBasedZero_noPrefix(String id) {
- if (id.length() > 0 && id.equals("0") == false) {
- ZoneId test = ZoneId.of(id);
- assertEquals(test, ZoneOffset.UTC);
- }
- }
-
- @Test(dataProvider="offsetBasedZero")
- public void factory_of_String_offsetBasedZero_prefixUTC(String id) {
- ZoneId test = ZoneId.of("UTC" + id);
- assertEquals(test, ZoneOffset.UTC);
- }
-
- @Test(dataProvider="offsetBasedZero")
- public void factory_of_String_offsetBasedZero_prefixGMT(String id) {
- ZoneId test = ZoneId.of("GMT" + id);
- assertEquals(test, ZoneOffset.UTC);
- }
-
- @Test(dataProvider="offsetBasedZero")
- public void factory_of_String_offsetBasedZero_prefixUT(String id) {
- ZoneId test = ZoneId.of("UT" + id);
- assertEquals(test, ZoneOffset.UTC);
- }
-
- @Test
- public void factory_of_String_offsetBasedZero_z() {
- ZoneId test = ZoneId.of("Z");
- assertEquals(test, ZoneOffset.UTC);
- }
-
+ // regular factory and .normalized()
//-----------------------------------------------------------------------
@DataProvider(name="offsetBasedValid")
Object[][] data_offsetBasedValid() {
return new Object[][] {
+ {"Z", "Z"},
{"+0", "Z"},
+ {"-0", "Z"},
+ {"+00", "Z"},
+ {"+0000", "Z"},
+ {"+00:00", "Z"},
+ {"+000000", "Z"},
+ {"+00:00:00", "Z"},
+ {"-00", "Z"},
+ {"-0000", "Z"},
+ {"-00:00", "Z"},
+ {"-000000", "Z"},
+ {"-00:00:00", "Z"},
{"+5", "+05:00"},
{"+01", "+01:00"},
- {"+0100", "+01:00"},{"+01:00", "+01:00"},
- {"+010000", "+01:00"},{"+01:00:00", "+01:00"},
+ {"+0100", "+01:00"},
+ {"+01:00", "+01:00"},
+ {"+010000", "+01:00"},
+ {"+01:00:00", "+01:00"},
{"+12", "+12:00"},
- {"+1234", "+12:34"},{"+12:34", "+12:34"},
- {"+123456", "+12:34:56"},{"+12:34:56", "+12:34:56"},
+ {"+1234", "+12:34"},
+ {"+12:34", "+12:34"},
+ {"+123456", "+12:34:56"},
+ {"+12:34:56", "+12:34:56"},
{"-02", "-02:00"},
{"-5", "-05:00"},
- {"-0200", "-02:00"},{"-02:00", "-02:00"},
- {"-020000", "-02:00"},{"-02:00:00", "-02:00"},
+ {"-0200", "-02:00"},
+ {"-02:00", "-02:00"},
+ {"-020000", "-02:00"},
+ {"-02:00:00", "-02:00"},
};
}
@@ -382,27 +362,126 @@
ZoneId test = ZoneId.of(input);
assertEquals(test.getId(), id);
assertEquals(test, ZoneOffset.of(id));
+ assertEquals(test.normalized(), ZoneOffset.of(id));
+ assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
+ assertEquals(test.getRules().isFixedOffset(), true);
+ assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(id));
}
- @Test(dataProvider="offsetBasedValid")
- public void factory_of_String_offsetBasedValid_prefixUTC(String input, String id) {
- ZoneId test = ZoneId.of("UTC" + input);
- assertEquals(test.getId(), id);
- assertEquals(test, ZoneOffset.of(id));
+ //-----------------------------------------------------------------------
+ @DataProvider(name="offsetBasedValidPrefix")
+ Object[][] data_offsetBasedValidPrefix() {
+ return new Object[][] {
+ {"", "", "Z"},
+ {"+0", "", "Z"},
+ {"-0", "", "Z"},
+ {"+00", "", "Z"},
+ {"+0000", "", "Z"},
+ {"+00:00", "", "Z"},
+ {"+000000", "", "Z"},
+ {"+00:00:00", "", "Z"},
+ {"-00", "", "Z"},
+ {"-0000", "", "Z"},
+ {"-00:00", "", "Z"},
+ {"-000000", "", "Z"},
+ {"-00:00:00", "", "Z"},
+ {"+5", "+05:00", "+05:00"},
+ {"+01", "+01:00", "+01:00"},
+ {"+0100", "+01:00", "+01:00"},
+ {"+01:00", "+01:00", "+01:00"},
+ {"+010000", "+01:00", "+01:00"},
+ {"+01:00:00", "+01:00", "+01:00"},
+ {"+12", "+12:00", "+12:00"},
+ {"+1234", "+12:34", "+12:34"},
+ {"+12:34", "+12:34", "+12:34"},
+ {"+123456", "+12:34:56", "+12:34:56"},
+ {"+12:34:56", "+12:34:56", "+12:34:56"},
+ {"-02", "-02:00", "-02:00"},
+ {"-5", "-05:00", "-05:00"},
+ {"-0200", "-02:00", "-02:00"},
+ {"-02:00", "-02:00", "-02:00"},
+ {"-020000", "-02:00", "-02:00"},
+ {"-02:00:00", "-02:00", "-02:00"},
+ };
}
- @Test(dataProvider="offsetBasedValid")
- public void factory_of_String_offsetBasedValid_prefixGMT(String input, String id) {
- ZoneId test = ZoneId.of("GMT" + input);
- assertEquals(test.getId(), id);
- assertEquals(test, ZoneOffset.of(id));
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void factory_of_String_offsetBasedValid_prefixUTC(String input, String id, String offsetId) {
+ ZoneId test = ZoneId.of("UTC" + input);
+ assertEquals(test.getId(), "UTC" + id);
+ assertEquals(test.getRules(), ZoneOffset.of(offsetId).getRules());
+ assertEquals(test.normalized(), ZoneOffset.of(offsetId));
+ assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), displayName("UTC" + id));
+ assertEquals(test.getRules().isFixedOffset(), true);
+ assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(offsetId));
}
- @Test(dataProvider="offsetBasedValid")
- public void factory_of_String_offsetBasedValid_prefixUT(String input, String id) {
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void factory_of_String_offsetBasedValid_prefixGMT(String input, String id, String offsetId) {
+ ZoneId test = ZoneId.of("GMT" + input);
+ assertEquals(test.getId(), "GMT" + id);
+ assertEquals(test.getRules(), ZoneOffset.of(offsetId).getRules());
+ assertEquals(test.normalized(), ZoneOffset.of(offsetId));
+ assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), displayName("GMT" + id));
+ assertEquals(test.getRules().isFixedOffset(), true);
+ assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(offsetId));
+ }
+
+ @Test(dataProvider="offsetBasedValidPrefix")
+ public void factory_of_String_offsetBasedValid_prefixUT(String input, String id, String offsetId) {
ZoneId test = ZoneId.of("UT" + input);
- assertEquals(test.getId(), id);
- assertEquals(test, ZoneOffset.of(id));
+ assertEquals(test.getId(), "UT" + id);
+ assertEquals(test.getRules(), ZoneOffset.of(offsetId).getRules());
+ assertEquals(test.normalized(), ZoneOffset.of(offsetId));
+ assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), displayName("UT" + id));
+ assertEquals(test.getRules().isFixedOffset(), true);
+ assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(offsetId));
+ }
+
+ private String displayName(String id) {
+ if (id.equals("GMT")) {
+ return "Greenwich Mean Time";
+ }
+ if (id.equals("GMT0")) {
+ return "Greenwich Mean Time";
+ }
+ if (id.equals("UTC")) {
+ return "Coordinated Universal Time";
+ }
+ return id;
+ }
+
+ //-----------------------------------------------------------------------
+ @DataProvider(name="offsetBasedValidOther")
+ Object[][] data_offsetBasedValidOther() {
+ return new Object[][] {
+ {"GMT", "Z"},
+ {"GMT0", "Z"},
+ {"UCT", "Z"},
+ {"Greenwich", "Z"},
+ {"Universal", "Z"},
+ {"Zulu", "Z"},
+ {"Etc/GMT", "Z"},
+ {"Etc/GMT+0", "Z"},
+ {"Etc/GMT+1", "-01:00"},
+ {"Etc/GMT-1", "+01:00"},
+ {"Etc/GMT+9", "-09:00"},
+ {"Etc/GMT-9", "+09:00"},
+ {"Etc/GMT0", "Z"},
+ {"Etc/UCT", "Z"},
+ {"Etc/UTC", "Z"},
+ {"Etc/Greenwich", "Z"},
+ {"Etc/Universal", "Z"},
+ {"Etc/Zulu", "Z"},
+ };
+ }
+
+ @Test(dataProvider="offsetBasedValidOther")
+ public void factory_of_String_offsetBasedValidOther(String input, String offsetId) {
+ ZoneId test = ZoneId.of(input);
+ assertEquals(test.getId(), input);
+ assertEquals(test.getRules(), ZoneOffset.of(offsetId).getRules());
+ assertEquals(test.normalized(), ZoneOffset.of(offsetId));
}
//-----------------------------------------------------------------------
@@ -422,6 +501,12 @@
{"-19"}, {"-19:00"}, {"-18:01"}, {"-18:00:01"}, {"-1801"}, {"-180001"},
{"-01_00"}, {"-01;00"}, {"-01@00"}, {"-01:AA"},
{"@01:00"},
+ {"0"},
+ {"UT0"},
+ {"UTZ"},
+ {"UTC0"},
+ {"UTCZ"},
+ {"GMTZ"}, // GMT0 is valid in ZoneRulesProvider
};
}
@@ -440,6 +525,9 @@
@Test(dataProvider="offsetBasedInvalid", expectedExceptions=DateTimeException.class)
public void factory_of_String_offsetBasedInvalid_prefixGMT(String id) {
+ if (id.equals("0")) {
+ throw new DateTimeException("Fake exception: GMT0 is valid, not invalid");
+ }
ZoneId.of("GMT" + id);
}
@@ -479,6 +567,7 @@
ZoneId test = ZoneId.of("Europe/London");
assertEquals(test.getId(), "Europe/London");
assertEquals(test.getRules().isFixedOffset(), false);
+ assertEquals(test.normalized(), test);
}
//-----------------------------------------------------------------------
@@ -578,8 +667,10 @@
{"Europe/London", "Europe/London"},
{"Europe/Paris", "Europe/Paris"},
{"Europe/Berlin", "Europe/Berlin"},
- {"UTC", "Z"},
- {"UTC+01:00", "+01:00"},
+ {"Z", "Z"},
+ {"+01:00", "+01:00"},
+ {"UTC", "UTC"},
+ {"UTC+01:00", "UTC+01:00"},
};
}
diff -r 05525f23180e test/java/time/test/java/time/TestZoneId.java
--- a/test/java/time/test/java/time/TestZoneId.java Fri Feb 08 09:11:51 2013 -0800
+++ b/test/java/time/test/java/time/TestZoneId.java Mon Feb 11 20:14:51 2013 +0000
@@ -62,7 +62,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import java.lang.reflect.Field;
@@ -82,7 +81,6 @@
import java.util.SimpleTimeZone;
import java.util.TimeZone;
-import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
@@ -121,7 +119,6 @@
assertEquals(test.getRules().isFixedOffset(), true);
assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), ZoneOffset.UTC);
checkOffset(test.getRules(), createLDT(2008, 6, 30), ZoneOffset.UTC, 1);
- assertSame(test, ZoneId.of("UTC+00"));
}
//-----------------------------------------------------------------------
@@ -129,9 +126,7 @@
//-----------------------------------------------------------------------
public void test_systemDefault() {
ZoneId test = ZoneId.systemDefault();
- assertEquals(test.getId(), TimeZone.getDefault()
- .getID()
- .replaceAll("GMT|UTC|UT", "Z"));
+ assertEquals(test.getId(), TimeZone.getDefault().getID());
}
@Test(expectedExceptions = DateTimeException.class)
@@ -157,58 +152,6 @@
}
//-----------------------------------------------------------------------
- @DataProvider(name="String_Fixed")
- Object[][] data_of_string_Fixed() {
- return new Object[][] {
- {"+0", "Z"},
- {"+5", "+05:00"},
- {"+01", "+01:00"},
- {"+0100", "+01:00"},{"+01:00", "+01:00"},
- {"+010000", "+01:00"},{"+01:00:00", "+01:00"},
- {"+12", "+12:00"},
- {"+1234", "+12:34"},{"+12:34", "+12:34"},
- {"+123456", "+12:34:56"},{"+12:34:56", "+12:34:56"},
- {"-02", "-02:00"},
- {"-5", "-05:00"},
- {"-0200", "-02:00"},{"-02:00", "-02:00"},
- {"-020000", "-02:00"},{"-02:00:00", "-02:00"},
- };
- }
-
- @Test(dataProvider="String_Fixed")
- public void test_of_string_offset(String input, String id) {
- ZoneId test = ZoneId.of(input);
- assertEquals(test.getId(), id);
- assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
- assertEquals(test.getRules().isFixedOffset(), true);
- ZoneOffset offset = ZoneOffset.of(id);
- assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
- checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
- }
-
- @Test(dataProvider="String_Fixed")
- public void test_of_string_FixedUTC(String input, String id) {
- ZoneId test = ZoneId.of("UTC" + input);
- assertEquals(test.getId(), id);
- assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
- assertEquals(test.getRules().isFixedOffset(), true);
- ZoneOffset offset = ZoneOffset.of(id);
- assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
- checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
- }
-
- @Test(dataProvider="String_Fixed")
- public void test_of_string_FixedGMT(String input, String id) {
- ZoneId test = ZoneId.of("GMT" + input);
- assertEquals(test.getId(), id);
- assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id);
- assertEquals(test.getRules().isFixedOffset(), true);
- ZoneOffset offset = ZoneOffset.of(id);
- assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), offset);
- checkOffset(test.getRules(), createLDT(2008, 6, 30), offset, 1);
- }
-
- //-----------------------------------------------------------------------
// Europe/London
//-----------------------------------------------------------------------
public void test_London() {
diff -r 05525f23180e test/java/time/test/java/time/format/TestZoneTextPrinterParser.java
--- a/test/java/time/test/java/time/format/TestZoneTextPrinterParser.java Fri Feb 08 09:11:51 2013 -0800
+++ b/test/java/time/test/java/time/format/TestZoneTextPrinterParser.java Mon Feb 11 20:14:51 2013 +0000
@@ -41,6 +41,7 @@
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.time.zone.ZoneRulesProvider;
+import java.util.TreeSet;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -49,7 +50,7 @@
/**
* Test ZoneTextPrinterParser
*/
-@Test(groups={"implementation"})
+@Test
public class TestZoneTextPrinterParser extends AbstractTestPrinterParser {
protected static DateTimeFormatter getFormatter(Locale locale, TextStyle style) {
@@ -70,26 +71,23 @@
zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
.with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
for (String zid : zids) {
- if (zid.equals("ROC") ||
- zid.startsWith("UTC") ||
- zid.startsWith("GMT") || zid.startsWith("Etc/GMT")) {
- // UTC, GMT are treated as zone offset
+ if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
continue; // TBD: match jdk behavior?
}
zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
TimeZone tz = TimeZone.getTimeZone(zid);
boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
for (Locale locale : locales) {
- printText(locale, zdt, TextStyle.FULL,
- tz.getDisplayName(isDST, TimeZone.LONG, locale));
- printText(locale, zdt, TextStyle.SHORT,
- tz.getDisplayName(isDST, TimeZone.SHORT, locale));
+ printText(locale, zdt, TextStyle.FULL, tz,
+ tz.getDisplayName(isDST, TimeZone.LONG, locale));
+ printText(locale, zdt, TextStyle.SHORT, tz,
+ tz.getDisplayName(isDST, TimeZone.SHORT, locale));
}
}
}
}
- private void printText(Locale locale, ZonedDateTime zdt, TextStyle style, String expected) {
+ private void printText(Locale locale, ZonedDateTime zdt, TextStyle style, TimeZone zone, String expected) {
String result = getFormatter(locale, style).format(zdt);
if (!result.equals(expected)) {
if (result.equals("FooLocation")) { // from rules provider test if same vm
@@ -97,8 +95,8 @@
}
System.out.println("----------------");
System.out.printf("tdz[%s]%n", zdt.toString());
- System.out.printf("[%-4s, %5s] :[%s]%n", locale.toString(), style.toString(),result);
- System.out.printf("%4s, %5s :[%s]%n", "", "", expected);
+ System.out.printf("[%-5s, %5s] :[%s]%n", locale.toString(), style.toString(),result);
+ System.out.printf(" %5s, %5s :[%s] %s%n", "", "", expected, zone);
}
assertEquals(result, expected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.