Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LocalDateTimeTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.math.BigDecimal;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.time.Instant;
  21. import java.time.LocalDate;
  22. import java.time.LocalDateTime;
  23. import java.time.ZoneId;
  24. import java.time.format.DateTimeFormatter;
  25. import java.time.temporal.TemporalAccessor;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.HashSet;
  31. import java.util.LinkedHashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Set;
  35. import java.util.TimeZone;
  36. import java.util.TreeSet;
  37. import java.util.UUID;
  38. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  39. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  40. import com.healthmarketscience.jackcess.impl.RowIdImpl;
  41. import com.healthmarketscience.jackcess.impl.RowImpl;
  42. import com.healthmarketscience.jackcess.impl.TableImpl;
  43. import com.healthmarketscience.jackcess.util.LinkResolver;
  44. import junit.framework.TestCase;
  45. import static com.healthmarketscience.jackcess.TestUtil.*;
  46. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  47. import static com.healthmarketscience.jackcess.Database.*;
  48. /**
  49. *
  50. * @author James Ahlborn
  51. */
  52. public class LocalDateTimeTest extends TestCase
  53. {
  54. public LocalDateTimeTest(String name) throws Exception {
  55. super(name);
  56. }
  57. public void testAncientDates() throws Exception
  58. {
  59. ZoneId zoneId = ZoneId.of("America/New_York");
  60. DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
  61. List<String> dates = Arrays.asList("1582-10-15", "1582-10-14",
  62. "1492-01-10", "1392-01-10");
  63. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  64. Database db = createMem(fileFormat);
  65. db.setZoneId(zoneId);
  66. db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);
  67. Table table = new TableBuilder("test")
  68. .addColumn(new ColumnBuilder("name", DataType.TEXT))
  69. .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME))
  70. .toTable(db);
  71. for(String dateStr : dates) {
  72. LocalDate ld = LocalDate.parse(dateStr, sdf);
  73. table.addRow("row " + dateStr, ld);
  74. }
  75. List<String> foundDates = new ArrayList<String>();
  76. for(Row row : table) {
  77. foundDates.add(sdf.format(row.getLocalDateTime("date")));
  78. }
  79. assertEquals(dates, foundDates);
  80. db.close();
  81. }
  82. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.OLD_DATES)) {
  83. Database db = openCopy(testDB);
  84. db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);
  85. Table t = db.getTable("Table1");
  86. List<String> foundDates = new ArrayList<String>();
  87. for(Row row : t) {
  88. foundDates.add(sdf.format(row.getLocalDateTime("DateField")));
  89. }
  90. assertEquals(dates, foundDates);
  91. db.close();
  92. }
  93. }
  94. public void testZoneId() throws Exception
  95. {
  96. ZoneId zoneId = ZoneId.of("America/New_York");
  97. doTestZoneId(zoneId);
  98. zoneId = ZoneId.of("Australia/Sydney");
  99. doTestZoneId(zoneId);
  100. }
  101. private static void doTestZoneId(final ZoneId zoneId) throws Exception
  102. {
  103. final TimeZone tz = TimeZone.getTimeZone(zoneId);
  104. ColumnImpl col = new ColumnImpl(null, null, DataType.SHORT_DATE_TIME, 0, 0, 0) {
  105. @Override
  106. protected TimeZone getTimeZone() { return tz; }
  107. @Override
  108. protected ZoneId getZoneId() { return zoneId; }
  109. };
  110. SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
  111. df.setTimeZone(tz);
  112. long startDate = df.parse("2012.01.01").getTime();
  113. long endDate = df.parse("2013.01.01").getTime();
  114. Calendar curCal = Calendar.getInstance(tz);
  115. curCal.setTimeInMillis(startDate);
  116. DateTimeFormatter sdf = DateTimeFormatter.ofPattern("uuuu.MM.dd HH:mm:ss");
  117. while(curCal.getTimeInMillis() < endDate) {
  118. Date curDate = curCal.getTime();
  119. LocalDateTime curLdt = LocalDateTime.ofInstant(
  120. Instant.ofEpochMilli(curDate.getTime()), zoneId);
  121. LocalDateTime newLdt = ColumnImpl.ldtFromLocalDateDouble(
  122. col.toDateDouble(curDate));
  123. if(!curLdt.equals(newLdt)) {
  124. System.out.println("FOO " + curLdt + " " + newLdt);
  125. assertEquals(sdf.format(curLdt), sdf.format(newLdt));
  126. }
  127. curCal.add(Calendar.MINUTE, 30);
  128. }
  129. }
  130. }