Skip to content
Permalink
Browse files

Fixes `LocalDateTime` handling of BC dates (#1388)

The assertion in `SetObject310Test.testSetLocalDateTimeBc`  is

>   // -1997-06-30T23:59:59.999999 -> 1997-06-30 23:59:59.999999 BC

This is incorrect.  It is actually `-1996  == 1997 BC` because there is no year zero in AD/BC.

I used a `DateTimeFormatter` in the test to prove my case. This “fix” for this method is to fix `TimeStampUtils.appendDate` and stop using the proleptic year (aka `YEAR`) and start using the year of the era (aka `YEAR_OF_ERA`).
  • Loading branch information...
kdubb authored and davecramer committed Jan 16, 2019
1 parent a7f63bf commit ebada4afb25864bc2d8ba900b433a849c69decf8
@@ -836,7 +836,7 @@ public synchronized String toString(LocalDateTime localDateTime) {
}

private static void appendDate(StringBuilder sb, LocalDate localDate) {
int year = Math.abs(localDate.getYear()); // year is negative for BC dates
int year = localDate.get(ChronoField.YEAR_OF_ERA);
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
appendDate(sb, year, month, day);
@@ -29,8 +29,12 @@
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.chrono.IsoChronology;
import java.time.chrono.IsoEra;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Arrays;
@@ -40,6 +44,26 @@
public class SetObject310Test {
private static final TimeZone saveTZ = TimeZone.getDefault();

public static final DateTimeFormatter LOCAL_TIME_FORMATTER =
new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.optionalStart()
.appendOffset("+HH:mm", "+00")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendPattern("GG")
.toFormatter()
.withResolverStyle(ResolverStyle.LENIENT)
.withChronology(IsoChronology.INSTANCE);

private Connection con;

@Before
@@ -313,8 +337,7 @@ public void testSetLocalDateTimeBc() throws SQLException {
bcDates.add(LocalDateTime.parse("0997-06-30T23:59:59.999999").with(ChronoField.ERA, IsoEra.BCE.getValue()));

for (LocalDateTime bcDate : bcDates) {
// -1997-06-30T23:59:59.999999 -> 1997-06-30 23:59:59.999999 BC
String expected = bcDate.toString().substring(1).replace('T', ' ') + " BC";
String expected = LOCAL_TIME_FORMATTER.format(bcDate);
localTimestamps(ZoneOffset.UTC, bcDate, expected);
}
}

0 comments on commit ebada4a

Please sign in to comment.
You can’t perform that action at this time.