提交 ac795f43 编写于 作者: S sherman

8033662: DateTimeFormatter parsing ignores withZone()

Summary: to include the set "zone" in resolved result
Reviewed-by: sherman, chegar
上级 4ef20345
......@@ -1934,8 +1934,8 @@ public final class DateTimeFormatter {
*/
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
ParsePosition pos = (position != null ? position : new ParsePosition(0));
Parsed unresolved = parseUnresolved0(text, pos);
if (unresolved == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
DateTimeParseContext context = parseUnresolved0(text, pos);
if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
String abbr;
if (text.length() > 64) {
abbr = text.subSequence(0, 64).toString() + "...";
......@@ -1950,7 +1950,7 @@ public final class DateTimeFormatter {
pos.getIndex(), text, pos.getIndex());
}
}
return unresolved.resolve(resolverStyle, resolverFields);
return context.toResolved(resolverStyle, resolverFields);
}
/**
......@@ -1993,10 +1993,14 @@ public final class DateTimeFormatter {
* @throws IndexOutOfBoundsException if the position is invalid
*/
public TemporalAccessor parseUnresolved(CharSequence text, ParsePosition position) {
return parseUnresolved0(text, position);
DateTimeParseContext context = parseUnresolved0(text, position);
if (context == null) {
return null;
}
return context.toUnresolved();
}
private Parsed parseUnresolved0(CharSequence text, ParsePosition position) {
private DateTimeParseContext parseUnresolved0(CharSequence text, ParsePosition position) {
Objects.requireNonNull(text, "text");
Objects.requireNonNull(position, "position");
DateTimeParseContext context = new DateTimeParseContext(this);
......@@ -2007,7 +2011,7 @@ public final class DateTimeFormatter {
return null;
}
position.setIndex(pos); // errorIndex not updated from input
return context.toParsed();
return context;
}
//-----------------------------------------------------------------------
......@@ -2128,23 +2132,23 @@ public final class DateTimeFormatter {
@Override
public Object parseObject(String text, ParsePosition pos) {
Objects.requireNonNull(text, "text");
Parsed unresolved;
DateTimeParseContext context;
try {
unresolved = formatter.parseUnresolved0(text, pos);
context = formatter.parseUnresolved0(text, pos);
} catch (IndexOutOfBoundsException ex) {
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
}
return null;
}
if (unresolved == null) {
if (context == null) {
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
}
return null;
}
try {
TemporalAccessor resolved = unresolved.resolve(formatter.resolverStyle, formatter.resolverFields);
TemporalAccessor resolved = context.toResolved(formatter.resolverStyle, formatter.resolverFields);
if (parseType == null) {
return resolved;
}
......
......@@ -64,10 +64,12 @@ package java.time.format;
import java.time.ZoneId;
import java.time.chrono.Chronology;
import java.time.chrono.IsoChronology;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
/**
......@@ -77,8 +79,8 @@ import java.util.function.Consumer;
* It has the ability to store and retrieve the parsed values and manage optional segments.
* It also provides key information to the parsing methods.
* <p>
* Once parsing is complete, the {@link #toParsed()} is used to obtain the data.
* It contains a method to resolve the separate parsed fields into meaningful values.
* Once parsing is complete, the {@link #toUnresolved()} is used to obtain the unresolved
* result data. The {@link #toResolved()} is used to obtain the resolved result.
*
* @implSpec
* This class is a mutable context intended for use from a single thread.
......@@ -309,16 +311,27 @@ final class DateTimeParseContext {
}
/**
* Gets the result of the parse.
* Gets the unresolved result of the parse.
*
* @return the result of the parse, not null
*/
Parsed toParsed() {
Parsed toUnresolved() {
return currentParsed();
}
/**
* Gets the resolved result of the parse.
*
* @return the result of the parse, not null
*/
TemporalAccessor toResolved(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {
Parsed parsed = currentParsed();
parsed.effectiveChrono = getEffectiveChronology();
return parsed;
parsed.chrono = getEffectiveChronology();
parsed.zone = (parsed.zone != null ? parsed.zone : formatter.getZone());
return parsed.resolve(resolverStyle, resolverFields);
}
//-----------------------------------------------------------------------
/**
* Gets the first value that was parsed for the specified field.
......
......@@ -135,10 +135,6 @@ final class Parsed implements TemporalAccessor {
* Whether a leap-second is parsed.
*/
boolean leapSecond;
/**
* The effective chronology.
*/
Chronology effectiveChrono;
/**
* The resolver style to use.
*/
......@@ -241,7 +237,6 @@ final class Parsed implements TemporalAccessor {
fieldValues.keySet().retainAll(resolverFields);
}
this.resolverStyle = resolverStyle;
chrono = effectiveChrono;
resolveFields();
resolveTimeLenient();
crossCheck();
......@@ -266,14 +261,16 @@ final class Parsed implements TemporalAccessor {
TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);
if (resolvedObject != null) {
if (resolvedObject instanceof ChronoZonedDateTime) {
ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime) resolvedObject;
if (zone.equals(czdt.getZone()) == false) {
ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime<?>) resolvedObject;
if (zone == null) {
zone = czdt.getZone();
} else if (zone.equals(czdt.getZone()) == false) {
throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);
}
resolvedObject = czdt.toLocalDateTime();
}
if (resolvedObject instanceof ChronoLocalDateTime) {
ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime) resolvedObject;
ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime<?>) resolvedObject;
updateCheckConflict(cldt.toLocalTime(), Period.ZERO);
updateCheckConflict(cldt.toLocalDate());
changedCount++;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册