Skip to content

Commit

Permalink
Unhandled timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling committed Jun 1, 2023
1 parent 96b7cd2 commit 912c6d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/main/java/com/apptasticsoftware/rssreader/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static ZonedDateTime toZonedDateTime(String dateTime) {
throw new IllegalArgumentException("Unknown date time format " + dateTime);
}

if (dateTime.length() == 19 ||
if (dateTime.length() == 19 || ((dateTime.length() == 29 || dateTime.length() == 32 || dateTime.length() == 35) && dateTime.charAt(10) == 'T') ||
((dateTime.length() == 24 || dateTime.length() == 25) && dateTime.charAt(3) == ',')) {
// Missing time zone information use default time zone. If not setting any default time zone system default
// time zone is used.
Expand Down Expand Up @@ -228,7 +228,7 @@ private static DateTimeFormatter getDateTimeFormatter(String dateTime, boolean s
}

private static DateTimeFormatter parseIsoDateTime(String dateTime) {
if (dateTime.length() >= 20 && dateTime.length() <= 31 && dateTime.charAt(4) == '-' && dateTime.charAt(10) == 'T')
if (dateTime.length() >= 20 && dateTime.length() <= 35 && dateTime.charAt(4) == '-' && dateTime.charAt(10) == 'T')
return ISO_OFFSET_DATE_TIME;
else if (dateTime.length() == 19 && dateTime.charAt(10) == 'T')
return ISO_LOCAL_DATE_TIME;
Expand Down Expand Up @@ -358,6 +358,15 @@ public static Long toEpochMilli(String dateTime) {
return zonedDateTime.toInstant().toEpochMilli();
}

public static Instant toInstant(String dateTime) {
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);

if (zonedDateTime == null)
return null;

return zonedDateTime.toInstant();
}

/**
* Comparator comparing publication date of Item class. Sorted in ascending order (oldest first)
*
Expand All @@ -370,7 +379,7 @@ public static Long toEpochMilli(String dateTime) {
@SuppressWarnings("java:S1133")
@Deprecated(since="3.3.0", forRemoval=true)
public static Comparator<Item> pubDateComparator() {
return Comparator.comparing(i -> i.getPubDate().map(DateTime::toEpochMilli).orElse(0L));
return Comparator.comparing(i -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apptasticsoftware.rssreader.DateTime;
import com.apptasticsoftware.rssreader.Item;

import java.time.Instant;
import java.util.Comparator;

/**
Expand All @@ -20,7 +21,7 @@ private ItemComparator() {
* @return comparator
*/
public static <I extends Item> Comparator<I> oldestItemFirst() {
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toEpochMilli).orElse(0L));
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH));
}

/**
Expand All @@ -29,7 +30,7 @@ public static <I extends Item> Comparator<I> oldestItemFirst() {
* @return comparator
*/
public static <I extends Item> Comparator<I> newestItemFirst() {
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toEpochMilli).orElse(0L)).reversed();
return Comparator.comparing((I i) -> i.getPubDate().map(DateTime::toInstant).orElse(Instant.EPOCH)).reversed();
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/apptasticsoftware/rssreader/DateTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ void dateTimeFormat14() {
assertEquals(1677829433000L, timestamp);
}

@Test
void dateTimeFormat15() {
var timestamp = DateTime.toEpochMilli("2023-02-28T17:37:08.823050123+00:00");
assertEquals(1677605828823L, timestamp);

timestamp = DateTime.toEpochMilli("2023-02-28T17:37:08.823050+00:00");
assertEquals(1677605828823L, timestamp);

timestamp = DateTime.toEpochMilli("2023-02-28T17:37:08.823+00:00");
assertEquals(1677605828823L, timestamp);
}

@Test
void testWrongDayOfWeek() {
assertEquals(1423026000000L, DateTime.toEpochMilli("Monday, 04 Feb 2015 00:00:00 EST"));
Expand Down

0 comments on commit 912c6d7

Please sign in to comment.