Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Allow ZoneOffset to handle single digit forms like +0, +5, -6
Browse files Browse the repository at this point in the history
Extend support to ZoneId, absorbing previous GMT-0, GMT0 and GMT+0 regions
  • Loading branch information
jodastephen committed Nov 21, 2012
1 parent 18d7e83 commit 1a5baea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
8 changes: 2 additions & 6 deletions src/main/java/javax/time/ZoneId.java
Expand Up @@ -361,7 +361,7 @@ private static ZoneId ofId(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");

// special fixed cases
if (zoneId.equals("UTC") || zoneId.equals("GMT")) {
if (zoneId.equals("UTC") || zoneId.equals("GMT") || zoneId.equals("GMT0")) {
return UTC;
}
if (zoneId.startsWith(GROUP_UTC_COLON)) {
Expand All @@ -372,11 +372,7 @@ private static ZoneId ofId(String zoneId, boolean checkAvailable) {
}
}
if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
try {
return of(ZoneOffset.of(zoneId.substring(3)));
} catch (IllegalArgumentException ex) {
// continue, in case it is something like GMT0, GMT+0, GMT-0
}
return of(ZoneOffset.of(zoneId.substring(3)));
}

// normal non-fixed IDs
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/javax/time/ZoneOffset.java
Expand Up @@ -133,6 +133,8 @@ public final class ZoneOffset
* {@link #getId()}, plus some additional formats:
* <p><ul>
* <li>{@code Z} - for UTC
* <li>{@code +h}
* <li>{@code +hh}
* <li>{@code +hh:mm}
* <li>{@code -hh:mm}
* <li>{@code +hhmm}
Expand Down Expand Up @@ -161,10 +163,11 @@ public static ZoneOffset of(String offsetId) {
return offset;
}

// parse - +hh, +hhmm, +hh:mm, +hhmmss, +hh:mm:ss
// parse - +h, +hh, +hhmm, +hh:mm, +hhmmss, +hh:mm:ss
final int hours, minutes, seconds;
int len = offsetId.length();
switch (len) {
switch (offsetId.length()) {
case 2:
offsetId = offsetId.charAt(0) + "0" + offsetId.charAt(1); // fallthru
case 3:
hours = parseNumber(offsetId, 1, false);
minutes = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/javax/time/zone/TZDBZoneRulesCompiler.java
Expand Up @@ -921,6 +921,9 @@ private void buildZoneRules() throws Exception {
// remove UTC and GMT
builtZones.remove("UTC");
builtZones.remove("GMT");
builtZones.remove("GMT0");
builtZones.remove("GMT+0");
builtZones.remove("GMT-0");
}

//-----------------------------------------------------------------------
Expand Down
17 changes: 13 additions & 4 deletions src/tck/java/javax/time/TCKZoneOffset.java
Expand Up @@ -133,7 +133,7 @@ public void test_constant_UTC() {
@Test(groups={"tck"})
public void test_factory_string_UTC() {
String[] values = new String[] {
"Z",
"Z", "+0",
"+00","+0000","+00:00","+000000","+00:00:00",
"-00","-0000","-00:00","-000000","-00:00:00",
};
Expand All @@ -148,12 +148,12 @@ public void test_factory_string_invalid() {
String[] values = new String[] {
"","A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","ZZ",
"+0","+0:00","+00:0","+0:0",
"0", "+0:00","+00:0","+0:0",
"+000","+00000",
"+0:00:00","+00:0:00","+00:00:0","+0:0:0","+0:0:00","+00:0:0","+0:00:0",
"+01_00","+01;00","+01@00","+01:AA",
"1", "+01_00","+01;00","+01@00","+01:AA",
"+19","+19:00","+18:01","+18:00:01","+1801","+180001",
"-0","-0:00","-00:0","-0:0",
"-0:00","-00:0","-0:0",
"-000","-00000",
"-0:00:00","-00:0:00","-00:00:0","-0:0:0","-0:0:00","-00:0:0","-0:00:0",
"-19","-19:00","-18:01","-18:00:01","-1801","-180001",
Expand All @@ -176,6 +176,15 @@ public void test_factory_string_null() {
}

//-----------------------------------------------------------------------
@Test(groups={"tck"})
public void test_factory_string_singleDigitHours() {
for (int i = -9; i <= 9; i++) {
String str = (i < 0 ? "-" : "+") + Math.abs(i);
ZoneOffset test = ZoneOffset.of(str);
doTestOffset(test, i, 0, 0);
}
}

@Test(groups={"tck"})
public void test_factory_string_hours() {
for (int i = -18; i <= 18; i++) {
Expand Down
22 changes: 6 additions & 16 deletions src/test/java/javax/time/TestZoneId.java
Expand Up @@ -284,13 +284,16 @@ public void test_of_string_GMT(String id) {
Object[][] data_of_string_Fixed() {
return new Object[][] {
{"Z", "UTC:Z"},
{"+0", "UTC:Z"},
{"+5", "UTC:+05:00"},
{"+01", "UTC:+01:00"},
{"+0100", "UTC:+01:00"},{"+01:00", "UTC:+01:00"},
{"+010000", "UTC:+01:00"},{"+01:00:00", "UTC:+01:00"},
{"+12", "UTC:+12:00"},
{"+1234", "UTC:+12:34"},{"+12:34", "UTC:+12:34"},
{"+123456", "UTC:+12:34:56"},{"+12:34:56", "UTC:+12:34:56"},
{"-02", "UTC:-02:00"},
{"-5", "UTC:-05:00"},
{"-0200", "UTC:-02:00"},{"-02:00", "UTC:-02:00"},
{"-020000", "UTC:-02:00"},{"-02:00:00", "UTC:-02:00"},
};
Expand Down Expand Up @@ -360,11 +363,6 @@ public void test_of_string_UTC_invalid(String id) {
ZoneId.of("UTC" + id);
}

@Test(dataProvider="String_UTC_Invalid", expectedExceptions=DateTimeException.class)
public void test_of_string_UTCp0_invalid(String id) {
ZoneId.of("UTC+0");
}

@Test(dataProvider="String_UTC_Invalid", expectedExceptions=DateTimeException.class)
public void test_of_string_GMT_invalid(String id) {
ZoneId.of("GMT" + id);
Expand Down Expand Up @@ -400,17 +398,9 @@ public void test_ofUnchecked_string_invalid(String id) {
//-----------------------------------------------------------------------
public void test_of_string_GMT0() {
ZoneId test = ZoneId.of("GMT0");
assertEquals(test.getId(), "GMT0");
assertEquals(test.getGroupId(), "TZDB");
assertEquals(test.getRegionId(), "GMT0");
assertEquals(test.getRules().isFixedOffset(), true);
}

public void test_of_string_groupGMT0() {
ZoneId test = ZoneId.of("TZDB:GMT0");
assertEquals(test.getId(), "GMT0");
assertEquals(test.getGroupId(), "TZDB");
assertEquals(test.getRegionId(), "GMT0");
assertEquals(test.getId(), "UTC:Z");
assertEquals(test.getGroupId(), "UTC");
assertEquals(test.getRegionId(), "Z");
assertEquals(test.getRules().isFixedOffset(), true);
}

Expand Down

0 comments on commit 1a5baea

Please sign in to comment.