diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-12-28 04:19:21 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-12-28 04:19:21 +0000 |
commit | 0f568a46209da6557cfeab5f351bc6ee432eb3ea (patch) | |
tree | d028bfaf7a201d55acd842e400869d9ed2eb1a03 /src/test | |
parent | 61024b0227e3bd4455a4e55fae0e498f40fdda5a (diff) | |
download | jackcess-0f568a46209da6557cfeab5f351bc6ee432eb3ea.tar.gz jackcess-0f568a46209da6557cfeab5f351bc6ee432eb3ea.zip |
add system prop for date/time type; rework how date/times are written based on date/time type
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jdk8@1253 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/test')
3 files changed, 93 insertions, 1 deletions
diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 1aa2483..0a33a99 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -995,6 +995,10 @@ public class DatabaseTest extends TestCase public TimeZone getTimeZone() { return tz; } @Override public ZoneId getZoneId() { return null; } + @Override + public ColumnImpl.DateTimeFactory getDateTimeFactory() { + return getDateTimeFactory(DateTimeType.DATE); + } }; SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd"); diff --git a/src/test/java/com/healthmarketscience/jackcess/LocalDateTimeTest.java b/src/test/java/com/healthmarketscience/jackcess/LocalDateTimeTest.java index 637629a..e17bbfb 100644 --- a/src/test/java/com/healthmarketscience/jackcess/LocalDateTimeTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/LocalDateTimeTest.java @@ -62,7 +62,71 @@ public class LocalDateTimeTest extends TestCase super(name); } - public void testAncientDates() throws Exception + public void testWriteAndReadLocalDate() throws Exception { + for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = createMem(fileFormat); + + db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME); + + Table table = new TableBuilder("test") + .addColumn(new ColumnBuilder("name", DataType.TEXT)) + .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME)) + .toTable(db); + + // since jackcess does not really store millis, shave them off before + // storing the current date/time + long curTimeNoMillis = (System.currentTimeMillis() / 1000L); + curTimeNoMillis *= 1000L; + + DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); + List<Date> dates = + new ArrayList<Date>( + Arrays.asList( + df.parse("19801231 00:00:00"), + df.parse("19930513 14:43:27"), + null, + df.parse("20210102 02:37:00"), + new Date(curTimeNoMillis))); + + Calendar c = Calendar.getInstance(); + for(int year = 1801; year < 2050; year +=3) { + for(int month = 0; month <= 12; ++month) { + for(int day = 1; day < 29; day += 3) { + c.clear(); + c.set(Calendar.YEAR, year); + c.set(Calendar.MONTH, month); + c.set(Calendar.DAY_OF_MONTH, day); + dates.add(c.getTime()); + } + } + } + + ((DatabaseImpl)db).getPageChannel().startWrite(); + try { + for(Date d : dates) { + table.addRow("row " + d, d); + } + } finally { + ((DatabaseImpl)db).getPageChannel().finishWrite(); + } + + List<LocalDateTime> foundDates = new ArrayList<LocalDateTime>(); + for(Row row : table) { + foundDates.add(row.getLocalDateTime("date")); + } + + assertEquals(dates.size(), foundDates.size()); + for(int i = 0; i < dates.size(); ++i) { + Date expected = dates.get(i); + LocalDateTime found = foundDates.get(i); + assertSameDate(expected, found); + } + + db.close(); + } + } + + public void testAncientLocalDates() throws Exception { ZoneId zoneId = ZoneId.of("America/New_York"); DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu-MM-dd"); @@ -130,6 +194,10 @@ public class LocalDateTimeTest extends TestCase public TimeZone getTimeZone() { return tz; } @Override public ZoneId getZoneId() { return zoneId; } + @Override + public ColumnImpl.DateTimeFactory getDateTimeFactory() { + return getDateTimeFactory(DateTimeType.LOCAL_DATE_TIME); + } }; SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd"); diff --git a/src/test/java/com/healthmarketscience/jackcess/TestUtil.java b/src/test/java/com/healthmarketscience/jackcess/TestUtil.java index b5db277..83b2d7d 100644 --- a/src/test/java/com/healthmarketscience/jackcess/TestUtil.java +++ b/src/test/java/com/healthmarketscience/jackcess/TestUtil.java @@ -27,6 +27,10 @@ import java.io.PrintWriter; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -379,6 +383,22 @@ public class TestUtil } } + static void assertSameDate(Date expected, LocalDateTime found) + { + if((expected == null) && (found == null)) { + return; + } + if((expected == null) || (found == null)) { + throw new AssertionError("Expected " + expected + ", found " + found); + } + + LocalDateTime expectedLdt = LocalDateTime.ofInstant( + Instant.ofEpochMilli(expected.getTime()), + ZoneId.systemDefault()); + + Assert.assertEquals(expectedLdt, found); + } + static void copyFile(File srcFile, File dstFile) throws IOException { |