Skip to content

Commit

Permalink
Fix parsing of Etc/GMT+1
Browse files Browse the repository at this point in the history
Also fixes similar zones which have a longer form like Etc/GMT+10
Fixes #21
  • Loading branch information
jodastephen committed Dec 9, 2014
1 parent f1ae2cf commit 5e9389c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

<!-- types are add, fix, remove, update -->
<release version="1.2" date="SNAPSHOT" description="v1.2">
<action dev="jodastephen" type="fix" >
Fix parsing of Etc/GMT+1 and similar zones which have a longer form like Etc/GMT+10.
Fixes #21.
</action>
<action dev="jodastephen" type="fix" >
Remove references to LocaleServiceProvider.
Fixes #22.
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/threeten/bp/format/DateTimeFormatterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3505,21 +3505,27 @@ public int parse(DateTimeParseContext context, CharSequence text, int position)

// parse
String parsedZoneId = null;
String lastZoneId = null;
while (tree != null) {
int nodeLength = tree.length;
if (position + nodeLength > length) {
break;
}
lastZoneId = parsedZoneId;
parsedZoneId = text.subSequence(position, position + nodeLength).toString();
tree = tree.get(parsedZoneId, context.isCaseSensitive());
}
ZoneId zone = convertToZone(regionIds, parsedZoneId, context.isCaseSensitive());
if (zone == null) {
if (context.charEquals(nextChar, 'Z')) {
context.setParsed(ZoneOffset.UTC);
return position + 1;
zone = convertToZone(regionIds, lastZoneId, context.isCaseSensitive());
if (zone == null) {
if (context.charEquals(nextChar, 'Z')) {
context.setParsed(ZoneOffset.UTC);
return position + 1;
}
return ~position;
}
return ~position;
parsedZoneId = lastZoneId;
}
context.setParsed(zone);
return position + parsedZoneId.length();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/threeten/bp/format/TestDateTimeFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.threeten.bp.LocalDate;
import org.threeten.bp.LocalTime;
import org.threeten.bp.YearMonth;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.temporal.TemporalAccessor;
import org.threeten.bp.temporal.TemporalQuery;

Expand Down Expand Up @@ -485,4 +487,14 @@ public void test_toFormat_Class() throws Exception {
BASIC_FORMATTER.toFormat(null);
}

//-------------------------------------------------------------------------
public void test_parse_allZones() throws Exception {
for (String zoneStr : ZoneId.getAvailableZoneIds()) {
ZoneId zone = ZoneId.of(zoneStr);
ZonedDateTime base = ZonedDateTime.of(2014, 12, 31, 12, 0, 0, 0, zone);
ZonedDateTime test = ZonedDateTime.parse(base.toString());
assertEquals(test, base);
}
}

}

0 comments on commit 5e9389c

Please sign in to comment.