8066982: ZonedDateTime.parse() returns wrong ZoneOffset around DST fall transition

In Parsed.java the method resolveInstant() is altered such that, the offset (if present) will be given priority over zone.

Reviewed-by: rriggs, scolebourne
This commit is contained in:
Ramanand Patil 2015-12-25 16:43:37 +03:00 committed by Ivan Gerasimov
parent 741ed62a7c
commit c6d209b505
4 changed files with 241 additions and 8 deletions

View File

@ -80,6 +80,7 @@ import java.time.DateTimeException;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.Chronology;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser;
@ -473,6 +474,17 @@ import java.util.Set;
* day-of-week was valid for the date.
* <li>If an {@linkplain #parsedExcessDays() excess number of days}
* was parsed then it is added to the date if a date is available.
* <li> If a second-based field is present, but {@code LocalTime} was not parsed,
* then the resolver ensures that milli, micro and nano second values are
* available to meet the contract of {@link ChronoField}.
* These will be set to zero if missing.
* <li>If both date and time were parsed and either an offset or zone is present,
* the field {@link ChronoField#INSTANT_SECONDS} is created.
* If an offset was parsed then the offset will be combined with the
* {@code LocalDateTime} to form the instant, with any zone ignored.
* If a {@code ZoneId} was parsed without an offset then the zone will be
* combined with the {@code LocalDateTime} to form the instant using the rules
* of {@link ChronoLocalDateTime#atZone(ZoneId)}.
* </ol>
*
* @implSpec

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -594,15 +594,16 @@ final class Parsed implements TemporalAccessor {
private void resolveInstant() {
// add instant seconds if we have date, time and zone
// Offset (if present) will be given priority over the zone.
if (date != null && time != null) {
if (zone != null) {
long instant = date.atTime(time).atZone(zone).getLong(ChronoField.INSTANT_SECONDS);
Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
if (offsetSecs != null) {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
long instant = date.atTime(time).atZone(offset).toEpochSecond();
fieldValues.put(INSTANT_SECONDS, instant);
} else {
Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
if (offsetSecs != null) {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
long instant = date.atTime(time).atZone(offset).getLong(ChronoField.INSTANT_SECONDS);
if (zone != null) {
long instant = date.atTime(time).atZone(zone).toEpochSecond();
fieldValues.put(INSTANT_SECONDS, instant);
}
}

View File

@ -751,7 +751,7 @@ public class TCKZonedDateTime extends AbstractDateTimeTest {
{"2012-06-30T12:30:40Z[GMT]", 2012, 6, 30, 12, 30, 40, 0, "GMT"},
{"2012-06-30T12:30:40Z[UT]", 2012, 6, 30, 12, 30, 40, 0, "UT"},
{"2012-06-30T12:30:40Z[UTC]", 2012, 6, 30, 12, 30, 40, 0, "UTC"},
{"2012-06-30T12:30:40+01:00[Z]", 2012, 6, 30, 12, 30, 40, 0, "Z"},
{"2012-06-30T12:30:40+01:00[Z]", 2012, 6, 30, 11, 30, 40, 0, "Z"},
{"2012-06-30T12:30:40+01:00[+01:00]", 2012, 6, 30, 12, 30, 40, 0, "+01:00"},
{"2012-06-30T12:30:40+01:00[GMT+01:00]", 2012, 6, 30, 12, 30, 40, 0, "GMT+01:00"},
{"2012-06-30T12:30:40+01:00[UT+01:00]", 2012, 6, 30, 12, 30, 40, 0, "UT+01:00"},

View File

@ -0,0 +1,220 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package tck.java.time.format;
import static org.testng.AssertJUnit.assertEquals;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Testing DateTimeFormatter Parsing with 4 different test conditions:
* 1. When Zone and Offset not provided
* 2. When Zone and Offset provided
* 3. When Offset is not provided and Zone is provided
* 4. When Zone is not provided and Offset is provided
*/
@Test
public class TCKDTFParsedInstant {
private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin");
private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul");
private DateTimeFormatter dtFormatter;
private ZonedDateTime zdt1, zdt2;
private LocalDateTime ldt1;
private OffsetDateTime odt1;
@BeforeMethod
public void setUp() throws Exception {
dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
}
@DataProvider(name="parseWithoutZoneWithoutOffset")
Object[][] data_parse_WithoutOffset_WithoutZone() {
return new Object[][] {
{"1966-12-31T00:01:10", LocalDateTime.of(1966, 12, 31, 0, 1, 10)},
{"1970-01-01T00:00:00", LocalDateTime.of(1970, 1, 1, 0, 0, 0)},
{"2004-02-29T00:30:00", LocalDateTime.of(2004, 2, 29, 0, 30, 0)},
{"2015-12-31T23:59:59", LocalDateTime.of(2015, 12, 31, 23, 59, 59)}
};
}
@Test(dataProvider="parseWithoutZoneWithoutOffset")
public void testWithoutZoneWithoutOffset(String ldtString, LocalDateTime expectedLDT) {
dtFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
ldt1 = LocalDateTime.parse(ldtString, dtFormatter);
assertEquals(expectedLDT, ldt1);
}
@DataProvider(name="parseWithZoneWithOffset")
Object[][] data_parse_WithZone_WithOffset() {
return new Object[][] {
{"2012-10-28T01:45:00-02:30[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN},
{"2012-10-28T01:45:00-01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN},
{"2012-10-28T01:45:00-00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN},
{"2012-10-28T01:45:00+00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN},
{"2012-10-28T01:45:00+01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN},
{"2012-10-28T01:45:00+02:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN},
{"2012-10-28T01:45:00+03:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00-02:30[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN},
{"2012-10-28T02:45:00-01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00-00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00+00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00+01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00+02:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00+03:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00-02:30[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN},
{"2012-10-28T03:45:00-01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00-00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00+00:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00+01:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00+02:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN},
{"2012-10-28T03:45:00+03:00[Europe/Berlin]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN},
{"2012-10-28T02:45:00-02:30[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00-01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00-00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00+00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00+01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00+02:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL},
{"2012-10-28T02:45:00+03:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00-02:30[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00-01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00-00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00+00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00+01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00+02:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL},
{"2012-10-28T03:45:00+03:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00-02:30[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00-01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00-00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00+00:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00+01:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00+02:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL},
{"2012-10-28T04:45:00+03:00[Asia/Istanbul]",
LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL}
};
}
@Test(dataProvider="parseWithZoneWithOffset")
public void testWithZoneWithOffset(String zdtString, LocalDateTime ldt, ZoneOffset offset, ZoneId zone) {
dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
zdt1 = ZonedDateTime.ofInstant(ldt, offset, zone);
zdt2 = ZonedDateTime.parse(zdtString, dtFormatter);
assertEquals(zdt1, zdt2);
}
@DataProvider(name="parseWithZoneWithoutOffset")
Object[][] data_parse_WithZone_WithoutOffset() {
return new Object[][] {
{"28 Oct 00:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 0, 45, 0, 0, EUROPE_BERLIN)},
{"28 Oct 01:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, EUROPE_BERLIN)},
{"28 Oct 02:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, EUROPE_BERLIN)},
{"28 Oct 03:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, EUROPE_BERLIN)},
{"28 Oct 04:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, EUROPE_BERLIN)},
{"28 Oct 01:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, ASIA_ISTANBUL)},
{"28 Oct 02:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ASIA_ISTANBUL)},
{"28 Oct 03:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, ASIA_ISTANBUL)},
{"28 Oct 04:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, ASIA_ISTANBUL)},
{"28 Oct 05:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 5, 45, 0, 0, ASIA_ISTANBUL)}
};
}
@Test(dataProvider="parseWithZoneWithoutOffset")
public void testWithZoneWithoutOffset(String withZoneWithoutOffset, ZonedDateTime expectedZDT) {
dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV");
zdt1 = ZonedDateTime.parse(withZoneWithoutOffset, dtFormatter);
assertEquals(expectedZDT, zdt1);
}
@DataProvider(name="parseWithOffsetWithoutZone")
Object[][] data_parse_WithOffset_WithoutZone() {
return new Object[][] {
{"2015-12-14T00:45:00-11:30", OffsetDateTime.of(2015, 12, 14, 0, 45, 0, 0, ZoneOffset.of("-11:30"))},
{"2015-12-14T01:45:00-05:00", OffsetDateTime.of(2015, 12, 14, 1, 45, 0, 0, ZoneOffset.of("-05:00"))},
{"2015-12-14T02:45:00-00:00", OffsetDateTime.of(2015, 12, 14, 2, 45, 0, 0, ZoneOffset.of("-00:00"))},
{"2015-12-14T03:45:00+00:00", OffsetDateTime.of(2015, 12, 14, 3, 45, 0, 0, ZoneOffset.of("+00:00"))},
{"2015-12-14T04:45:00+03:30", OffsetDateTime.of(2015, 12, 14, 4, 45, 0, 0, ZoneOffset.of("+03:30"))},
{"2015-12-14T05:45:00+10:00", OffsetDateTime.of(2015, 12, 14, 5, 45, 0, 0, ZoneOffset.of("+10:00"))}
};
}
@Test(dataProvider="parseWithOffsetWithoutZone")
public void testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD) {
dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
odt1 = OffsetDateTime.parse(odtString, dtFormatter);
assertEquals(expectedOTD, odt1);
}
}