Skip to content

Commit c0a3ded

Browse files
committed
Handle updating of wrongly formatted timestamps
Handle rewriting timestamps from the specific RSS feed's format to a more unified Instant string format. If a DateTimeParseException is thrown while attempting to work with the date format, use that opportunity to attempt to parse that original date with the given date format pattern, then convert it into a string using 'Instant#toString'. If the conversion fails, simply return an empty Optional and let the rest of the code handle it from there since the new value will be overwritten in any case. Signed-off-by: Chris Sdogkos <work@chris-sdogkos.com>
1 parent 85dc995 commit c0a3ded

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/features/rss/RSSHandlerRoutine.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.io.IOException;
3131
import java.time.Instant;
32+
import java.time.ZonedDateTime;
3233
import java.time.format.DateTimeFormatter;
3334
import java.time.format.DateTimeParseException;
3435
import java.time.temporal.ChronoUnit;
@@ -188,16 +189,24 @@ private Optional<Predicate<Item>> prepareItemPostPredicate(RSSFeed feedConfig,
188189
return Optional.empty();
189190
}
190191

191-
Optional<Instant> lastSavedDate =
192-
getLastSavedDateFromDatabaseRecord(rssFeedRecord.orElseThrow());
192+
Instant lastSavedDate;
193+
try {
194+
lastSavedDate = getLastSavedDateFromDatabaseRecord(rssFeedRecord.orElseThrow());
195+
} catch (DateTimeParseException _) {
196+
Optional<Instant> convertedDate = convertDateTimeToInstant(feedConfig);
193197

194-
if (lastSavedDate.isEmpty()) {
195-
return Optional.empty();
198+
if (convertedDate.isEmpty()) {
199+
return Optional.empty();
200+
}
201+
202+
lastSavedDate = convertedDate.get();
196203
}
197204

205+
final Instant convertedLastSavedDate = lastSavedDate;
206+
198207
return Optional.of(item -> {
199208
Instant itemPubDate = getDateTimeFromItem(item, feedConfig.dateFormatterPattern());
200-
return itemPubDate.isAfter(lastSavedDate.orElseThrow());
209+
return itemPubDate.isAfter(convertedLastSavedDate);
201210
});
202211
}
203212

@@ -222,13 +231,9 @@ private Optional<RssFeedRecord> getRssFeedRecordFromDatabase(RSSFeed feedConfig)
222231
* @return An {@link Optional} containing the last saved date if it could be retrieved and
223232
* parsed successfully, otherwise an empty {@link Optional}
224233
*/
225-
private Optional<Instant> getLastSavedDateFromDatabaseRecord(RssFeedRecord rssRecord)
234+
private Instant getLastSavedDateFromDatabaseRecord(RssFeedRecord rssRecord)
226235
throws DateTimeParseException {
227-
try {
228-
return Optional.of(Instant.parse(rssRecord.getLastDate()));
229-
} catch (DateTimeParseException _) {
230-
return Optional.empty();
231-
}
236+
return Instant.parse(rssRecord.getLastDate());
232237
}
233238

234239
/**
@@ -332,6 +337,31 @@ private static boolean isValidDateFormat(Item rssItem, RSSFeed feedConfig) {
332337
return true;
333338
}
334339

340+
private Optional<Instant> convertDateTimeToInstant(RSSFeed feedConfig) {
341+
Optional<RssFeedRecord> feedOptional = getRssFeedRecordFromDatabase(feedConfig);
342+
String dateTimeFormat = feedConfig.dateFormatterPattern();
343+
344+
if (feedOptional.isEmpty() || dateTimeFormat.isEmpty()) {
345+
return Optional.empty();
346+
}
347+
348+
RssFeedRecord feedRecord = feedOptional.get();
349+
String lastDate = feedRecord.getLastDate();
350+
351+
ZonedDateTime zonedDateTime;
352+
try {
353+
zonedDateTime =
354+
ZonedDateTime.parse(lastDate, DateTimeFormatter.ofPattern(dateTimeFormat));
355+
} catch (DateTimeParseException exception) {
356+
logger.error(
357+
"Attempted to convert date time from database ({}) to instant, but failed:",
358+
lastDate, exception);
359+
return Optional.empty();
360+
}
361+
362+
return Optional.of(zonedDateTime.toInstant());
363+
}
364+
335365
/**
336366
* Attempts to find text channels from a given RSS feed configuration.
337367
*

0 commit comments

Comments
 (0)