add date parsing method that returns epochMillis instead of date object
This commit is contained in:
@@ -1,7 +1,17 @@
|
||||
package org.lucares.pdbui.date;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
@@ -43,7 +53,7 @@ public class FastISODateParserTest {
|
||||
@Test(dataProvider = "providerValidDate")
|
||||
public void testParseValidDate(final String date) {
|
||||
|
||||
final OffsetDateTime actualDate = FastISODateParser.parse(date);
|
||||
final OffsetDateTime actualDate = new FastISODateParser().parse(date);
|
||||
|
||||
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
|
||||
Assert.assertEquals(actualDate, expectedDate);
|
||||
@@ -81,6 +91,104 @@ public class FastISODateParserTest {
|
||||
|
||||
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "providerParseInvalidDate")
|
||||
public void testParseInvalidDate(final String invalidDate) {
|
||||
FastISODateParser.parse(invalidDate);
|
||||
new FastISODateParser().parse(invalidDate);
|
||||
}
|
||||
|
||||
@DataProvider(name = "providerDateToTimestamp")
|
||||
public Object[][] providerDateToTimestamp() {
|
||||
return new Object[][] { //
|
||||
{ "2018-11-18T14:42:49.123Z" }, //
|
||||
// There are no leap seconds in java-time:
|
||||
// In reality, UTC has a leap second 2016-12-31T23:59:60Z, but java handles
|
||||
// this differently. This makes it a little bit easier for us, because we do not
|
||||
// have to handle this.
|
||||
{ "2016-12-31T23:59:59.999Z" }, // before leap second
|
||||
{ "2017-01-01T00:00:00.000Z" }, // after leap second
|
||||
|
||||
// normal leap days exist
|
||||
{ "2016-02-28T23:59:59.999Z" }, // before leap day
|
||||
{ "2016-02-29T00:00:00.000Z" }, // leap day
|
||||
{ "2016-02-29T23:59:59.999Z" }, // leap day
|
||||
{ "2016-03-01T00:00:00.000Z" }, // after leap day
|
||||
|
||||
// dates with non-UTC timezones
|
||||
{ "2018-11-18T14:42:49.123+12:34" }, //
|
||||
{ "2018-11-18T02:34:56.123+12:34" }, //
|
||||
|
||||
// dates with non-UTC timezones and leap days
|
||||
{ "2016-02-29T00:59:59.999+01:00" }, // before leap day
|
||||
{ "2016-02-29T01:00:00.000+01:00" }, // leap day
|
||||
{ "2016-03-01T00:59:59.999+01:00" }, // leap day
|
||||
{ "2016-03-01T01:00:00.000+01:00" }, // after leap day
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerDateToTimestamp")
|
||||
public void testDateToTimestamp(final String date) {
|
||||
|
||||
final long actualEpochMilli = new FastISODateParser().parseAsTimestamp(date);
|
||||
|
||||
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
|
||||
final long expectedEpochMilli = expectedDate.toInstant().toEpochMilli();
|
||||
Assert.assertEquals(actualEpochMilli, expectedEpochMilli);
|
||||
}
|
||||
|
||||
@Test(enabled = false)
|
||||
public void test() {
|
||||
|
||||
final OffsetDateTime expectedDate = OffsetDateTime
|
||||
.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-12-31T23:00:00.000Z"));
|
||||
|
||||
final long epochMilli = expectedDate.toInstant().toEpochMilli();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
|
||||
final long timestamp = epochMilli + i * 10000;
|
||||
final OffsetDateTime date = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.UTC);
|
||||
|
||||
System.out.println(timestamp + " " + date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws IOException, InterruptedException {
|
||||
final Path path = Path.of("/home/andi/ws/performanceDb/data/production/dates2.csv");
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
final List<String> dates = new ArrayList<>();
|
||||
|
||||
try (final BufferedReader reader = new BufferedReader(
|
||||
new FileReader(path.toFile(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
dates.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
System.gc();
|
||||
TimeUnit.MILLISECONDS.sleep(100);
|
||||
System.gc();
|
||||
TimeUnit.MILLISECONDS.sleep(100);
|
||||
System.gc();
|
||||
TimeUnit.MILLISECONDS.sleep(100);
|
||||
System.gc();
|
||||
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
|
||||
final long start = System.nanoTime();
|
||||
final FastISODateParser fastISODateParser = new FastISODateParser();
|
||||
|
||||
for (final String date : dates) {
|
||||
fastISODateParser.parseAsTimestamp(date);
|
||||
// final long timestamp =
|
||||
// fastISODateParser.parse(date).toInstant().toEpochMilli();
|
||||
// final long timestamp = OffsetDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
|
||||
// .toInstant().toEpochMilli();
|
||||
// sum += timestamp;
|
||||
}
|
||||
|
||||
final double millis = (System.nanoTime() - start) / 1_000_000.0;
|
||||
final long datesPerSecond = (long) (dates.size() / (millis / 1000.0));
|
||||
System.out.println("duration: " + millis + "ms ; speed: " + datesPerSecond + " dates/s");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user